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

首頁 > 語言 > PHP > 正文

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

2024-05-05 00:04:23
字體:
供稿:網(wǎng)友
本文實例講述了PHP使用curl請求實現(xiàn)post方式上傳圖片文件功能。分享給大家供大家參考,具體如下:

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

php代碼:

/* 使用curl函數(shù) */$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=image";$post_data = array(  'media' => '@bag03.jpg',);$response = curl_http($url, 'POST', $post_data);$params = array();$params = json_decode($response,true);if (isset($params['errcode'])){  echo "error:" . $params['errcode'];  echo "msg :" . $params['errmsg'];  exit;}var_dump( $params );/** * http請求方式: 默認GET */function curl_http($url, $method="GET", $postfields){  $ch = curl_init();  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  curl_setopt($ch, CURLOPT_URL, $url);  switch ($method) {    case "POST":      curl_setopt($ch, CURLOPT_POST, true);      if (!empty($postfields)) {        $hadFile = false;        if (is_array($postfields) && isset($postfields['media'])) {          /* 支持文件上傳 */          if (class_exists('/CURLFile')) {            curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);            foreach ($postfields as $key => $value) {              if (isPostHasFile($value)) {                $postfields[$key] = new /CURLFile(realpath(ltrim($value, '@')));                $hadFile = true;              }            }          } elseif (defined('CURLOPT_SAFE_UPLOAD')) {            if (isPostHasFile($value)) {              curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);              $hadFile = true;            }          }        }        $tmpdatastr = (!$hadFile && is_array($postfields)) ? http_build_query($postfields) : $postfields;        curl_setopt($ch, CURLOPT_POSTFIELDS, $tmpdatastr);      }      break;    default:      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); /* //設(shè)置請求方式 */      break;  }  $ssl = preg_match('/^https://///i',$url) ? TRUE : FALSE;  curl_setopt($ch, CURLOPT_URL, $url);  if($ssl){    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https請求 不驗證證書和hosts    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不從證書中檢查SSL加密算法是否存在  }  $response = curl_exec($ch);  curl_close($ch);  if(empty($response)){    exit("錯誤請求");  }  return $response;}function isPostHasFile($value){  if (is_string($value) && strpos($value, '@') === 0 && is_file(realpath(ltrim($value, '@')))) {    return true;  }  return false;}

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

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

/* 使用exec函數(shù) */$command = 'curl -F media=@'.$filepath.' "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image"';$retval = array();exec($command, $retval, $status);$params = array();$params = json_decode($retval[0],true);if ($status != 0) {  $params = array(    'errcode'  => '-100',    'errmsg'  => '公眾號服務(wù)出錯,請聯(lián)系管理員',  );}return $params;

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

/* 使用system函數(shù) */$command = 'curl -F media=@'.$filepath.' "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image"';$retval = 1;$last_line = system($command, $retval);$params = array();$params = json_decode($last_line,true);if ($retval != 0) {  if (isset($params['errcode'])) {    $params = array(      'errcode'  => '-100',      'errmsg'  => '公眾號服務(wù)出錯,請聯(lián)系管理員',    );  }}return $params;

希望本文所述對大家PHP程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 克东县| 甘谷县| 故城县| 祁东县| 九寨沟县| 金溪县| 九龙县| 富川| 铜陵市| 改则县| 阿勒泰市| 通辽市| 安溪县| 漳州市| 岢岚县| 海兴县| 札达县| 当阳市| 绍兴市| 连山| 桦南县| 衡水市| 多伦县| 新巴尔虎左旗| 弥勒县| 三江| 台湾省| 克什克腾旗| 原平市| 江城| 普宁市| 涟源市| 鄂伦春自治旗| 通河县| 右玉县| 西藏| 酒泉市| 慈溪市| 阿拉善盟| 布拖县| 江油市|