【问题标题】:Upload File Via PHP Curl PUT通过 PHP Curl PUT 上传文件
【发布时间】:2016-02-22 09:25:02
【问题描述】:

PUT - 处理 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>

【问题讨论】:

标签: php pdf curl postman


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    此代码对我有用,以便将文件上传到 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 ...
    }
    

    【讨论】:

    • 我一直在努力解决 Oracle 对象存储问题,我用您的回答解决了这个问题。谢谢
    猜你喜欢
    • 2017-10-25
    • 2016-03-19
    • 2011-08-04
    • 1970-01-01
    • 2014-02-11
    • 2016-12-14
    • 1970-01-01
    • 2019-05-29
    相关资源
    最近更新 更多