PHP 微信接口封装类_PHP教程
程序员文章站
2024-01-22 08:19:04
...
weixin_run(); //执行接收器方法
class wechatCallbackapiTest
{
private $fromUsername;
private $toUsername;
private $times;
private $keyword;
public function weixin_run(){
$this->responseMsg();
$arr[]=array("很靠谱,不错!","aa","http://www.chuangen.com/pic/hj/s_4.png","http://www.baidu.com");
$arr[]=array("很靠谱,不错!","aa","http://www.chuangen.com/pic/hj/s_4.png","http://www.baidu.com");
$arr[]=array("很靠谱,不错!","aa","http://www.chuangen.com/pic/hj/s_4.png","http://www.baidu.com");
$this->fun_xml("news",$arr,array(2,0));
}
function t($n){
include("t_api.php");
$c_name=$t_api[$n];
$json=file_get_contents("http://m.weather.com.cn/data/101110310.html");
return json_decode($json);
}
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->fromUsername = $postObj->FromUserName;
$this->toUsername = $postObj->ToUserName;
$this->keyword = trim($postObj->Content);
$this->times = time();
}else {
echo "this a file for weixin API!";
exit;
}
}
//微信封装类,
//type: text 文本类型, news 图文类型
//text,array(内容),array(ID)
//news,array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID)
private function fun_xml($type,$value_arr,$o_arr=array(0)){
//=================xml header============
$con="fromUsername}]]> toUsername}]]> {$this->times} ";
//=================type content============
switch($type){
case "text" :
$con.="{$o_arr} ";
break;
case "news" :
$con.="{$o_arr[0]} ";
foreach($value_arr as $id=>$v){
if($id>=$o_arr[0]) break; else null; //判断数组数不超过设置数
$con.="
";
}
$con.=" {$o_arr[1]} ";
break;
} //end switch
//=================end return============
echo $con." ";
}
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;
}
}
}
?>
上一篇: 学习编程的七个阶段
推荐阅读
-
PHP 微信接口封装类_PHP教程
-
PHP微信开发之查询城市天气,php查询城市天气_PHP教程
-
PHP实现微信网页授权开发教程_PHP
-
PHP微信公众平台开发 - 消息回复的封装_PHP教程
-
微信订阅号开发之token验证后,自动回复消息功能做好,发送消息没有返回,token自动回复_PHP教程
-
微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应_PHP教程
-
oauth - php有没有可以支持微信登陆的独立user类库?
-
PHP使用Face++接口开发微信公众平台人脸识别系统的方法
-
开发微信支付的一点心得_PHP教程
-
PHP微信开发之根据用户回复关键词位置返回附近信息,php关键词_PHP教程