Introduction to uploading temporary materials for PHP WeChat development

高洛峰
Release: 2017-03-13 13:20:28
Original
2895 people have browsed it

This article introduces relevant information on uploading temporary materials in PHP WeChat development. Interested friends can refer to it.

The example of this article shares the specific code for uploading temporary materials in PHP WeChat for everyone. For reference, the specific content is as follows


 public function uploadImg($imgUrl){

  $TOKEN=$this->getAccessToken();
  $URL ='http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token='.$TOKEN.'&type=image';
  $data = array('media'=>'@'.$imgUrl);
  $result = $this->curl_post($URL,$data);
  $data = @json_decode($result,true);
  
  return $data['media_id'];
 }

 public function getAccessToken(){

  $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxe574b1bd35d7d4da&secret=d4624c36b6795d1d99dcf0547af5443d'; 
  
  $result = json_decode($this->curlGet($url),true);

  return $result['access_token'];
 }

 function curl_post($url, $data = null)
{
 //创建一个新cURL资源
 $curl = curl_init();
 //设置URL和相应的选项 
 curl_setopt($curl, CURLOPT_URL, $url);
 if (!empty($data)){
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 }
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 //执行curl,抓取URL并把它传递给浏览器
 $output = curl_exec($curl);
 //关闭cURL资源,并且释放系统资源
 curl_close($curl);
 return $output;
}
Copy after login

CalluploadImg($imgurl) Pass the image address parameter
Return the result


array (
 'type' => 'image',
 'media_id' => 'W89mt3FEaxXOMOw0fLj2Cb6A8vfMjuXrj6XW59O3l9a7Tj_h2SjlBEr4dvp4Du2R',
 'created_at' => 1464140301,
Copy after login

The above is the detailed content of Introduction to uploading temporary materials for PHP WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!