php開發微信公眾號代碼封裝
注意,php版本不要大于7.x,不然*$postStr = $GLOBALS[“HTTP_RAW_POST_DATA”] ;*這句代碼會受影響;
<?php
define("token", "aaabbbccc");
define("appid", "假假,我愛過你!");
define("appsecret", "希望你過的更好!祝你幸福~~~");
// 注意php版本不要7.x以上
// 呼叫下面的方法
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$wx = new wechatCallbackapiTest();
if (isset($_GET['echostr'])){
$wx->valid();
}else if(!empty($postStr)){
$wx->responseMsg();
}
//類
class wechatCallbackapiTest
{
//判斷是微信服務器發來的還是用戶發來的并作出反應
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
ob_clean();
echo $echoStr;
exit;
}
}
//驗證訊息
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token=token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
//接收文本
public function responseMsg()
{
// $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
global $postStr;
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
//用戶發送的訊息型別判斷
switch ($RX_TYPE)
{
case "text": //文本訊息
$result = $this->receiveText($postObj);
break;
case "image": //圖片訊息
$result = $this->receiveImage($postObj);
break;
case "voice": //語音訊息
$result = $this->receiveVoice($postObj);
break;
case "video": //視頻訊息
$result = $this->receiveVideo($postObj);
break;
case "location"://位置訊息
$result = $this->receiveLocation($postObj);
break;
case "link": //鏈接訊息
$result = $this->receiveLink($postObj);
break;
default:
$result = "unknow msg type: ".$RX_TYPE;
break;
}
echo $result;
}else {
echo "";
exit;
}
}
/*
* 接收文本訊息
*/
private function receiveText($object)
{
$content = "你發送的是文本,內容為:".$object->Content;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收圖片訊息
*/
private function receiveImage($object)
{
$content = "你發送的是圖片,地址為:".$object->PicUrl;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收語音訊息
*/
private function receiveVoice($object)
{
$content = "你發送的是語音,媒體ID為:".$object->MediaId;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收視頻訊息
*/
private function receiveVideo($object)
{
$content = "你發送的是視頻,媒體ID為:".$object->MediaId;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收位置訊息
*/
private function receiveLocation($object)
{
$content = "你發送的是位置,緯度為:".$object->Location_X.";經度為:".$object->Location_Y.";縮放級別為:".$object->Scale.";位置為:".$object->Label;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收鏈接訊息
*/
private function receiveLink($object)
{
$content = "你發送的是鏈接,標題為:".$object->Title.";內容為:".$object->Description.";鏈接地址為:".$object->Url;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 回復文本訊息
*/
private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
public function post($Url,$postDataArr){
$postJosnData = json_encode($postDataArr);
$ch = curl_init($Url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
return $data;
}
public function get($url){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);//目標URL
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );//設定是否顯示頭資訊,1為顯示
curl_setopt($oCurl, CURLOPT_BINARYTRANSFER, true) ;//在啟用CURLOPT_RETURNTRANSFER時候將獲取資料回傳
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);//獲取頁面各種資訊
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
$sContent = json_decode($sContent, true);
// 轉成json陣列形式
return $sContent;
}else{
return false;
}
}
public function getToken($url){
$url=str_replace("APPID",appid,$url);
$url=str_replace("APPSECRET",appsecret,$url);
$data=$this->get($url);
return $data;
}
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/276699.html
標籤:其他
