cURL没有将文件发送到API CALL [英] cURL not sending file to API CALL
问题描述
我正在构建一个通过API运行的移动站点,并拥有一个API CALL处理程序类,该类可以执行我从主函数文件运行的所有调用.
I am building a mobile site that runs from an API and have an API CALL handler class which does all the calls which I run from a primary functions file.
这里的问题是我的文件未发送到API,并且无法识别什么是文件,并返回文件不存在的错误.
The issue here is my files are not being sent through to the API and it's not recognising what is a file and is returning a file not present error.
注意:问题已解决且工作代码如下
以下代码:
表格
<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>
上传功能
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');
}
}
处理方法
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;
}
CURL请求方法
/**
* 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;
}
推荐答案
好,我发现了问题所在,希望该解决方案将对很多不想更改代码方式的人有所帮助.别人.
OK I have found out what the issue was, hopefully the solution will help a lot of people who don't want to change how their code is in lieu of someone elses.
cURL没有检测到它应该以多部分形式发送,因此它以默认编码发送帖子,这意味着另一端没有收到$ _FILES变量.
cURL was not detecting that it was supposed to send this form as a multipart so it was sending the post as a default encoding meaning the other end wasn't receiving the $_FILES variable.
要解决此问题,您需要将postdata作为数组提供,我正在创建要发送的字符串,我已经删除了它,并为CURLOPT_POSTFIELDS提供了一个数组.
To solve this you need to give the postdata as an array, I was creating the string for the send, I have removed this and am giving the CURLOPT_POSTFIELDS an array.
使用cURL直接从表单上载时,另一个重要的事情是将文件的信息与实际文件一起包括在内.
Another important thing when uploading directly from a form using cURL is to include the information for your file along with the actual file.
我的API调用处理程序现在创建数组如下:
My API Call handler now created the array as follows:
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变量为$ _FILES ['tmp_name'],然后还必须定义文件名.我将使用解决方案来更新问题.
Take note that the $file variable is $_FILES['tmp_name'] You then also have to define the file name. I will be updating the question with the solution.
这篇关于cURL没有将文件发送到API CALL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!