【问题标题】:HTTP Post to a web service using VBA with curl php example使用带有 curl php 示例的 VBA 将 HTTP 发布到 Web 服务
【发布时间】:2013-07-25 08:19:54
【问题描述】:

我正在使用 vba 设置一个 POST 函数,它会在我们得到它时将文本消息信息发布到我们的客户端。我在呼叫中心工作,所以一旦通话结束,我需要将数据发布给我们的客户,他们会自动发送一条短信。

我从我们的客户端收到了一个使用 Curl 用 PHP 编写的示例 API。我正在尝试将其转换为 vba。

这是php代码:

`public function SendSingleSMS($user, $pass, $mobile, $originator, $innerMessage,                                 $messageType) 
    {      
        //Send Data
        $fields = array
        (
            'user' => $user,
            'pass' => $pass,
            'func' => 8,
            'mobile' => $mobile,
            'orig' => $originator,
            'msg' => urlencode($innerMessage),
            'msgType' => $messageType
        );

        $response = $this->sendUsingCURL($this->apiURL, $fields);
        return $response;
    }

    //Send information
    private function sendUsingCURL($url, $fields)
    {
        //url-ify the data for the POST
        foreach($fields as $key=>$value) 
            $fields_string .= $key.'='.$value.'&';

        rtrim($fields_string, '&');

        //open connection
        $ch = curl_init();

        //WARNING: this would prevent curl from detecting a 'man in the middle' attack
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

        //execute post
        $response = curl_exec($ch);
        $err = curl_error($ch);

        curl_close($ch);

        if($response === false)
            throw new Exception(__CLASS__."::".__FUNCTION__."_".$err);

        return $response;
    }
}`

我已经尝试了所有方法并完成了所有搜索。我似乎无法毫无错误地将 vba 发布到此服务。

vba 代码:

    Dim oHttp As Object
Dim strServer, strUsername, strPassword, strMessage, strMobileNumber, strData  As String
Set oHttp = CreateObject("Microsoft.XMLHTTP")

'-- get message paramaters from screen fields
strUsername = "user"
strPassword = "pass"
strfunc = "8"
strmobile = "0773432111"
strorig = "71111"
strmsg = "HelloWorld"
strmsgtype = "4"
'-- build the HTTP POST dataset
strData = "&user=" + strUsername + "&pass=" + strPassword + "&func=" + strfunc + "&mobile=" + strmobile + "&orig=" + strorig + "&msg=" + strmsg + "&msgType=" + strmsgtype

'-- prepare the HTTP POST message
oHttp.Open "POST", "http://test.com/API/Link.php", False
oHttp.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
oHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oHttp.setRequestHeader "Accept", "*/*"
oHttp.setRequestHeader "Content-Length", CStr(Len(strData))
oHttp.setRequestHeader "Connection", "close"

'-- send the message
oHttp.send (strData)
        Text0 = strData
'-- get the response
MsgBox (oHttp.responseText)

有没有办法让 VBA 发布类似于 curl 帖子的数据?我尝试过的所有操作都给了我错误的登录信息,或者错误:

“消息中发现不兼容的字符。请仅使用与 GSM 兼容的字符(非标准撇号和破折号通常是罪魁祸首)”

【问题讨论】:

    标签: php http vba post curl


    【解决方案1】:

    我在将数据发布到 web 服务时遇到了类似的问题,最终将我的结果变成了一个库:(无耻的插件)https://github.com/VBA-tools/VBA-Web。虽然它主要使用 JSON,但它可能会有所帮助。

    如果不查看 API,我会说前导“&”可能会导致问题,根据我的经验,您不需要明确设置这些标头。要了解 Excel 发送的请求,您可以尝试Fiddler,它会捕获请求详细信息。

    更新:

    刚刚将更新推送到VBA-Web,这应该会有所帮助,使用以下示例:

    Dim Client As New WebClient
    Client.BaseUrl = "http://test.com/API/"
    
    Dim Request As New WebRequest
    Request.Resource = "Link.php"
    Request.Method = WebMethod.HttpPost
    Request.Format = WebFormat.FormUrlEncoded
    
    Request.AddBodyParameter "user", strUsername
    Request.AddBodyParameter "pass", strPassword
    Request.AddBodyParameter "func", strfunc
    Request.AddBodyParameter "mobile", strmobile
    Request.AddBodyParameter "orig", strorig
    Request.AddBodyParameter "msg", strmsg
    Request.AddBodyParameter "msgType", strmsgtype
    
    Dim Response As WebResponse
    Set Response = Client.Execute(Request)
    MsgBox Response.Content
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多