cURL没有将文件发送到API CALL

And*_*ira 0 php curl file-upload file

我正在构建一个从API运行的移动站点,并且有一个API CALL处理程序类,它执行从主函数文件运行的所有调用.

这里的问题是我的文件没有被发送到API并且它没有识别什么是文件并且返回文件不存在错误.

注意:下面的问题解决和工作代码

代码如下:

形成

<form id="uploadPhoto" action="<?php uploadStreamPhoto(); ?>" method="post" enctype="multipart/form-data">
    <input type="file" name="streamPhotoUpload" id="streamPhotoUpload" />
    <input type="submit" name="streamPhotoUploadSubmit" id="streamPhotoUploadSubmit" value="Upload" />
</form>
Run Code Online (Sandbox Code Playgroud)

上传功能

function uploadStreamPhoto()
{

    if(isset($_POST['streamPhotoUploadSubmit']))
    {

        $apiHandler = new APIHandler();
        $result = $apiHandler->uploadStreamPhoto($_FILES['streamPhotoUpload']['tmp_name']);
        $json = json_decode($result);
        var_dump($json);

        //header('Location: '.BASE_URL.'stream-upload-preview');

    }

}
Run Code Online (Sandbox Code Playgroud)

HANDLER方法

public function uploadStreamPhoto($file)
{

    $result = $this->request(API_URL_ADD_PHOTO, array(
    'accessToken' => $this->accessToken,
    'file' => "@$file;filename=".time().".jpg",
    'photoName' => time(),
    'albumName' => 'Stream'
    )); 

    return $result;

}
Run Code Online (Sandbox Code Playgroud)

CURL REQUEST METHOD

/**
* Creates a curl request with the information passed in post fields
*
* @access private
* @param string $url
* @param array $postFields
* @return string
**/
private function request($url, $postFields = array())
{

    $curl = curl_init();

    //Check the SSL Matches the host
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

    if($this->debug == true)
    {

        //Prevent curl from verifying the certificate
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

    }

    //Set the URL to call
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);

    //Set the results to be returned
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    //Set the curl request as a post
    curl_setopt($curl, CURLOPT_POST, 1); 

    //Set the post fields
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields); 

    $result = curl_exec($curl);

    if($result === false)
    {

        $result = 'Curl error: '.curl_error($curl);

    }

    curl_close($curl);

    return $result;

}
Run Code Online (Sandbox Code Playgroud)

Lux*_*ian 7

对于那些在PHP 5.5发布之后结束的人来说,我只有2美分.有两件事值得一提:

PHP 5.5更改

在PHP 5.5中,引入了一个更改文件上载过程的新功能.rfc:curl-file-uploads描述得最好.因此,如果您使用的是PHP 5.5或更高版本,则应该尝试使用curl_file_create()而不是添加@/full/file/path为文件字段值.

在PHP 5.5或更高版本中使用遗留方法

如果您使用的是PHP 5.5或更高版本,则在使用旧的上载文件方式时可能会遇到问题.

首先是你必须使用CURLOPT_SAFE_UPLOAD选项并将其设置为FALSE.

其次,让我花费数小时调试的事情是你必须在设置之前这样做CULROPT_POSTFIELDS.如果你使用curl_setopt_array()那么之前CURLOPT_SAFE_UPLOAD应该添加到那个数组CURLOPT_POSTFIELDS.如果你正在使用curl_setopt()那么你只需要在CURLOPT_SAFE_UPLOAD之前设置.如果不这样做,将导致文件字段作为包含@/full/file/path字符串的文本发送,而不是正确上传文件.

使用遗留方法的示例,但即使使用较新的版本也应如此

<?php
$options = array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_SAFE_UPLOAD => FALSE,
  CURLOPT_POSTFIELDS => array(
    'text1' => 'test',
    'submit' => 'Send!',
    'file1' => '@' . realpath('images/a.jpg'),
    'file2' => '@' . realpath('images/b.jpg'),
  ),
);
$ch = curl_init();
// Needed for PHP > 5.5 to enable the old method of uploading file.
// Make sure to include this before CURLOPT_POSTFIELDS.
if (defined('CURLOPT_SAFE_UPLOAD')) {
  curl_setopt($ch, CURLOPT_SAFE_UPLOAD, FALSE);
}
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

完整代码在这里

PHP 5.5或更高版本应该像这样使用

$options = array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_POSTFIELDS => array(
    'text1' => 'test',
    'submit' => 'Send!',
    'file1' => curl_file_create(realpath('images/a.jpg')),
    'file2' => curl_file_create(realpath('images/b.jpg')),
  ),
);

$ch = curl_init();
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

完整代码在这里