Php cURL不向API调用发送文件
我正在构建一个从API运行的移动站点,并且有一个API调用处理程序类,它执行我从主函数文件运行的所有调用 这里的问题是我的文件没有发送到API,它无法识别什么是文件,并返回一个文件不存在错误 注意:问题已解决,工作代码如下 代码如下: 形式 处理程序方法Php cURL不向API调用发送文件,php,file,file-upload,curl,Php,File,File Upload,Curl,我正在构建一个从API运行的移动站点,并且有一个API调用处理程序类,它执行我从主函数文件运行的所有调用 这里的问题是我的文件没有发送到API,它无法识别什么是文件,并返回一个文件不存在错误 注意:问题已解决,工作代码如下 代码如下: 形式 处理程序方法 public function uploadStreamPhoto($file) { $result = $this->request(API_URL_ADD_PHOTO, array( 'accessToken' =&
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;
}
卷曲请求方法
/**
* 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;
}
根据您的需要使用这个curl函数,因为我正在使用它发送post-even文件中的数据
及
//正确路径=c:/images/a.jpg
curl_grab_page("url", $data);
好的,我已经发现了问题所在,希望这个解决方案能帮助很多不想改变代码替代他人的人 cURL没有检测到它应该将此表单作为多部分发送,因此它将post作为默认编码发送,这意味着另一端没有接收$\u FILES变量 为了解决这个问题,您需要将postdata作为数组提供,我正在为send创建字符串,我已经删除了这个字符串,并为CURLOPT_POSTFIELDS提供了一个数组 使用cURL直接从表单上载时,另一个重要的事情是将文件的信息与实际文件一起包含 我的API调用处理程序现在创建了数组,如下所示:
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;
}
请注意,$file变量是$\u FILES['tmp\u name'],然后还必须定义文件名。我将用解决方案更新这个问题。在PHP5.5发布后,我将为那些在这里结束的人支付2美分。有两件事值得一提: PHP5.5更改 在PHP5.5中引入了一个新函数,该函数更改了文件上载过程。描述得最好。因此,如果您使用的是PHP5.5或更新版本,您可能应该尝试使用而不是添加
@/full/file/path
作为文件字段值
在PHP5.5或更新版本中使用遗留方法
如果您使用的是PHP5.5或更新版本,那么在使用旧的上传文件方式时可能会遇到问题
首先,您必须使用CURLOPT\u SAFE\u UPLOAD
选项并将其设置为FALSE
其次,让我花费数小时进行调试的是,在设置CULROPT\u POSTFIELDS
之前,您必须这样做。如果使用curl\u setopt\u array()
则应在CURLOPT\u POSTFIELDS
之前将CURLOPT\u SAFE\u UPLOAD
添加到该数组中。如果您使用的是curl\u setopt()
,那么您只需在上传之前设置CURLOPT\u SAFE\u。否则将导致文件字段作为包含@/full/file/path
字符串的文本发送,而不是正确上载文件
使用遗留方法的示例,但即使使用较新的版本也应适用
复制和粘贴代码的内容不多,请您看看我发布的内容,并尝试帮助确定问题,我需要修复代码中的问题。您没有正确使用curl,这就是为什么我向您发送适用于所有内容的正确curl代码的原因$this->extractRequestPostFields($postFields)
在curl中不需要它,因为如果它是一个数组,它将自动以您想要的格式发送数据,即application/x-url-encoded和encrypt/multipart,并且您必须在文件存储位置添加一个正确的路径,而不是正确使用临时路径curl,这是文件应该发送到的部分,它没有深入研究文档,我想我现在知道问题出在哪里了。你可以很好地使用你的代码,但不要使用$_file[]['tmp']将它存储在某处,然后将路径粘贴到你的$file=c:/uploadedfile/filename;嗯,我明白你的意思了,好吧,我有个主意。非常感谢你,这是生命的拯救。我很高兴这有帮助。感谢您抽出时间回信!
function curl_grab_page($url,$data,$secure="false",$ref_url="",$login = "false",$proxy = "null",$proxystatus = "false")
{
if($login == 'true') {
$fp = fopen("cookie.txt", "w");
fclose($fp);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if ($proxystatus == 'true') {
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if($secure=='true')
{
curl_setopt($ch, CURLOPT_SSLVERSION,3);
}
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start();
return curl_exec ($ch); // execute the curl command
curl_getinfo($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
}
$data['FileName'] = '@'.$ProperPath;
curl_grab_page("url", $data);
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;
}
<?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);
$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);