国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP使用curl請求實現(xiàn)post方式上傳圖片文件功能示例

2024-09-04 11:46:58
字體:
來源:轉載
供稿:網友

本文實例講述了PHP使用curl請求實現(xiàn)post方式上傳圖片文件功能。分享給大家供大家參考,具體如下:

在調用第三方api接口時,有時會遇到通過http協(xié)議上傳圖片,以下是一個微信公眾平臺新增永久素材的例子;

php代碼:

  1. /* 使用curl函數(shù) */ 
  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. //Vevb.com 
  66.   if (is_string($value) && strpos($value'@') === 0 && is_file(realpath(ltrim($value'@')))) { 
  67.     return true; 
  68.   } 
  69.   return false; 

也可以使用php內置的系統(tǒng)函數(shù),如果使用過程中出現(xiàn)問題,建議查看是否啟用相應的系統(tǒng)函數(shù)。

使用exec系統(tǒng)函數(shù):

  1. /* 使用exec函數(shù) */ 
  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'  => '公眾號服務出錯,請聯(lián)系管理員'
  11.   ); 
  12. return $params

使用system系統(tǒng)函數(shù):

  1. /* 使用system函數(shù) */ 
  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'  => '公眾號服務出錯,請聯(lián)系管理員'
  12.     ); //Vevb.com 
  13.   } 
  14. return $params

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 怀宁县| 康平县| 黄冈市| 呼和浩特市| 北川| 夏邑县| 庆元县| 福海县| 驻马店市| 延津县| 海宁市| 阳信县| 建始县| 若羌县| 昂仁县| 江都市| 乌苏市| 古田县| 满城县| 景洪市| 浑源县| 宁都县| 淮滨县| 阳曲县| 镇康县| 鹤山市| 长治市| 鹿泉市| 乌拉特后旗| 泸州市| 广德县| 乌鲁木齐县| 洪泽县| 鄂温| 依兰县| 荥经县| 仁布县| 许昌县| 江门市| 澄城县| 荣成市|