首先一點點準備工作,
本地開發時,需要將本地ip 寫攻到白名單中,不然無法獲取token.
知道自己的appid 和appSecret
微信的助手類。提供獲取token 向api 請求等功能封裝。用法示例如下。
//獲取token 值
$this->access_token = WxHelper::getAccessToken(0 , $this->app_id , $this->secret );
//向api 請求。
$url = "https://sz.api.weixin.qq.com/cgi-bin/menu/create?access_token=" .$token;
$response = WxHelper::accessapi($url,'POST',$xml, 1, $this->app_id, $this->secret);
/**
* Class WHelper
* @description 一些基礎微信用ifc
*/
class WHelper extends CComponent{
//獲取token
public static function getAccessToken($update = 0, $appid='', $secret='')
{
if (!$appid){
$appid = WAPP_ID;
}
if (!$secret){
$secret = APP_SECRET;
}
$time = time();
$token_file = COMMON_FOLDER.'/logs/'.$appid.'.token';
if(is_file($token_file)){
$token = file_get_contents($token_file);
$obj = json_decode($token);
if ($obj->expires_in < $time || $update==1) {
$obj=self::getToken1($time, $appid, $secret);
if(!$obj){return false;}
file_put_contents($token_file, json_encode($obj));
}
}else{
$obj=self::getToken1($time, $appid, $secret);
if(!$obj){return false;}
file_put_contents($token_file, json_encode($obj),777);
}
return $obj->access_token;
}
private static function getToken1($time, $appid, $secret){
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
$obj=self::accessapi($url,'POST', '', 1, $appid, $secret);
if (!isset($obj->expires_in)) {
Yii::log( "getToken_error". print_r($obj,1), CLogger::LEVEL_ERROR ,'log_error'); //異常時,日誌記錄。
return false;
}
$obj->expires_in = $time + $obj->expires_in - 30;
return $obj;
}
static function accessapi($url,$method='POST',$jsonData='',$repeat=1, $appid='', $secret=''){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Errno:' . curl_error($ch);
exit;
}
curl_close($ch);
$obj=json_decode($tmpInfo);
if(!is_object($obj)){
return $tmpInfo;
}
if(isset($obj->errcode)){
if($obj->errcode == 40001 && $repeat==1){
$arr=parse_url($url);
$arr1=explode('&',$arr['query']);
$query='';
if(count($arr1)>0 && $arr['query']){
$i=0;
foreach($arr1 as $arr2){
$arr3=explode("=",$arr2);
if($arr3[0]=='access_token'){
$token=self::getAccessToken(1, $appid, $secret);
$arr2=$arr3[0].'='.$token;
}
if($i>0){
$query.='&'.$arr2;
}else{
$query.=$arr2;
}
$i++;
}
}
$url=$arr['scheme'].'://'.$arr['host'].$arr['path'].'?'.$query;
return self::accessapi($url,$method,$jsonData,0);
}
}
return $obj;
}
}
業務處理類。
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/6/6
* Time: 15:06
*/
class WHandle extends CComponent{
public $app_id;
public $secret;
public $access_token;
/**
* @param $app_id
* @param $secret
* @description 構建函數,創建handle 時,同時拉取access_token
*/
public function __construct($app_id , $secret){ //
$this->app_id = $app_id;
$this->secret = $secret;
$this->access_token = WHelper::getAccessToken(0 , $this->app_id , $this->secret );
}
/**
* @return string json 字符串
*/
public function getMenu(){ //
$token = $this->access_token;
$url = "https://sz.api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $token;
$obj = WxHelper::accessapi($url,'GET','', 1, $this->app_id, $this->secret);
return $obj;
// return json_encode($obj);
}
/**
* @param $json
* @return string json 字符串
*/
public function setMenu( $json){
$token = $this->access_token;
$url = "https://sz.api.weixin.qq.com/cgi-bin/menu/create?access_token=" .$token;
return WxHelper::accessapi($url,'POST',$json, 1, $this->app_id, $this->secret);
}
}
調用示例: 獲取菜單數據
//獲取菜單配置。
$app_id = "xxx";
$app_secret = "yyy";
$handle = new WHandle($app_id , $app_secret);
$menus = $handle->getMenu(); //返回一個json 對象。
print_r( $menus );x結
調用示例: 設置菜單數據
//獲取菜單配置。 $app_id = "xxx"; $app_secret = "yyy"; $handle = new WHandle($app_id , $app_secret);$data =array("button"=> array( array( "type"=>"click" , "name" => "事件1" , "key" => "123456" ), array( "type"=>"click" , "name" => "事件2" , "key" => "123456" ), array( "name"=>"最新活動", "sub_button" => array( array("type"=> "view" , "name"=>"百度" , "url"=> "http://www.baidu.com"), array("type"=> "view" , "name"=>"鳳凰網" , "url"=> "http://ifeng.com"), array("type"=> "view" , "name"=>"qq" , "url"=> "http://www.qq.com"), array("type"=> "view" , "name"=>"網易" , "url"=> "http://163.com"), ) ) )); // echo json_encode( $data , JSON_UNESCAPED_UNICODE ); $r = $handle->setMenu(json_encode($data , JSON_UNESCAPED_UNICODE)); pr( $r );
得到的返回。
這樣,基礎的讀和寫就ok了,存入數據庫中,再做個crud 就可以包裝爲服務供運營小夥伴使用了。