当前位置: 首页>>代码示例>>PHP>>正文


PHP HTTPRequest::addPostFields方法代码示例

本文整理汇总了PHP中HTTPRequest::addPostFields方法的典型用法代码示例。如果您正苦于以下问题:PHP HTTPRequest::addPostFields方法的具体用法?PHP HTTPRequest::addPostFields怎么用?PHP HTTPRequest::addPostFields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HTTPRequest的用法示例。


在下文中一共展示了HTTPRequest::addPostFields方法的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);
 }
开发者ID:sinkcup,项目名称:qiniu-sdk-php,代码行数:51,代码来源:RS.php


注:本文中的HTTPRequest::addPostFields方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。