微信公众平台 接口

流程:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

wx_sample.php

  1 <?php 
  2 class CURL{  //把不同的方法封装到一个类中
  3 
  4     //get请求
  5     public static function getRequest($url){
  6         $ch=curl_init();
  7         curl_setopt($ch, CURLOPT_URL, $url);//请求借口  $url必须是http://开头
  8         curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);//定义返回值是否以字符串格式返回
  9         curl_setopt($ch, CURLOPT_HEADER, 0);
 10         $result=  curl_exec($ch);
 11         curl_close($ch);
 12         return $result;
 13     }
 14     //post请求
 15     public static function postRequest($url,$data){
 16         $ch= curl_init();
 17         curl_setopt($ch, CURLOPT_URL, $url);
 18         curl_setopt($ch, CURLOPT_POST, 1);//设置请求方式为post
 19         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//定义返回值是否以字符串格式返回
 20         curl_setopt($ch, CURLOPT_HEADER, 0);
 21        // curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
 22         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post请求参数
 23         $result=  curl_exec($ch);
 24         curl_close($ch);
 25         return $result;
 26     }    
 27 }
 28 
 29 class Token{
 30     //用来获取接入凭证
 31     private $appID="wx5c238169e3910d0f";
 32     private $appSeceret="b59a3a3e1b142efad166cee437eb81a7";
 33     public function getToken(){  //获取access_token的值,access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。
 34         $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; 
 35         $url1=  sprintf($url,$this->appID,$this->appSeceret);  //sprintf();依次匹配%s
 36         if(file_exists("token.php") && (time()- filemtime("token.php"))<7200){
 37             $result=  file_get_contents("token.php");
 38         }else{
 39             $result = CURL::getRequest($url1);
 40             unlink("token.php");
 41             file_put_contents("token.php", $result);
 42         }
 43         return $result; //返回的是json字符串
 44     }
 45 }
 46 //$a=new Token();
 47 //$a->getToken();
 48 //上传
 49 class Upload{
 50     public function tmpupload(){
 51         $url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";
 52         $t=new Token();
 53         $token=$t->getToken();
 54         $token_arr=json_decode($token,TRUE); //json字符串转换为数组
 55         if(isset($token_arr['errcode'])){
 56             echo "token获取失败";
 57             exit;
 58         }
 59         $url1=sprintf($url,$token_arr['access_token'],'image');
 60       //  echo $url1;
 61         $key="media\"; filename=\"1.jpg\r\nContent-Type:image\r\nAccept:\"";
 62         if(class_exists("CURLFile")){           
 63         $data=array(
 64            'media'=>new CURLFile(realpath('1.jpg'))
 65         );
 66     }else{
 67         $data=array(
 68             'media'=>'@'.realpath("1.jpg")
 69         );
 70         }
 71         $result=CURL::postRequest($url1,$data);
 72         file_put_contents("image_media.php", $result);
 73 //        var_dump($result);
 74     }
 75 
 76     public function getImage(){
 77         $url="https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s";
 78         $t=new Token();
 79         $token=$t->getToken();
 80         $token_arr=  json_decode($token,TRUE);
 81         if(isset($token_arr['errcode'])){
 82             echo "token获取失败";
 83             exit;
 84         }
 85         $media=  file_get_contents("image_media.php");
 86         $media_arr=  json_decode($media,true);
 87         if(isset($media_arr['errcode'])){
 88             echo "media失败";
 89             exit;
 90         }
 91         $url=  sprintf($url,$token_arr['access_token'],$media_arr['media_id']);
 92         $result=CURL::getRequest($url);
 93         var_dump($result);
 94         exit;
 95 //        echo $result;
 96 //        header("Content-Disposition:attachment;filename=2.jpg");
 97        // header("Content-Type:image/jpeg");
 98     }
 99 }
100 $u=new Upload();
101 //$u->tmpupload();
102 $u->getImage();

 

posted on 2016-12-07 16:27  PHP博客园  阅读(195)  评论(0)    收藏  举报

导航