curl_setopt()在php中尝试通过curl上载文件时引发错误异常
我发现了一些与上述相关的问题,但没有得到任何适当的答案。这是我的密码:curl_setopt()在php中尝试通过curl上载文件时引发错误异常,php,curl,Php,Curl,我发现了一些与上述相关的问题,但没有得到任何适当的答案。这是我的密码: $requestUrl = <MY_URL>; $filePath = <MY_FILE_PATH>; $post = array('extra_info' => '123456','file_contents'=>'@'.$filePath); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$requestUrl); curl_seto
$requestUrl = <MY_URL>;
$filePath = <MY_FILE_PATH>;
$post = array('extra_info' => '123456','file_contents'=>'@'.$filePath);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$requestUrl);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
任何帮助都将不胜感激 请参考此信息,有什么帮助吗 PHP5.5引入了一个CurlFile对象,该对象不支持旧的@filename语法 另见:
从PHP5.5.0开始,@前缀已被弃用,可以使用CURLFile发送文件。你可以查看手册 试试这个:
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Authorization: Bearer XXXXXXXXXXXXXXXYYYYYzzzzzz', 'Accept-Version: ~1', 'Except: 100-continue', 'Content-type: multipart/form-data'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$args['file'] = new CurlFile('<file_path>', 'text/plain', 'myfile');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$response = curl_exec($ch);
$ch=curl\u init($requestUrl);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“POST”);
curl_setopt($ch,CURLOPT_HTTPHEADER,
数组('Authorization:Bearer xxxxxxxxxxxxx yyyy zzzzzzzz','Accept Version:~1','Except:100 continue','Content type:multipart/form data');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLINFO_HEADER_OUT,true);
$args['file']=新卷发文件(''text/plain','myfile');
curl_setopt($ch,CURLOPT_POSTFIELDS,$args);
$response=curl\u exec($ch);
您使用的是什么php版本?我使用的是php版本5.5.9请在此处检查解决方案:
public function getCurlValue()
{
if (function_exists('curl_file_create')) {
return curl_file_create($this->filename, $this->contentType, $this->postname);
}
// Use the old style if using an older version of PHP
$value = "@{$this->filename};filename=" . $this->postname;
if ($this->contentType) {
$value .= ';type=' . $this->contentType;
}
return $value;
}
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Authorization: Bearer XXXXXXXXXXXXXXXYYYYYzzzzzz', 'Accept-Version: ~1', 'Except: 100-continue', 'Content-type: multipart/form-data'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$args['file'] = new CurlFile('<file_path>', 'text/plain', 'myfile');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$response = curl_exec($ch);