duanmei2805 2014-02-14 13:55
浏览 1773

如何使用php curl使用multipart / form-data body创建HTTP POST请求

I need to generate a HTTP post request accordingly to documentation found here.

Here is an example of how the request body should look like

Content-Type: multipart/form-data; boundary=---------------------------7d54b1fee05aa

-----------------------------7d54b1fee05aa
Content-Disposition: form-data; name="Username"

5556090455
-----------------------------7d54b1fee05aa
Content-Disposition: form-data; name="Password"

qwerty
-----------------------------7d54b1fee05aa
Content-Disposition: form-data; name="Attachment"; filename="C:\example.doc" 
<Document content is here>
-----------------------------7d54b1fee05aa
Content-Disposition: form-data; name="Recipient"

5556465589|John Doe
-----------------------------7d54b1fee05aa
Content-Disposition: form-data; name="Recipient"

5555568552|John Smith
-----------------------------7d54b1fee05aa
Content-Disposition: form-data; name="Coverpagetext"

This is a test fax from web
-----------------------------7d54b1fee05aa--

Here is the PHP code that I have so far

$postURL = 'https://service.ringcentral.com/faxapi.asp';
$ch  = curl_init($postURL);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/');

$hiddens = '';
$hiddens .= 'Username'.'='.'8558415124'.'&';
$hiddens .= 'Password'.'='.'Champion92'.'&';
$hiddens .= 'Recipient'.'='.'8882466583'.'&';
$hiddens .= 'Coverpage'.'='.'NONE'.'&';
$hiddens .= 'Coverpagetext'.'='.'asdf asdf'.'&';
$hiddens .= 'Attachment'.'='.'C:\example.doc'.'&';
$hiddens = substr($hiddens, 0, strlen($hiddens)-1);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,  $hiddens);

$page = curl_exec($ch);
echo $page;// output result
if ($page === FALSE) {
  var_dump(curl_getinfo($ch));
  exit( "Post: FAILED = ".curl_error($ch) );
}
curl_close($ch); // close the connection

I am getting a response of 1 (which means Authorization failed). I have the feeling that I am doing a normal post request and the authorization fails because the request is not well formatted.

How can I turn this as Multipart request? And how can I troubleshoot the HTTP header I am sending, not the one I receive?

  • 写回答

2条回答 默认 最新

  • duanben1909 2014-02-14 14:10
    关注

    Try using urlencode on the POST values like this:

    $hiddens = urlencode(substr($hiddens, 0, strlen($hiddens)-1));
    
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $hiddens);
    

    Without URL encoding the string it looks like this:

    Username=8558415124&Password=Champion92&Recipient=8882466583&Coverpage=NONE&Coverpagetext=asdf asdf&Attachment=C:\example.doc1
    

    With URL encoding the string looks like this:

    Username%3D8558415124%26Password%3DChampion92%26Recipient%3D8882466583%26Coverpage%3DNONE%26Coverpagetext%3Dasdf+asdf%26Attachment%3DC%3A%5Cexample.doc1
    

    Also, I am not too sure about how you are handling the file you are sending itself, because this does not look correct:

    Attachment=C:\example.doc1
    

    How exactly would a remote service have access to your C:\ drive? The file itself would have to be form encoded to be sent. Or perhaps the service requires that the file be on a publicly accessible URL?

    Looking at the documentation you link to it says it should be a binary stream:

    Description: Document to be faxed.
    Possible values: Original document file name must be passed in the filename field of the body-part header.
    Format: Binary stream.
    Number of occurrences: any.
    

    So you need to get that ironed out as well.

    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R