首页 > 语言 > PHP > 正文

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

2024-09-04 11:46:58
字体:大 中 小
来源:转载
供稿:网友

本文实例讲述了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. { 
  11.   echo "error:" . $params['errcode']; 
  12.   echo "msg :" . $params['errmsg']; 
  13.   exit; 
  14. } 
  15. var_dump( $params ); 
  16. /** 
  17.  * http请求方式: 默认GET 
  18.  */ 
  19. function curl_http($url, $method="GET", $postfields){ 
  20.   $ch = curl_init(); 
  21.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  22.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
  23.   curl_setopt($ch, CURLOPT_URL, $url); 
  24.   switch ($method) { 
  25.     case "POST": 
  26.       curl_setopt($ch, CURLOPT_POST, true); 
  27.       if (!emptyempty($postfields)) { 
  28.         $hadFile = false; 
  29.         if (is_array($postfields) && isset($postfields['media'])) { 
  30.           /* 支持文件上传 */ 
  31.           if (class_exists('/CURLFile')) { 
  32.             curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); 
  33.             foreach ($postfields as $key => $value) { 
  34.               if (isPostHasFile($value)) { 
  35.                 $postfields[$key] = new /CURLFile(realpath(ltrim($value, '@'))); 
  36.                 $hadFile = true; 
  37.               } 
  38.             } 
  39.           } elseif (defined('CURLOPT_SAFE_UPLOAD')) { 
  40.             if (isPostHasFile($value)) { 
  41.               curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 
  42.               $hadFile = true; 
  43.             } 
  44.           } 
  45.         } 
  46.         $tmpdatastr = (!$hadFile && is_array($postfields)) ? http_build_query($postfields) : $postfields; 
  47.         curl_setopt($ch, CURLOPT_POSTFIELDS, $tmpdatastr); 
  48.       } 
  49.       break; 
  50.     default: 
  51.       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */ 
  52.       break; 
  53.   } 
  54.   $ssl = preg_match('/^https://///i',$url) ? TRUE : FALSE; 
  55.   curl_setopt($ch, CURLOPT_URL, $url); 
  56.   if($ssl){ 
  57.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts 
  58.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在 
  59.   } 
  60.   $response = curl_exec($ch); 
  61.   curl_close($ch); 
  62.   if(emptyempty($response)){ 
  63.     exit("错误请求"); 
  64.   } 
  65.   return $response; 
  66. } 
  67. function isPostHasFile($value) 
  68. { //Vevb.com 
  69.   if (is_string($value) && strpos($value, '@') === 0 && is_file(realpath(ltrim($value, '@')))) { 
  70.     return true; 
  71.   } 
  72.   return false; 
  73. } 

也可以使用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. } 
  13. 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.     ); //Vevb.com 
  13.   } 
  14. } 
  15. return $params; 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表