通过PHP Curl PUT上传文件

时间:2015-11-20 01:18:02

标签: php pdf curl postman

有很多麻烦 PUT -ting a PDF。我已经设法让它在Postman中正常工作,使用下面的代码(大代码块)并通过正文将PDF作为表单数据附加。我现在试图在PHP中复制它。我在附加PDF时遇到问题。请停下来!

我尝试过多种技术,试图通过" 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 个答案:

答案 0 :(得分:2)

400是HTTP响应代码,表示请求无法满足。这与随附的消息文本一起表明PHP进程没有对目标目录的写访问权。

答案 1 :(得分:0)

此代码对我有用,以便将文件上传到bluemix Cloud Object Storage。使用PUT方法提交表单后,文件将从临时文件夹中上传。上传之前,请不要忘记验证文件的MIME和扩展名。

if (is_uploaded_file($_FILES['my_file']['tmp_name'])){
    $ch = curl_init();

    $url = IBM_BLUEMIX_BUCKET_END_POINT.$bucket_name."/".$file_name; // give the file a unique name


    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_PUT, true); //PUT REQUEST                
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'x-amz-acl: public-read', //header required for bluemix 
        'Authorization: Bearer '.$access_token, // authorization for bluemix iam
        'Content-Type: '.$conten_type, //application/pdf or image/jpg
        'Expect: '
    ));

    $image_or_file = fopen($_FILES['my_file']['tmp_name'], "rb");

    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_INFILE, $image_or_file);
    curl_setopt($ch, CURLOPT_INFILESIZE, $_FILES[$fieldName]['size']);

    curl_setopt(
        $ch,
        CURLOPT_POSTFIELDS,
        array(
          'file' =>
              '@'            . $_FILES['my_file']['tmp_name']
              . ';filename=' . $_FILES['my_file']['name']
              . ';type='     . $conten_type //application/pdf or image/jpg
    ));

    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,16);
    curl_setopt($ch,CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking

    $response = curl_exec($ch);
    $headerSent = curl_getinfo($ch ); // request headers from response (check if something wrong)

    curl_close ($ch);
    fclose($image_or_file);

    if(!$response){ // or response
       // do something...
    }
}else{
    //File did not upload, do something ...
}