post上传图片

需求是这样的,需要把图片post上传到第三方的图片服务器上。使用到的方法如下:

function upload($fullName,$imgUrl){

   $uploadToken = '1131112319g8sd09g80';  //token

   $url = 'http://img.a.com';   //第三方请求地址
   $mimeBoundary = md5(microtime());
   $header = array('Content-Type'=>'multipart/form-data;boundary='.$mimeBoundary);
   $data = array();
   //需要传递的参数,这里只是用了一个密钥,fileName为需要将图片上传到的目的地址
   //$fullName 为'/a/b/asfsfaf.jpg'格式
  //如果有其他参数,需要像下面添加到$fields里面
   $fields = array(
         'secureKey'=>$uploadToken,
         'fileName'=>$fullName,
   );
   foreach ($fields as $name => $val) {
      array_push($data, '--' . $mimeBoundary);
      array_push($data, "Content-Disposition: form-data; name=\"$name\"");
      array_push($data, '');
      array_push($data, $val);
   }

   //文件
   array_push($data, '--' . $mimeBoundary);
   $name = 'file';
   $fileName = '1.jpg';
   $fileBody = file_get_contents($imgUrl);//$imgUrl为图片路径
   array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$fileName\"");
   array_push($data, 'Content-Type: application/octet-stream');
   array_push($data, '');
   array_push($data, $fileBody);

   array_push($data, '--' . $mimeBoundary . '--');
   array_push($data, '');

   $body = implode("\r\n", $data);
   $response = request($url, 'POST', $header, $body);
   return $response;
}

function request($path, $method, $headers = null, $body = null){
   $timeout = '';
   $ch  = curl_init($path);

   $_headers = array('Expect:');
   if (!is_null($headers) && is_array($headers)){
      foreach($headers as $k => $v) {
         array_push($_headers, "{$k}: {$v}");
      }
   }

   $length = 0;
   $date   = gmdate('D, d M Y H:i:s \G\M\T');

   if (!is_null($body)) {
      if(is_resource($body)){
         fseek($body, 0, SEEK_END);
         $length = ftell($body);
         fseek($body, 0);

         array_push($_headers, "Content-Length: {$length}");
         curl_setopt($ch, CURLOPT_INFILE, $body);
         curl_setopt($ch, CURLOPT_INFILESIZE, $length);
      } else {
         $length = @strlen($body);
         array_push($_headers, "Content-Length: {$length}");
         curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
      }
   } else {
      array_push($_headers, "Content-Length: {$length}");
   }

   // array_push($_headers, 'Authorization: ' . $this->sign($method, $uri, $date, $length));
   array_push($_headers, "Date: {$date}");

   curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);
   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

   if ($method == 'PUT' || $method == 'POST') {
      curl_setopt($ch, CURLOPT_POST, 1);
   } else {
      curl_setopt($ch, CURLOPT_POST, 0);
   }

   if ($method == 'HEAD') {
      curl_setopt($ch, CURLOPT_NOBODY, true);
   }

   $response = curl_exec($ch);
   $status   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   curl_close($ch);
   list($header, $body) = explode("\r\n\r\n", $response, 2);
   if ($status == 200) {
      if ($method == 'GET') {
         return $body;
      } else {
         return response($response);
      }
   } else {
      return false;
   }
}

function response($text){
   $headers = explode(PHP_EOL, $text);
   $items = array();
   foreach($headers as $header) {
      $header = trim($header);
      if(strpos($header, '{') !== False){
         $items = json_decode($header, 1);
         break;
      }
   }
   return $items;
}
upload('/a/c/d/m/adfsadfa.jpg','public/aaa.jpg');
将public/aaa.jpg上传到第三方服务器的/a/b/c/d/m/adfsadfa.jpg 下,这里的/a不一定是linux下的/目录...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值