标簽:
public function http_request_json($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function http_request_json_data($url,$post_data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
//http://suez.xyhs.xin/index.php/api/weixin/pipei
//$Weixin = new WeixinController(); //執行個體化類
//$Weixin->sendpipei($openid,$clientName,$tel,$product); //調用方法
public function pipei(){
$this->sendpipei("omPn_0godUbAbwPgpUEVN_X87Css","使用者主動比對","比對成功");
}
//獲得全局access_token
public function get_token(){
//如果已經存在直接傳回access_token
//if($_SESSION['access_token'] && $_SESSION['expire_time']>time()){
//return $_SESSION['access_token'];
//}else{
$appid = C('WX_APPID'); //appid
$appsecret = C('WX_APPSERECT'); //appid
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; //請求位址
$res=$this->http_request_json($url);
if( curl_errno($ch) ){
var_dump( curl_error($ch) ); //列印錯誤資訊
}
curl_close( $ch );
$arr = json_decode($res, true); //将結果轉為數組
//$_SESSION['access_token']=$arr['access_token']; //将access_token存入session中,可以不存,每次都獲得新的token
//$_SESSION['expire_time']=time()+7200;
return $arr['access_token'];
//}
}
//推送模闆資訊 參數:發送給誰的openid,客戶姓名,客戶電話,推薦樓盤(參數自定)
public function sendpipei($openid,$pipeileixing,$jindu) {
//擷取全局token
$token = $this->get_token();
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token; //模闆資訊請求位址
//發送的模闆資訊(微信要求json格式,這裡為數組(友善添加變量)格式,然後轉為json)
$post_data = array(
"touser"=>$openid, //推送給誰,openid
"template_id"=>"h7_UWH1okF8PoDneH8Q86cGpdLxlvkFAKQCM9ZhNFmo", //微信背景的模闆資訊id
"url"=>"http://www.xxxx.xin/index.php/jmobile/index/my", //點選之後跳轉頁面
"data"=> array(
"first" => array(
"value"=>"您的比對情況有了更新",
"color"=>"#173177"
),
"keyword1"=>array(
"value"=>$pipeileixing, //傳的變量 類型:客戶主動比對
"color"=>"#173177"
),
"keyword2"=>array(
"value"=>$jindu,//傳的變量 進度:比對成功
"color"=>"#173177"
),
"remark"=> array(
"value"=>"歡迎您及時确定您的更新資訊。",
"color"=>"#173177"
),
)
);
//将上面的數組資料轉為json格式
$post_data = json_encode($post_data);
//發送資料,post方式
$data=$this->http_request_json_data($url,$post_data);
$data = json_decode($data,true); //将json資料轉成數組
return $data;
}
标簽:
來源: https://www.cnblogs.com/thinkbig/p/10217188.html