卷曲POST附件

时间:2014-11-09 19:22:02

标签: php curl http-post ringcentral

我需要在本地驱动器上附加一个pdf文件,并使用PHP CURL将其发布到API。

以下是RingCentral FaxOut API Documentation

$url = "https://service.ringcentral.com/faxapi.asp";

$data = array(

    'Username' => 'XXXXXXXXX', 
    'Password' => 'XXXXXXXXX',
    'Recipient' => 'XXXXXXXXXX|Navneet',
    'Coverpage' => 'Default',
    'Coverpagetext' => 'Testing Faxout API ',
    'Resolution' => 'High',
    "Sendtime"   => date('d:m:y H:i:s'),
    'Attachment' => file_get_contents(PATH_TO_FILE)

);

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch,CURLOPT_POST, count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);

API不会返回任何响应。我想,我没有正确发送附件。附件应该是二进制流。我试过base64_encode但没有成功。

如Request body示例中所示,附件的标题应该是这样的

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

2 个答案:

答案 0 :(得分:0)

您可以使用CURL向api发布任何内容,我的示例中的$doc是您要发布的内容,它可以是json_encoded文件,base64_encoded图像或pdf或其他任何内容。

$baseUri = https://service.ringcentral.com/faxapi.asp;

$doc = file_get_contents(PATH_TO_FILE);

$ci = curl_init();
curl_setopt($ci, CURLOPT_URL, $baseUri);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ci, CURLOPT_FORBID_REUSE, 0);
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ci, CURLOPT_POSTFIELDS, $doc);

// also you can specify any specific header like this : 

$h1['Content-Disposition'] =  'Content-Disposition'. ': ' . 'form-data'; // headers are key-value pairs right ? 

curl_setopt($ci, CURLOPT_HTTPHEADER, array_values($h1));  // you can use this line for each header as a new key-value pair 

$h2['name'] =  'name'. ': ' . 'Attachment'; 
curl_setopt($ci, CURLOPT_HTTPHEADER, array_values($h2));
$h3['filename'] =  'filename'. ': ' . 'C:\example.doc'; 
curl_setopt($ci, CURLOPT_HTTPHEADER, array_values($h3));

$response = curl_exec($ci);

**注意**:首先检查你的file_get_contents函数是否有效是个好主意: 所以在另一个php文件中检查一下:

echo file_get_contents(PATH_TO_FILE);

看看它是否正确回响

答案 1 :(得分:0)

<块引用>

这是函数以及我如何为请求生成信息

function send_curl_request_with_attachment($method, $headers, $url, $post_fields) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
if($headers != "" && count($headers) > 0){
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$result = curl_exec($ch); 
curl_close($ch);  
return $result;}

$token_slams = "Authorization: Bearer " . $access_token;
$authHeader = array(
$token_slams,
'Accept: application/form-data');

$schedule_path ='../../documents/' . $docs_record["document"];
$cFile = curl_file_create($schedule_path);

$post = array(
    'old_record' => $old_record,
    'employer_number' => $employer_number,
    'payment_date' => $payment_date,
    'fund_year' => $fund_year,
    'fund_month' => $fund_month,
    'employer_schedule'=> $cFile
);

send_curl_request_with_attachment("POST", $authHeader, $my_url, $post);