微信公眾號開發之微信公用平台訊息回複類執行個體,公眾執行個體_PHP教程

來源:互聯網
上載者:User

公眾號開發之公用平台訊息回複類執行個體,公眾執行個體


本文執行個體講述了公眾號開發之公用平台訊息回複類。分享給大家供大家參考。具體如下:

公眾號開發代碼我在網上看到了有不少,其實都是大同小義了都是參考官方給出的demo檔案進行修改的,這裡就給各位分享一個。

複製代碼 代碼如下:
<?php
/**
* 公用平台訊息回複類
*
*
*/
class BBCweixin{

private $APPID="******";
private $APPSECRET="******";
/*
*簡訊回複
*@param array object
*@param string content
*@return string
*/
public function resText($object,$content,$flag=0){
$xmlText="
%s
%s
%s
text
%s
%d
";
$resultStr=sprintf($xmlText,$object->FromUserName,$object->ToUserName,time(),$content,$flag);
echo $resultStr;exit();
}
/*
*圖片訊息回複
*@param array object
*@param string url
*@return string
*/
public function resImage($object,$media_id){
$xmlImage="";
$xmlImage.="%s";
$xmlImage.="%s";
$xmlImage.="%s";
$xmlImage.="image";
$xmlImage.="%s";
$xmlImage.="";
$resultStr=sprintf($xmlImage,$object->FromUserName,$object->ToUserName,time(),$media_id);
echo $resultStr;exit();
}
/*
*圖文訊息回複
*@param array object
*@param array newsData 二維數組 必須包含[Title][Description][PicUrl][Url]欄位
*@return string
*/
public function resNews($object,$newsData=array()){
$CreateTime=time();
$FuncFlag=0;
$newTplHeader="
{$object->FromUserName}
{$object->ToUserName}
{$CreateTime}
news
%s
%s";
$newTplItem="
<![CDATA[%s]]>
%s
%s
%s
";
$newTplFoot="
%s
";
$Content='';
$itemsCount=count($newsData);
$itemsCount=$itemsCount<10?$itemsCount:10;//公眾平台圖文回複的訊息一次最多10條
if($itemsCount){
foreach($newsData as $key=>$item){
if($key<=9){
$Content.=sprintf($newTplItem,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
}
}
}
$header=sprintf($newTplHeader,0,$itemsCount);
$footer=sprintf($newTplFoot,$FuncFlag);
echo $header.$Content.$footer;exit();
}

/*
*音樂訊息回複
*@param array object
*@param array musicContent 二維數組 包含[Title][Description][MusicUrl][HQMusicUrl]欄位
*@return string
*/
public function resMusic($object,$musicContent=array()){
$xmlMusic="
%s
%s
%s
music

<![CDATA[%s]]>
%s
%s
%s

";
if(empty($musicContent[0]['HQMusicUrl'])){
$musicContent[0]['HQMusicUrl']=$musicContent[0]['MusicUrl'];
}
$resultStr=sprintf($xmlMusic,$object->FromUserName,$object->ToUserName,time(),$musicContent[0]['Title'],$musicContent[0]['Description'],$musicContent[0]['MusicUrl'],$musicContent[0]['HQMusicUrl']);
echo $resultStr;exit();
}
/*
*上傳多媒體檔案介面
*@param
*@param array mediaArr filename、filelength、content-type
*@return object
*/
public function uploadMedia($accessToken,$type='image',$mediaArr){
$url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$accessToken."&type=".$type;
$doPost=self::curlPost($mediaArr,$url);
return $doPost;
}
/*
*GPS,Google座標轉換成百度座標
*@param lnt
*@param lat
*@return array
*/
public function mapApi($lng,$lat,$type){
$map=array();
if($type=='gps'){
$url="http://map.yanue.net/gpsApi.php?lat=".$lat."&lng=".$lng;
$res=json_decode(file_get_contents($url));
$map['lng']=$res->baidu->lng;
$map['lat']=$res->baidu->lat;
}
if($type=='google'){
$url="http://api.map.baidu.com/ag/coord/convert?from=2&to=4&mode=1&x=".$lng."&y=".$lat;
$res=json_decode(file_get_contents($url));
$map['lng']=base64_decode($res[0]->x);
$map['lat']=base64_decode($res[0]->y);
}
return $map;
}

/**************************************************************
*
* 使用特定function對數組中所有元素做處理
* @param string &$array 要處理的字串
* @param string $function 要執行的函數
* @return boolean $apply_to_keys_also 是否也應用到key上
* @access public
*
*************************************************************/
public function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
self::arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
}

if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}

/**************************************************************
*
* 將數群組轉換為JSON字串(相容中文)
* @param array $array 要轉換的數組
* @return string 轉換得到的json字串
* @access public
*
*************************************************************/
public function JSON($array) {
self::arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
/*
*建立菜單
*
*/
public function creatMenu($shop_id,$data){
$jsonArray=self::JSON($data);
$AccessToken=self::accessToken($weiXin[0]['key'],$weiXin[0]['secret']);
$MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$AccessToken;
return self::curlPost($jsonArray,$MENU_URL);
}
/*
*客服訊息回複
*@param array jsonArray Array {"touser":"OPENID","msgtype":"text","text":{"content":"Hello World"}}
*@return string
*/

public function customService($jsonArray,$hash){
if(empty($jsonArray)){
return false;
}
$db=M();
$sql="select * from bbc_wechats where hash='".$hash."'";
$weChast=$db->query($sql);
$AccessToken=self::accessToken($weChast[0]['key'],$weChast[0]['secret']);
$TokenUrl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$AccessToken;
$CustomRes=self::curlPost($jsonArray,$TokenUrl);
return $CustomRes;
}
/*

*擷取access_token
*@return objectStr
*/
public function accessToken($appid,$secret){
$access_token=BBCcache::getCache('accesstoken'.$appid);
if($access_token){
$AccessTokenRet=$access_token;
}else{
$TookenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$AccessTokenRes=@file_get_contents($TookenUrl);
$AccessToken=json_decode($AccessTokenRes);
$AccessTokenRet=$AccessToken->access_token;
BBCcache::setCache('accesstoken'.$appid,$AccessToken->access_token,3600);
}
return $AccessTokenRet;
}
/*
*向遠程介面POST資料
*@data Array {"touser":"OPENID","msgtype":"text","text":{"content":"Hello World"}}
*@return objectArray
*/
public function curlPost($data,$url){
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
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, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$info = curl_exec($ch);

if (curl_errno($ch)) {
echo 'Errno'.curl_error($ch);
}

curl_close($ch);
return json_decode($info);
}
//根據經緯度計算距離和方向
function getRadian($d){
return $d * M_PI / 180;
}

function getDistance ($lat1, $lng1, $lat2, $lng2){
$EARTH_RADIUS=6378.137;//地球半徑
$lat1 =getRadian($lat1);
$lat2 = getRadian($lat2);

$a = $lat1 - $lat2;
$b = getRadian($lng1) - getRadian($lng2);

$v = 2 * asin(sqrt(pow(sin($a/2),2) + cos($lat1) * cos($lat2) * pow(sin($b/2),2)));

$v = round($EARTH_RADIUS * $v * 10000) / 10000;

return $v;
}
}
?>

希望本文所述對大家基於PHP的公眾號開發有所協助。

http://www.bkjia.com/PHPjc/911900.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/911900.htmlTechArticle公眾號開發之公用平台訊息回複類執行個體,公眾執行個體 本文執行個體講述了公眾號開發之公用平台訊息回複類。分享給大家供大家...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.