“; 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=”;
$doPost=self::curlPost; return $doPost;}/* *GPS,谷歌坐标转换成百度坐标
*@param lnt *@param lat *@return array */public function mapApi{
$map=array{
$url=”;
$res=json_decode(file_get_contents;
$map[‘lng’]=$res->baidu->lng;
$map[‘lat’]=$res->baidu->lat; } if{
$url=”;
$res=json_decode(file_get_contents; $map[‘lng’]=base64_decode;
$map[‘lat’]=base64_decode; } 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 { self::arrayRecursive($array[$key], $function,
$apply_to_keys_also); } else { $array[$key] = $function; } if
($apply_to_keys_微信公众号开发之微信公共平台消息回复类实例_php技巧_脚本之家。also && is_string { $new_key = $function; if {
$array[$new_key] = $array[$key]; unset; } } }
$recursive_counter–;}/**************************************************************
* * 将数组转换为JSON字符串 * @param array $array 要转换的数组 *
@return string 转换得到的json字符串 * @access public *
*************************************************************/public
function JSON { self::arrayRecursive($array, ‘urlencode’, true); $json =
json_encode; return urldecode;}/* *创建菜单 * */public function
creatMenu{ $jsonArray=self::JSON;
$AccessToken=self::accessToken($weiXin[0][‘key’],$weiXin[0][‘secret’]);
$MENU_URL=”;
return self::curlPost;}/* *客服消息回复 *@param array jsonArray Array
{“touser”:”OPENID”,”msgtype”:”text”,”text”:{“content”:”Hello World”}}
*@return string */ public function customService{ if{ return false; }
$db=M(); $sql=”select * from bbc_wechats where hash='”.$hash.”‘”;
$weChast=$db->query;
$AccessToken=self::accessToken($weChast[0][‘key’],$weChast[0][‘secret’]);
$TokenUrl=”;
$CustomRes=self::curlPost; return $CustomRes; } /* *获取access_token
*@return objectStr */ public function accessToken{
$access_token=BBCcache::getCache; if{ $AccessTokenRet=$access_token;
}else{
$TookenUrl=””;
$AccessTokenRes=@file_get_contents; $AccessToken=json_decode;
$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{ $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; if
{ echo ‘Errno’.curl_error; } curl_close; return json_decode;
}//根据经纬度计算距离和方向function getRadian{ return $d * M_PI /
180;}function getDistance ($lat1, $lng1, $lat2, $lng2){
$EARTH_RADIUS=6378.137;//地球半径 $lat1 =getRadian; $lat2 = getRadian;
$a = $lat1 – $lat2; $b = getRadian – getRadian; $v = 2 * asin + cos *
pow; $v = round($EARTH_RADIUS * $v * 10000) / 10000; return
$v;}}?>

微信公众号开发代码我在网上看到了有不少,其实都是大同小义了都是参考官方给出的demo文件进行修改的,这里就给各位分享一个。

本文实例讲述了微信公众号开发之微信公共平台消息回复类。分享给大家供大家参考。具体如下:

希望本文所述对大家基于PHP的微信公众号开发有所帮助。

“; $newTplFoot=” %s “; $Content=”; $itemsCount=count;
$itemsCount=$itemsCount<10?$itemsCount:10;//微信公众平台图文回复的消息一次最多10条
if{ foreach($newsData as $key=>$item){ if{
$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 %d “;
$resultStr=sprintf($xmlText,$object->FromUserName,$object->ToUserName,time;
echo $resultStr;exit();}/* *图片消息回复 *@param array object
*@param string url *@return string */public function resImage{
$xmlImage=””; $xmlImage.=””; $xmlImage.=””; $xmlImage.=”%s”;
$xmlImage.=””; $xmlImage.=””; $xmlImage.=””;
$resultStr=sprintf($xmlImage,$object->FromUserName,$object->ToUserName,time;
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=” FromUserName}]]> ToUserName}]]>
{$CreateTime} %s”; $newTplItem=”

发表评论

电子邮件地址不会被公开。 必填项已用*标注