2015-11-20 135 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进程没有对目标目录的写入权限。