SpeedPHP微信接口扩展
这个扩展实现了SP和微信公众平台的对接,1.0版暂时只实现了最简单的功能:绑定,收信息,回复信息。
扩展配置方法:
$spConfig = array(
'mode' => 'debug'
//扩展配置
, 'ext' => array(
//微信扩展设置
'spWeiXin' => array(
'TOKEN' => 'weixin' //微信通信密钥,后台设置
)
)
);然后在controll的方法里面执行:
$wx = spClass('spWeiXin');
$msg = $wx->run();就行了,如果是绑定那么会直接输出微信指定显示的echostr,程序中止执行。如果是收到信息,那么$msg就是收到的信息的数组,键名对照微信官方文档就成。
/**
* SpeedPHP微信接口扩展
*
2013年8月15日 08:51:23
* @author Lee(leeldy[AT]163.com)
* @version 1.0
*/
class spWeiXin {
//微信通信密钥
private $token;
//微信公众帐号ID
private $publicuserid;
//信息接受者
private $touserid;
public function __construct() {
$params = spExt('spWeiXin');
//获取配置参数
if (empty($params['TOKEN'])) {
spError('微信公众帐号通信密钥未设置!');
}
$this->token = $params['TOKEN'];
}
/**
* 绑定微信接口
* @return stringfalse
*/
public function bind() {
//随机字符串
$echoStr = $_GET["echostr"];
//微信加密签名
$signature = $_GET["signature"];
//签名时间戳
$timestamp = $_GET["timestamp"];
//加密随机数
$nonce = $_GET["nonce"];
/*
* 加密/校验流程:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
*/
$tmpArr = array($this->token, $timestamp, $nonce);
sort($tmpArr);
$_signature = sha1(implode($tmpArr));
if ($_signature != $signature) {
//签名不正确,返回false
$echoStr = false;
}
return $echoStr;
}
/**
* 接收消息,返回消息数组
* @return array
*/
public function receive() {
$raw = $this->php_fix_raw_query();
$msg = false;
if (!empty($raw)) {
$msg = (array) simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);
$this->publicuserid = $msg['ToUserName'];
$this->touserid = $msg['FromUserName'];
}
return $msg;
}
/**
* 获取 POST 提交的原始数据
* @author robotreply at gmail dot com (24-Jul-2009 08:17)
* @return string
*/
private function php_fix_raw_query() {
// Try globals array
//试图从全局变量中获取
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {
return $GLOBALS["HTTP_RAW_POST_DATA"];
}
// Try globals variable
//试图从全局变量中获取
if (isset($HTTP_RAW_POST_DATA)) {
return $HTTP_RAW_POST_DATA;
}
$post = '';
// Try stream
//试图从流中获取
if (!function_exists('file_get_contents')) {
//php://input is not available with enctype="multipart/form-data".
$fp = fopen("php://input", "r");
if ($fp) {
while (!feof($fp)) {
$post = fread($fp, 1024);
}
fclose($fp);
}
} else {
$post = file_get_contents("php://input");
}
return $post;
}
/**
* 回复文本消息
*
对于每一个POST请求,开发者在响应包中返回特定xml结构
*
对该消息进行响应(现支持回复文本、图文、语音、视频、音乐和对收到的消息进行星标操作)
*
微信服务器在五秒内收不到响应会断掉连接。
* @param string $Content 回复的消息内容
* @return stringfalse
*/
public function replyText($Content) {
$msg = false;
if (!empty($Content)) {
//CreateTime 消息创建时间
$CreateTime = time();
$msg =
touserid}]]>
publicuserid}]]>
{$CreateTime}
XML;
}
return $msg;
}
/**
* 回复音乐消息
* @param string $Title 标题
* @param string $Description 描述
* @param string $MusicUrl 音乐链接
* @param string $HQMusicUrl 高质量音乐链接,WIFI环境优先使用该链接播放音乐
* @return stringfalse
*/
public function replyMusic($Title, $Description, $MusicUrl, $HQMusicUrl) {
$msg = false;
if (!empty($MusicUrl)) {
//CreateTime 消息创建时间
$CreateTime = time();
$msg =
touserid}]]>
publicuserid}]]>
{$CreateTime}
XML;
}
return $msg;
}
/**
* 回复图文消息
* @param type $Articles 文章列表 array(array(Title,PicUrl,Url))
* @return stringfalse
*/
public function replyNews($Articles) {
$msg = false;
$articlesStr = '';
//图文消息个数,限制为10条以内
$ArticleCount = 0;
foreach ($Articles as $_art) {
if (!empty($_art['Title']) && !empty($_art['PicUrl']) && !empty($_art['Url'])) {
$ArticleCount++;
$articlesStr .= "
";
}
}
if (!empty($ArticleCount)) {
//CreateTime 消息创建时间
$CreateTime = time();
$msg =
touserid}]]>
publicuserid}]]>
{$CreateTime}
{$ArticleCount}
{$articlesStr}
XML;
}
return $msg;
}
/**
* 运行
* @return type
*/
function run() {
//微信服务器每次请求都会将signature,timestamp,nonce三个参数GET到接口
//只能通过是否存在echostr来判断是否是接口绑定动作
if (isset($_GET['echostr'])) {
//绑定
exit($this->bind());
} else {
//收到信息
return $this->receive();
}
}
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety
