本文整理匯總了PHP中HTTPRequest::addPostFile方法的典型用法代碼示例。如果您正苦於以下問題:PHP HTTPRequest::addPostFile方法的具體用法?PHP HTTPRequest::addPostFile怎麽用?PHP HTTPRequest::addPostFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類HTTPRequest
的用法示例。
在下文中一共展示了HTTPRequest::addPostFile方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: uploadFile
/**
* 上傳文件,要token認證
* @example shell curl -i -F 'file=@2.jpg' -F 'token=asdf' -F 'key=2.jpg' 'http://up.qiniu.com/'
* @example shell ./qrsync ./conf.json
* @return array array(
"httpUri" => "http://com-163-sinkcup-test.qiniudn.com/1.jpg",
"httpsUri" => "https://dn-com-163-sinkcup-test.qbox.me/1.jpg",
}
*/
public function uploadFile($localPath, $remoteFileName, $headers = array())
{
$remoteFileName = str_replace('/', '', $remoteFileName);
$uri = 'http://' . str_replace('//', '/', $this->conf['host']['up'] . '/');
//scope中指定文件,就可以覆蓋。如果隻寫bucket,則重複上傳會出現錯誤:614 文件已存在。
$policy = array('scope' => $this->bucket . ':' . $remoteFileName, 'deadline' => time() + 3600);
$pics = array('image/jpeg', 'image/webp', 'image/png');
//如果是圖片,則需要返回分辨率
if (isset($headers['Content-Type']) && in_array($headers['Content-Type'], $pics)) {
$policy['returnBody'] = json_encode(array('width' => '$(imageInfo.width)', 'height' => '$(imageInfo.height)'));
}
$data = $this->encode(json_encode($policy));
$token = $this->sign($data) . ':' . $data;
//$hash = hash_file('crc32b', $localPath);
//$tmp = unpack('N', pack('H*', $hash));
$fields = array('token' => $token, 'key' => $remoteFileName);
$http = new \HTTPRequest($uri, HTTP_METH_POST);
$contentType = isset($headers['Content-Type']) ? $headers['Content-Type'] : 'multipart/form-data';
$http->addPostFile('file', $localPath, $contentType);
$http->addPostFields($fields);
//$http->setHeader($headers);
$http->send();
$body = json_decode($http->getResponseBody(), true);
$code = $http->getResponseCode();
if ($code == 200) {
//自定義域名一定是http,因為證書不能跨域名
if (!isset($this->conf['customDomain']) || empty($this->conf['customDomain'])) {
$httpUri = 'http://' . str_replace('//', '/', $this->bucket . $this->conf['httpUriSuffix'] . '/' . $remoteFileName);
} else {
$httpUri = 'http://' . $this->conf['customDomain'] . '/' . $remoteFileName;
}
$r = array('httpUri' => $httpUri, 'httpsUri' => 'https://' . str_replace('//', '/', $this->conf['httpsUriPrefix'] . $this->bucket . $this->conf['httpsUriSuffix'] . '/' . $remoteFileName));
if (isset($body['width'])) {
$r['width'] = $body['width'];
}
if (isset($body['height'])) {
$r['height'] = $body['height'];
}
return $r;
}
throw new Exception($body['error'], $code);
}