您好,欢迎访问一九零五行业门户网

PHP实现微信公众账号开发

1、首先需要一个可以外网访问的接口url。
我这里是申请的新浪免费云服务器,http://xxxxx.applinzi.com/wx.php,具体自己可以去新浪云中心申请地址为:http://www.sinacloud.com
2、去微信公众账号申请个人的公众账号,地址为:https://mp.weixin.qq.com,然后进入到左侧菜单开发里面选择基本配置。
3、点击修改配置,填写必要的信息,认证如下图:
需要在wx.php里面写入认证代码,具体代码下文会提供。
4、等待认证成功后,启用配置
附详细代码:
1 valid(); 6 7 class wechatcallbackapitest { 8 public function valid() 9 { 10
//这里是认证过程
 $echostr = $_get[echostr]; 11 if($this->checksignature() && $echostr) { 12 echo $echostr; 13 exit; 14 } else { 15 $this->responsemsg();//当认证成功后执行下面的代码 16 } 17 } 18 19 private function responsemsg() { 20 21 $poststr = $globals[http_raw_post_data]; 22 if (!empty($poststr)) { 23 libxml_disable_entity_loader(true); 24 $texttpl = 25 26 27 %s 28 29 30 0 31 ; 32 $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); 33 //第一次关注的时候 34 if (strtolower($postobj->msgtype) == 'event') { 35 if (strtolower($postobj->event) == 'subscribe') { 36 $tousername = $postobj->tousername; 37 $fromusername = $postobj->fromusername; 38 $time = time(); 39 $msgtype = text; 40 $contentstr = ; 41 $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr); 42 echo $resultstr; 43 } 44 //关键字文本回复 45 } else if ( strtolower($postobj->msgtype) == 'text' ) { 46 if ( $postobj->content == 'imooc' ) { 47 $tousername = $postobj->tousername; 48 $fromusername = $postobj->fromusername; 49 $time = time(); 50 $msgtype = text; 51 $contentstr = imooc is very good; 52 $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr); 53 echo $resultstr; 54 exit; 55 } else if ( $postobj->content == '美丽说' ) { 56 $array = array( 57 array( 58 'title' => 'meilishuo', 59 'description'=> 'meilishuo is very good', 60 'picurl' => 'http://d05.res.meilishuo.net/img/_o/67/24/65bc4ebfe22d0c2eca1702c9736c_117_43.ch.png', 61 'url' => 'http://www.meilishuo.com' 62 ) 63 ); 64 $imagetpl = 65 66 67 %s 68 69 .count($array). 70 ; 71 foreach ($array as $key => $value) { 72 $imagetpl .= 73 74 75 76 77 78 ; 79 } 80 $imagetpl .= ; 81 $tousername = $postobj->tousername; 82 $fromusername = $postobj->fromusername; 83 $time = time(); 84 $msgtype = news; 85 $resultstr = sprintf($imagetpl, $fromusername, $tousername, $time, $msgtype); 86 echo $resultstr; 87 exit; 88 } 89 } 90 91 } else { 92 echo ; 93 exit; 94 } 95 96 } 97 98 private function checksignature() { 99 if (!defined(token)) {100 throw new exception('token is not defined!');101 }102 103 $signature = $_get[signature];104 $timestamp = $_get[timestamp];105 $nonce = $_get[nonce]; 106 $token = token;107 $tmparr = array($token, $timestamp, $nonce);108 sort($tmparr, sort_string);109 $tmpstr = implode( $tmparr );110 $tmpstr = sha1( $tmpstr );111 return ($tmpstr == $signature) ? true : false;112 }113 }
其它类似信息

推荐信息