当前位置:首页 > PHP教程 > php上传下载 > 列表

PHP使用curl请求实现post方式上传图片文件功能示例

发布:smiling 来源: PHP粉丝网  添加日期:2018-11-15 09:38:00 浏览: 评论:0 

本文实例讲述了PHP使用curl请求实现post方式上传图片文件功能。分享给大家供大家参考,具体如下:

在调用第三方api接口时,有时会遇到通过http协议上传图片,以下是一个微信公众平台新增永久素材的例子;

php代码:

  1. /* 使用curl函数 */ 
  2. $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=image"
  3. $post_data = array
  4.   'media' => '@bag03.jpg'
  5. ); 
  6. $response = curl_http($url'POST'$post_data); 
  7. $params = array(); 
  8. $params = json_decode($response,true); 
  9. if (isset($params['errcode'])) 
  10.   echo "error:" . $params['errcode']; 
  11.   echo "msg :" . $params['errmsg']; 
  12.   exit
  13. var_dump( $params ); 
  14. /** 
  15.  * http请求方式: 默认GET 
  16.  */ 
  17. function curl_http($url$method="GET"$postfields){ 
  18.   $ch = curl_init(); 
  19.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  20.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
  21.   curl_setopt($ch, CURLOPT_URL, $url); 
  22.   switch ($method) { 
  23.     case "POST"
  24.       curl_setopt($ch, CURLOPT_POST, true); 
  25.       if (!emptyempty($postfields)) { 
  26.         $hadFile = false; 
  27.         if (is_array($postfields) && isset($postfields['media'])) { 
  28.           /* 支持文件上传 */ 
  29.           if (class_exists('\CURLFile')) { 
  30.             curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); 
  31.             foreach ($postfields as $key => $value) { 
  32.               if (isPostHasFile($value)) { 
  33.                 $postfields[$key] = new \CURLFile(realpath(ltrim($value'@'))); 
  34.                 $hadFile = true; 
  35.               } 
  36.             } 
  37.           } elseif (defined('CURLOPT_SAFE_UPLOAD')) { 
  38.             if (isPostHasFile($value)) { 
  39.               curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 
  40.               $hadFile = true; 
  41.             } 
  42.           } 
  43.         } 
  44.         $tmpdatastr = (!$hadFile && is_array($postfields)) ? http_build_query($postfields) : $postfields
  45.         curl_setopt($ch, CURLOPT_POSTFIELDS, $tmpdatastr); 
  46.       } 
  47.       break
  48.     default
  49.       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */ 
  50.       break
  51.   } 
  52.   $ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE; 
  53.   curl_setopt($ch, CURLOPT_URL, $url); 
  54.   if($ssl){ 
  55.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts 
  56.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在 
  57.   } 
  58.   $response = curl_exec($ch); 
  59.   curl_close($ch); 
  60.   if(emptyempty($response)){ 
  61.     exit("错误请求"); 
  62.   } 
  63.   return $response
  64. function isPostHasFile($value
  65. //phpfensi.com 
  66.   if (is_string($value) && strpos($value'@') === 0 && is_file(realpath(ltrim($value'@')))) { 
  67.     return true; 
  68.   } 
  69.   return false; 

也可以使用php内置的系统函数,如果使用过程中出现问题,建议查看是否启用相应的系统函数。

使用exec系统函数:

  1. /* 使用exec函数 */ 
  2. $command = 'curl -F media=@'.$filepath.' "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image"'
  3. $retval = array(); 
  4. exec($command$retval$status); 
  5. $params = array(); 
  6. $params = json_decode($retval[0],true); 
  7. if ($status != 0) { 
  8.   $params = array
  9.     'errcode'  => '-100'
  10.     'errmsg'  => '公众号服务出错,请联系管理员'
  11.   ); 
  12. return $params

使用system系统函数:

  1. /* 使用system函数 */ 
  2. $command = 'curl -F media=@'.$filepath.' "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image"'
  3. $retval = 1; 
  4. $last_line = system($command$retval); 
  5. $params = array(); 
  6. $params = json_decode($last_line,true); 
  7. if ($retval != 0) { 
  8.   if (isset($params['errcode'])) { 
  9.     $params = array
  10.       'errcode'  => '-100'
  11.       'errmsg'  => '公众号服务出错,请联系管理员'
  12.     ); //phpfensi.com 
  13.   } 
  14. return $params

Tags: curl请求 post方式上传

分享到: