PHP5.6 微信上傳永久圖片素材
阿新 • • 發佈:2019-01-02
由於PHP版本的問題 在公眾號開發過程中上傳圖片老是失敗,下面給大家提供一個PHP5.6以上的上傳圖片的例子
我使用的是Yii1.1框架 呼叫newupload方法 傳入引數AccessToken 圖片的路徑 就可以進行上傳了/** * 上傳永久圖片的介面------------------------------------------------------------------------ */ public function actionNewupload() { $TOKEN=$this->actionToken(); $file = "D:/upload/bb4.png"; $data = array( 'media'=> new CURLFile($file) ); $url = 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token='.$TOKEN.'&type=image'; $result = $this->curl_posts($url,$data); var_dump($result); } function curl_posts($url, $data, $header = array()){ if(function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if(is_array($header) && !empty($header)){ $set_head = array(); foreach ($header as $k=>$v){ $set_head[] = "$k:$v"; } curl_setopt($ch, CURLOPT_HTTPHEADER, $set_head); } curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 0);// 1s to timeout. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); if(curl_errno($ch)){ //error return curl_error($ch); } $reslut = curl_getinfo($ch); print_r($reslut); curl_close($ch); $info = array(); if($response){ $info = json_decode($response, true); } return $info; } else { throw new Exception('Do not support CURL function.'); } }
希望對你有幫助