douna6802 2013-07-25 08:19
浏览 106

HTTP使用带有curl php示例的VBA发布到Web服务

I am setting up a POST function using vba that will post text message information to our client as we get it. I work in a call center, so once the call has ending I need to post the data to our client who will automatically send out a text message.

I received an example API from our client written in PHP that uses Curl. I am trying to convert this into vba.

Heres the php code:

`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;
    }
}`

I have tried everything and done every search. I cant seem to make vba post to this service without an error.

The vba code:

    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)

Is there a way I can get VBA to post data similar to the curl post? Everything I've tried give me wrong login info, or the error:

"Incompatible characters found in message. Please only use GSM compatible characters (Non-standard apostrophes and dashes are commonly to blame)"

  • 写回答

1条回答 默认 最新

  • doukan4795 2014-02-22 02:34
    关注

    I was having similar issues POSTing data to webservices and ended up turning my results into a library: (shameless plug) https://github.com/VBA-tools/VBA-Web. Although it uses JSON primarily, it may be helpful.

    Without looking at the API, I would say the leading "&" may be causing an issue and in my experience you don't need to set those headers explicitly. To get an idea of the request that Excel is sending, you can try Fiddler, which captures request details.

    Update:

    Just pushed an update to VBA-Web that should help, using the following example:

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog