2014-02-14 58 views
1

我需要根據文檔found here生成一個HTTP post請求。如何使用php curl創建帶有multipart/form-data body的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-- 

下面的例子是我迄今爲止

$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響應PHP代碼(這意味着授權失敗)。我感覺我正在做一個普通的發佈請求,並且授權失敗,因爲請求格式不正確。

我怎樣才能把它作爲多部分請求? 如何解決我發送的HTTP標題,而不是我收到的標題?

回答

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 

此外,我不太清楚你如何處理文件y OU正在發送本身,因爲這看起來不正確:

Attachment=C:\example.doc1 

究竟是如何將遠程服務訪問您的C:\驅動?文件本身必須被編碼才能被髮送。或者,該服務可能要求該文件位於可公開訪問的網址上?

看着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. 

所以,你需要獲得理順,以及。

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