1. 程式人生 > >php使用curl新增微信臨時素材(上傳圖片)

php使用curl新增微信臨時素材(上傳圖片)

() command ltrim apps 地址 ken define image post

<?php$APPID=‘‘;
$APPSECRET=‘‘;
$info=json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET"));
//獲取token
$access_token=$info->access_token;

/* 使用curl函數 */
    $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token
&type=image";; $post_data = array( ‘media‘ => ‘@bag03.png‘, ); $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); /* //設置請求方式 */ 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; } //命令不會返回錯誤 如果返回空 2>&1 輸出報錯信息 如果亂碼就是用 header("content-type:text/html;charset=gbk"); curl下載地址 https://curl.haxx.se/download.html $filepath=‘./bag03.png‘; /* 使用exec函數 */ $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‘ => ‘公眾號服務出錯,請聯系管理員‘, ); } return $params; /* 使用system函數 */ $command = ‘curl -F media=@‘.$filepath.‘ "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=‘.$access_token.‘&type=image" 2>&1‘; $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‘ => ‘公眾號服務出錯,請聯系管理員‘, ); } } return $command;

php使用curl新增微信臨時素材(上傳圖片)