我正在构建一个从 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>
上传功能
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;
}
卷曲请求方法
/**
* 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;
}