WeChat sharing configuration and examples
WeChat public platform server configuration
The following is the code of wx_check.php, put this file in the newly created [wx_check] folder under your server domain name.
<?php
define("TOKEN", "你的token(任意字符串,与服务器上相同)");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
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);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
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;
}
}
}
?>
I found a strange phenomenon that after adding the wx_ prefix to the folder and file name, I passed the verification in seconds, so I named the name this wx_xxx here
WeChat public account sharing development
development process
Download the WeChat sample
which contains
the sample code on sample.php.
Open sample.php and delete the html code, and
the last step above is to separate the code from the data
, then change the name of the sample to a very powerful name, and change the parent folder [PHP] as well. Note, it is best to use English,
put it into the root directory of the server domain name folder
, create a new test project,
share.png is the picture displayed when sharing, preferably 200*200 pixel size
testshare.php is the code
and then in your code write like this
<?php
require_once "../牛逼名字/牛逼名字.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
TEST share
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
/*
* 注意:
* 1. 所有的JS接口只能在公众号绑定的域名下调用,公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
* 2. 如果发现在 Android 不能分享自定义内容,请到官网下载最新的包覆盖安装,Android 自定义分享接口需升级至 6.0.2.58 版本及以上。
* 3. 常见问题及完整 JS-SDK 文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
*
* 开发中遇到问题详见文档“附录5-常见错误及解决办法”解决,如仍未能解决可通过以下渠道反馈:
* 邮箱地址:[email protected]
* 邮件主题:【微信JS-SDK反馈】具体问题
* 邮件内容说明:用简明的语言描述问题所在,并交代清楚遇到该问题的场景,可附上截屏图片,微信团队会尽快处理你的反馈。
*/
wx.config({
debug: true,
appId: '<?php echo $signPackage["appId"];?>',
timestamp: <?php echo $signPackage["timestamp"];?>,
nonceStr: '<?php echo $signPackage["nonceStr"];?>',
signature: '<?php echo $signPackage["signature"];?>',
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage'
]
});
//当前链接地址
var gameUrl = 'http://你的域名/testshare/testshare.html';
var imgUrl = 'http://你的域名/testshare/share.png';
//分享到朋友数据
var sharedata = {
title: '分享测试',
desc: '分享测试,分享测试,分享测试,分享测试,分享测试,分享测试',
link: gameUrl,
imgUrl: imgUrl
};
//分享到朋友圈数据 默认和分享朋友一样
var timelinedata = {
title: "分享测试",
link: sharedata.link,
imgUrl: sharedata.imgUrl
};
wx.ready(function () {
//分享朋友圈
wx.onMenuShareTimeline(timelinedata);
//分享给朋友
wx.onMenuShareAppMessage(sharedata);
});
</script>
</body>
</html>
Put it into the root directory of the server domain name.
The browser accesses this file, and then sends the link to WeChat. Open WeChat and click on the upper left corner to share with friends and share with Moments
error resolution
error 1 url domain
During the development process, you will encounter situations that cannot be shared, and an error will be reported
errmsg config;invalid url domain
This is because the js security domain name is not configured.
Solution
- Open the WeChat public platform
- Find [Settings]
- Find [Function Settings] - "[js interface security domain name] - "[Settings]
- keep
Error 2 40164 invalid ip xxx
errMsg:config :invalid signature
This is a prompt for an illegal signature. click debug
Solution
Enter appid and secret. See if it prompts this question. If so, you need to add the prompted ip address to the WeChat whitelist
(strangely, I have been using it well before, and then the server environment changed from laragon to phpstudy and this problem occurred)
Error 3 The get method cannot get res
The get method of the official sample.php cannot get res.
Solution
As shown in the picture, change the verification to no verification (there is a problem to be verified, that is, after changing true to false, does the white list not need to be set?)
error 4 file not found
When opening the link, it still prompts illegal signature
errMsg:config :invalid signature
Solution
Check the code of sample.php
to see if the url shown in the figure above is written correctly.
The url commented out above will not automatically generate the two files in the above picture when called. It needs to be copied to the project project. and cannot be empty. Must have valid content.
There are two ways to obtain the valid content.
1. Use the WeChat online debugging tool to obtain the corresponding value
. 2. Use the url shown in the figure below. Without these two files, it can be automatically generated. In this way, there is a file.
Generally, I use the uncommented url in the picture above, which is convenient and smart.