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

时间:2014-02-14 13:55:24

标签: php web-services api curl multipartform-data

我需要根据文档found here生成HTTP post请求。

以下是请求正文应如何显示的示例

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

这是我到目前为止的PHP代码

$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

我的响应为1(表示授权失败)。我觉得我正在做一个正常的帖子请求,授权失败,因为请求格式不正确。

如何将其作为Multipart请求? 我如何解决我发送的HTTP标头,而不是我收到的HTTP标头?

2 个答案:

答案 0 :(得分:0)

尝试在POST值上使用urlencode,如下所示:

$hiddens = urlencode(substr($hiddens, 0, strlen($hiddens)-1));

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

如果没有URL编码字符串,它看起来像这样:

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

使用URL编码时,字符串如下所示:

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

另外,我不太确定你如何处理你自己发送的文件,因为这看起来不正确:

Attachment=C:\example.doc1

远程服务如何准确访问您的C:\驱动器?文件本身必须进行表格编码才能发送。或者,该服务可能要求该文件位于可公开访问的URL上?

查看the documentation you link to它说它应该是二进制流

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.

所以你需要解决这个问题。

答案 1 :(得分:0)

对于用户名,请尝试在传真号码前附加1,在附件前附加@,即

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