Home Backend Development PHP Tutorial [php development] Implement WeChat public account graphic message reply and access_token (code example)

[php development] Implement WeChat public account graphic message reply and access_token (code example)

Apr 25, 2019 pm 03:06 PM
php WeChat public account message reply

This article mainly talks about the implementation of WeChat public account graphic message reply and access_token in PHP. It has certain reference value and interested friends can learn about it.

 //代码如下
<?php
class IndexAction extends Action {
public function __construct(){

}
public function index(){
//获得参数 signature nonce token timestamp echostr
$nonce     = $_GET[&#39;nonce&#39;];
$token     = &#39;imooc&#39;;
$timestamp = $_GET[&#39;timestamp&#39;];
$echostr   = $_GET[&#39;echostr&#39;];
$signature = $_GET[&#39;signature&#39;];
//形成数组,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后与signature进行校验
$str = sha1( implode( $array ) );
if( $str  == $signature && $echostr ){
//第一次接入weixin api接口的时候
echo  $echostr;
exit;
}else{
$this->reponseMsg();
}
}
// 接收事件推送并回复
public function reponseMsg(){
//1.获取到微信推送过来post数据(xml格式)
$postArr = $GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;];
//2.处理消息类型,并设置回复类型和内容
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>*/
$postObj = simplexml_load_string( $postArr );
//$postObj->ToUserName = &#39;&#39;;
//$postObj->FromUserName = &#39;&#39;;
//$postObj->CreateTime = &#39;&#39;;
//$postObj->MsgType = &#39;&#39;;
//$postObj->Event = &#39;&#39;;
// gh_e79a177814ed
//判断该数据包是否是订阅的事件推送
if( strtolower( $postObj->MsgType) == &#39;event&#39;){
//如果是关注 subscribe 事件
if( strtolower($postObj->Event == &#39;subscribe&#39;) ){
//回复用户消息(纯文本格式)	
$toUser   = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time     = time();
$msgType  =  &#39;text&#39;;
$content  = &#39;欢迎关注我们的微信公众账号&#39;.$postObj->FromUserName.&#39;-&#39;.$postObj->ToUserName;
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
echo $info;
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>*/
}
}
//当微信用户发送imooc,公众账号回复‘imooc is very good&#39;
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>*/
/*if(strtolower($postObj->MsgType) == &#39;text&#39;){
switch( trim($postObj->Content) ){
case 1:
$content = &#39;您输入的数字是1&#39;;
break;
case 2:
$content = &#39;您输入的数字是2&#39;;
break;
case 3:
$content = &#39;您输入的数字是3&#39;;
break;
case 4:
$content = "<a href=&#39;http://www.imooc.com&#39;>慕课</a>";
break;
case &#39;英文&#39;:
$content = &#39;imooc is ok&#39;;
break;
}	
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
//注意模板中的中括号 不能少 也不能多
$fromUser = $postObj->ToUserName;
$toUser   = $postObj->FromUserName; 
$time     = time();
// $content  = &#39;18723180099&#39;;
$msgType  = &#39;text&#39;;
echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);

}
}
*/
//用户发送tuwen1关键字的时候,回复一个单图文
if( strtolower($postObj->MsgType) == &#39;text&#39; && trim($postObj->Content)==&#39;tuwen2&#39; ){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$arr = array(
array(
&#39;title&#39;=>&#39;imooc&#39;,
&#39;description&#39;=>"imooc is very cool",
&#39;picUrl&#39;=>&#39;http://www.imooc.com/static/img/common/logo.png&#39;,
&#39;url&#39;=>&#39;http://www.imooc.com&#39;,
),
array(
&#39;title&#39;=>&#39;hao123&#39;,
&#39;description&#39;=>"hao123 is very cool",
&#39;picUrl&#39;=>&#39;https://www.baidu.com/img/bdlogo.png&#39;,
&#39;url&#39;=>&#39;http://www.hao123.com&#39;,
),
array(
&#39;title&#39;=>&#39;qq&#39;,
&#39;description&#39;=>"qq is very cool",
&#39;picUrl&#39;=>&#39;http://www.imooc.com/static/img/common/logo.png&#39;,
&#39;url&#39;=>&#39;http://www.qq.com&#39;,
),
);
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";
foreach($arr as $k=>$v){
$template .="<item>
<Title><![CDATA[".$v[&#39;title&#39;]."]]></Title> 
<Description><![CDATA[".$v[&#39;description&#39;]."]]></Description>
<PicUrl><![CDATA[".$v[&#39;picUrl&#39;]."]]></PicUrl>
<Url><![CDATA[".$v[&#39;url&#39;]."]]></Url>
</item>";
}

$template .="</Articles>
</xml> ";
echo sprintf($template, $toUser, $fromUser, time(), &#39;news&#39;);
//注意:进行多图文发送时,子图文个数不能超过10个
}else{
switch( trim($postObj->Content) ){
case 1:
$content = &#39;您输入的数字是1&#39;;
break;
case 2:
$content = &#39;您输入的数字是2&#39;;
break;
case 3:
$content = &#39;您输入的数字是3&#39;;
break;
case 4:
$content = "<a href=&#39;http://www.imooc.com&#39;>慕课</a>";
break;
case &#39;英文&#39;:
$content = &#39;imooc is ok&#39;;
break;
}	
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
//注意模板中的中括号 不能少 也不能多
$fromUser = $postObj->ToUserName;
$toUser   = $postObj->FromUserName; 
$time     = time();
// $content  = &#39;18723180099&#39;;
$msgType  = &#39;text&#39;;
echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);

}//if end
}//reponseMsg end
function http_curl(){
//获取imooc
//1.初始化curl
$ch = curl_init();
$url = &#39;http://www.baidu.com&#39;;
//2.设置curl的参数
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//3.采集
$output = curl_exec($ch);
//4.关闭
curl_close($ch);
var_dump($output);
}
function getWxAccessToken(){
//1.请求url地址
$appid = &#39;wx08d5c2cb632bb5e4&#39;;
$appsecret =  &#39;06d3444fb9abd8d00314eb4a38ad61a8&#39;;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
//2初始化
$ch = curl_init();
//3.设置参数
curl_setopt($ch , CURLOPT_URL, $url);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
//4.调用接口 
$res = curl_exec($ch);
//5.关闭curl
curl_close( $ch );
if( curl_errno($ch) ){
var_dump( curl_error($ch) );
}
$arr = json_decode($res, true);
var_dump( $arr );
}
function getWxServerIp(){
$accessToken = "6vOlKOh7r5uWk_ZPCl3DS36NEK93VIH9Q9tacreuxJ5WzcVc235w_9zONy75NoO11gC9P0o4FBVxwvDiEtsdX6ZRFR0Lfs_ymkb8Bf6kRfo";
$url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$res = curl_exec($ch);
curl_close($ch);
if(curl_errno($ch)){
var_dump(curl_error($ch));
}
$arr = json_decode($res,true);
echo "<pre class="brush:php;toolbar:false">";
var_dump( $arr );
echo "
"; } }//class end
Copy after login

Related tutorials:

PHP video tutorial

WeChat applet video tutorial

The above is the detailed content of [php development] Implement WeChat public account graphic message reply and access_token (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles