濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > PHP使用curl請(qǐng)求實(shí)現(xiàn)post方式上傳圖片文件功能示例

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

熱門標(biāo)簽:合肥外呼系統(tǒng)app 電銷機(jī)器人-快迭智能 智能外呼電銷系統(tǒng) h5 地圖標(biāo)注 沈陽人工智能電銷機(jī)器人公司 哈爾濱400電話辦理到易號(hào)網(wǎng) 拉薩打電話機(jī)器人 寶安400電話辦理 高識(shí)別電銷機(jī)器人

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

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

php代碼:

/* 使用curl函數(shù) */
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKENtype=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請(qǐng)求方式: 默認(rèn)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è)置請(qǐng)求方式 */
      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請(qǐng)求 不驗(yàn)證證書和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不從證書中檢查SSL加密算法是否存在
  }
  $response = curl_exec($ch);
  curl_close($ch);
  if(empty($response)){
    exit("錯(cuò)誤請(qǐng)求");
  }
  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_TOKENtype=image"';
$retval = array();
exec($command, $retval, $status);
$params = array();
$params = json_decode($retval[0],true);
if ($status != 0) {
  $params = array(
    'errcode'  => '-100',
    'errmsg'  => '公眾號(hào)服務(wù)出錯(cuò),請(qǐng)聯(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_TOKENtype=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'  => '公眾號(hào)服務(wù)出錯(cuò),請(qǐng)聯(lián)系管理員',
    );
  }
}
return $params;

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總》

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

您可能感興趣的文章:
  • PHP如何使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求
  • PHP中使用CURL發(fā)送get/post請(qǐng)求上傳圖片批處理功能
  • PHP基于curl模擬post提交json數(shù)據(jù)示例
  • PHP基于curl post實(shí)現(xiàn)發(fā)送url及相關(guān)中文亂碼問題解決方法
  • PHP中的使用curl發(fā)送請(qǐng)求(GET請(qǐng)求和POST請(qǐng)求)
  • 詳解php用curl調(diào)用接口方法,get和post兩種方式
  • PHP的CURL方法curl_setopt()函數(shù)案例介紹(抓取網(wǎng)頁,POST數(shù)據(jù))
  • PHP使用curl函數(shù)發(fā)送Post請(qǐng)求的注意事項(xiàng)
  • 淺談PHP模擬發(fā)送POST請(qǐng)求之curl基本使用

標(biāo)簽:成都 山東 林芝 威海 巴中 張家口 梅州 泰州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP使用curl請(qǐng)求實(shí)現(xiàn)post方式上傳圖片文件功能示例》,本文關(guān)鍵詞  PHP,使用,curl,請(qǐng)求,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP使用curl請(qǐng)求實(shí)現(xiàn)post方式上傳圖片文件功能示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP使用curl請(qǐng)求實(shí)現(xiàn)post方式上傳圖片文件功能示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    玉门市| 临安市| 乐亭县| 钟山县| 湖口县| 桐柏县| 舟山市| 门头沟区| 大名县| 阿城市| 阿勒泰市| 望城县| 恭城| 梁山县| 平昌县| 玉环县| 武鸣县| 西藏| 商河县| 乌兰县| 枣强县| 青浦区| 平顺县| 军事| 高平市| 阜城县| 河北省| 兴城市| 区。| 潍坊市| 房山区| 伽师县| 昌宁县| 涡阳县| 隆化县| 云霄县| 西畴县| 昭苏县| 阳泉市| 马鞍山市| 醴陵市|