PHP WeChat public Platform for the development of micro-message mass information sample sharing

Source: Internet
Author: User
Tags cdata openid
This article is mainly for you to introduce the PHP public platform for the development of mass information, with a certain reference value, interested in small partners can refer to

1. Purpose

Complete the mass message in the public number. This is just a simple text send. can also send voice pictures, etc., just send data format is different, there are links below, you can query data type data transmission format.

2, the flow of mass SMS

Access to test public accounts (with account can not test account, but formal account restrictions more) Users concerned about the above public accounts through AppID and Appsecret get our access_token through Access_token mass SMS

3. Get the test public account + follow the public number

1), Public test account access

To access the above connection, select "Interface test number request" to get directly open http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/ Index login via client scan.

Once you are logged in, you will get a test public account. There are mainly AppID and Appsecret two parameters, which will uniquely mark a public number and need to be used as a parameter to get the user's information. 、

2), configure interface information

This step can refer to the access instructions, the page provides a PHP instance download, it is simple to basically modify the custom token, and then put the verification page on its own server.

Here I provide an example of what I do:

Prepare resources:

Domain + SPACE (my is SAE space + million domain name), only validated PHP files

Domain name point to the spatial root I created a wx_sample.php

wx_sample.php

<?php/*** WeChat php Test*///define your Tokendefine ("TOKEN", "Weixin"); $wechatObj = new Wechatcallbackapitest (); $ Wechatobj->valid (); class Wechatcallbackapitest{public function valid () {$echoStr = $_get["Echostr"];//valid Signature, optionif ($this->checksignature ()) {echo $echoStr; exit;}} Public Function responsemsg () {//get post data, May is due to the different environments$poststr = $GLOBALS ["Http_raw_post_ DATA "];//extract Post Dataif (!empty ($POSTSTR)) {/* Libxml_disable_entity_loader is to prevent XML eXternal entity Injecti On,the best-by-check, validity of XML by Yourself */libxml_disable_entity_loader (true); $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 () {//must define token by YOURSELFIF (!defined ("token")) {throw new Exception (' token I s not defined! ');} $signature = $_get["signature"]; $timestamp = $_get["timestamp"]; $nonce = $_get["nonce"]; $token = token; $tmpArr = Array ($ Token, $timestamp, $nonce);//Use sort_string Rulesort ($TMPARR, sort_string); $tmpStr = implode ($TMPARR); $tmpStr = SHA1 ( $TMPSTR); if ($tmpStr = = $signature) {return true;} Else{return false;}}? >

Then fill in the configuration information token (must be consistent with the token in the above wx_sample.php), URL (wx_sample.php address)

And then you can submit it.

If the prompt fails, please check the token and URL "if it is your own domain name and space, please record, Baidu SAE, Sina SAE needs their own application and certification through (that is, a hand-held documents to upload, very simple, the shortest 2 days o), this step must be"

3) Configure the JS interface secure domain name

Fill this domain name is must not bring protocol, for example http://www.sagosoft.com/this is wrong, this is the URL is not the domain name

The domain name should be similar to www.sagosoft.com such as "otherwise in JS-SDK access will prompt invalid URL domain"

4) Follow the public number

The user only has to pay attention to the public number, in order to open the public Information link to authorize third-party login, and access to user information operations. So we also need to use our attention number, the operation is as follows:

Or just the login success after the jump page, we can see that the page has a QR code, we can scan the QR code to pay attention to the success in the right "user list" will be more than one user's information. As shown in the following:

5) Configuring callback functions

When we visit the Third Party Web page (ie our own webpage), we can use the website authorization mechanism, we not only have to have the AppID and appsecret that we have obtained before, but also need to have the callback domain setting after the user authorizes, that is, after the user authorizes, the page jumps to where. The specific configuration is as follows:

Or in just the page, there is a "page authorized to get user basic information", click on the following changes

Fill in the callback domain name:

Domain name is configured above the root domain name, if you above "interface configuration information" Fill in the URL is zcr.sinaaappc.com/wx_sample.php, here as long as the zcr.sinaaappc.com can be filled.

If your URL is not blacklisted, it will appear at the top of the

Attention:

1, here is the domain name (is a string), rather than the URL, so do not add http://and other protocol headers;
2, the authorization callback domain name configuration specification is the whole domain name, for example needs the domain name which the webpage authorizes: www.qq.com, configures after this domain name below the page http://www.qq.com/music.html, http://www.qq.com/login.html can conduct OAuth2.0 authentication. But http://pay.qq.com, http://music.qq.com and http://qq.com cannot conduct OAuth2.0 authentication.

Here, we have completed the public number of test account acquisition and configuration, the user has been concerned about the public number.

4. Get our Access_token through AppID and Appsecret

Access_token is the public number's globally unique ticket, and the public number needs to use Access_token when invoking each interface. Developers need to be properly saved. The Access_token store retains at least 512 character space. The Access_token is currently valid for 2 hours and needs to be refreshed periodically, and repeated acquisition will result in the Access_token failure of the last acquisition.

Get method:

HTTP request mode: GET
Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

Parameter description:


Return description

Normally, the following JSON packets are returned to the public number:

Copy the Code code as follows:

{"Access_token": "Access_token", "expires_in": 7200}

Error code, such as error codes, the JSON packet example is as follows (the example is AppID invalid error):


Copy the Code code as follows:

{"Errcode": 40013, "errmsg": "Invalid AppID"}

Example:

Get Access_token:

Copy the Code code as follows:

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4d1cb8dbd827a16e9&secret= d462d4c36b116795d1d99dcf0547af5443d

Return Data:


{"Access_token": "qr5uk2vmf5athv8e-ub10fzw0catzm_1kbkue4opk2ilvvnaoa7plzywqluamx6sjq1e7pkhrvatug0_ 1mpkqmdfokm2750kalwnk59ds-idopjjxompjtxa3whbn5fkrwnhadavar "," expires_in ": 7200}

5, through the Access_token mass SMS

On the public platform Web site, the subscription number is provided with a daily mass permission for the service number to provide monthly (natural month) 4 of the mass permissions. For some of the development capacity of the public number of operators, through the advanced mass interface, to achieve more flexible mass capacity.

Please note:

1, for the authentication subscription number, the mass interface can be successfully called 1 times a day, the mass can choose to send to all users or a group;
2, for authentication Service number Although the developer uses the advanced Mass interface daily call limit to 100 times, but the user can only receive 4, no matter in the public platform website, or use the interface mass, the user can only receive 4 mass message per month, more than 4 mass will send to the user failure;
3, with the payment permission of the public number, in the use of mass interface upload, mass text message type, you can use the <a> tag to join the outside chain;
4, the developer can use the preview interface to proofread the message style and layout, through the preview interface can send the edited message to the specified user verification effect.

1) Group-based mass "subscription number and service number authentication will be available"

Calling interface:

Copy the Code code as follows:

HTTP request method: Posthttps://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=access_token

In the body to add the following data (in JSON format data sent)-send other format data, only need to change the parameters in the information, you can view the official documents:


{"Filter": {"Is_to_all": false, "group_id": 2}, "text": {"content": "Content"}, "Msgtype": "Text"}

Parameter description:

Example: Send to everyone

Url:

Copy the Code code as follows:

https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token= Kbononajz4-khafqvjoq6vbx0f-bls7nasjbn8fy7glwav4be1lrjcob1rhh6ww35ixxfwkjnzfnc-on9eqitg3oxewuw7o2yyvw9nadknu6pqx9fnsmqcr8o Jtk8ug-hdtcaaabxn

Sent JSON data: Send to everyone


{"Filter": {"Is_to_all": true}, "text": {"content": "Content"}, "Msgtype": "Text"}

Return Data:

{"Errcode": 0, "errmsg": "Send job Submission Success", "msg_id": 1000000003}

Parameter meaning:

Error codes and their queries:

Global error code parsing

Use the postman analog HTTPS request to send as shown:

2) According to the list of OpenID group "subscription number is not available, service number is certified"

HTTP request URL Sent: (Note: different from above)


Copy the Code code as follows:

HTTP request method: Posthttps://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=access_token

Data format:


Copy the Code code as follows:

{"Touser": ["OPENID1", "OPENID2"], "Msgtype": "Text", "text": {"content": "Hello from Boxer."}}

Where OPENID1 and OPENID2 are the user OpenID (the user's only logo) we want to send.

Example:

Send "Of3pcsnsrmijzewalzzbafwqpxci","OF3PCSHH1CUIHR_WYAU6SWUIPZLW" two users.

The content is: Hello from boxer.<a href= ' www.baidu.com ' > Welcome to Baidu </a>

Url:


Copy the Code code as follows:

https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token= wrytbnsiu18ssehmplf4bdfet-bt6e6tgr4cqgvlbiprcyjpkdakpyfm6-qkkuhun8urkjh6xvm0ouadfgqoo8ru8hodxl-cgc9bh-ezjb2zucjsnqk2s416z I8kbeofogydafarjb

JSON data:


{"Touser": ["Of3pcsnsrmijzewalzzbafwqpxci", "OF3PCSHH1CUIHR_WYAU6SWUIPZLW"], "Msgtype": "Text", "text": {"Content": " Hello from Boxer.<a href= ' http://www.seewoedu.com/' > Welcome Shiv Vilas College </a> "}}


Return Data:


{"Errcode": 0, "errmsg": "Send job Submission Success", "msg_id": 3147483654}


Use the Postman impersonation to send the request as follows:

Number of items Received:

Thanks: Thank you for reading!

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.