2015-11-20 170 views
3

相當有點麻煩PUT -ting PDF。我已經設法使它在Postman中正常工作,使用下面的代碼(大代碼塊)並通過身體附加PDF作爲表單數據。我現在試圖在PHP中複製它。雖然附加PDF,但我遇到了問題。請儘快回覆!通過PHP Curl上傳文件PUT

我試過通過「CURLOPT_INFILE」,「CURLOPT_POSTFIELDS」無法連接PDF的衆多技術。

我通過創建文件:

$pdf = $_SERVER['DOCUMENT_ROOT'] . '/pdf/temp/temp.pdf'; 
$file = curl_file_create($pdf, 'application/pdf', 'receipt');` 

$file = new CURLFile($pdf, 'application/pdf', 'receipt'); 

我已經嘗試使用:

$file = fopen($pdf, 'rb'); 
$file = array('file' => $file); 

CURLOPT_POSTFIELDS  => $file, 
CURLOPT_INFILESIZE  => $fileSize, 
CURLOPT_INFILE   => $file 

沒有運氣,雖然。

<?php 

$curl = curl_init(); 

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://staging-tallie.com/v2/enterprise/ENTERPRISEID/MyReceipt/", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "PUT", 
    CURLOPT_POSTFIELDS => "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n\r\n-----011000010111000001101001--", 
    CURLOPT_HTTPHEADER => array(
    "accept: application/json; charset=utf-8", 
    "cache-control: no-cache", 
    "content-type: multipart/form-data; boundary=---011000010111000001101001", 
    "token: TOKEN", 
    "upload-filename: receipt.pdf" 
), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 

if ($err) { 
    echo "cURL Error #:" . $err; 
} else { 
    echo $response; 
} 

錯誤讀取:

<?xml version="1.0" encoding="utf-8"?> 
<ErrorResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <ResponseCode>400</ResponseCode> 
    <Message>Unable to Save the file to the Storage Service.</Message> 
</ErrorResponse> 
+2

是否PHP程序必須在目標目錄的寫權限」 –

+0

這可能幫助:http://stackoverflow.com/questions/6081131/upload-a-file-using-an-api-put-request –

+0

@DarwinvonCorax釘在頭上!謝謝先生! –

回答

2

400是指示該請求是不可能滿足一個HTTP響應代碼。這與隨附的消息文本一起表明,PHP進程沒有對目標目錄的寫入權限。