_php Example of micro-letter mass information for the development of PHP micro-credit public platform

Source: Internet
Author: User
Tags cdata openid

1. Purpose

Complete the mass message in the micro-credit public number. Here just completes the simple text to send. can also send voice pictures and so on, just send the data format is different, there are links below, you can query the data type data transmission format.

2, the flow of mass SMS

Get the test public account (with the account can not test the account, but the official account limit more) Users pay attention to the above public accounts through AppID and Appsecret to get our access_token through Access_token mass SMS

3, to obtain the test public account number + public attention

1), the 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 through the micro-letter client Scan code login can be logged in.

Log in to obtain a test public account information. There are two parameters for AppID and Appsecret, which will uniquely mark a public number and need to take them as parameters to get the user's information. 、

2), Configuration interface information

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

Here I provide an example of what I do:

Prepare resources:

Domain + space (my SAE space + million network domain name), only for verification of PHP files

Domain-Directed space root directory I created a wx_sample.php.

wx_sample.php

<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
$WECHATOBJ = new Wechatcallbackapitest ();

$WECHATOBJ->valid ();

Class Wechatcallbackapitest {public function valid () {$echoStr = $_get["Echostr"];

Valid signature, option if ($this->checksignature ()) {echo $echoStr; exit;}} Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GLOBALS ["Http_raw_po

St_data "]; Extract post Data if (!empty ($POSTSTR)) {/* Libxml_disable_entity_loader is to prevent XML eXternal entity injection, TH
E best way is to check the 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, $from
Username, $toUsername, $time, $msgType, $CONTENTSTR);
Echo $resultStr; }else{echo "Input something ...";}}
else {echo "";
Exit The Private Function checksignature () {//You must define TOKEN by yourself if (!defined ("TOKEN")) {throw new Exception
(' TOKEN is not defined! ');
} $signature = $_get["signature"];
$timestamp = $_get["timestamp"];

$nonce = $_get["nonce"];
$token = token;
$TMPARR = Array ($token, $timestamp, $nonce);
Use sort_string the rule SORT ($TMPARR, sort_string);
$TMPSTR = implode ($TMPARR);

$TMPSTR = SHA1 ($TMPSTR); if ($tmpStr = = $signature) {return true;}

else{return false;}
 }}?>

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

And then you can submit it.

If prompted to fail, please check the token and URL "if it is their own domain name and space, please record;" Baidu SAE, Sina SAE need to apply for their own and certification through (that is, take a hand with a document upload, very simple the shortest 2 days on the O), this step must be "

3 Configure JS interface secure domain name

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

Domain name should be similar to www.sagosoft.com such "otherwise in the micro-letter Js-sdk access will prompt invalid URL domain"

4) attention to the public number

Users only pay attention to this public number, can be opened by the public Information link to authorize a third party to log in, and get the user information operation. Therefore, we also need to use our micro-letter attention to the micro-signal, the operation is as follows:

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

5) Configure callback functions

When we visit a third party Web page (that is, our own web page) on a micro-trust client, we can through the micro-letter web licensing mechanism, we have to not only have the previous acquisition of AppID and Appsecret also need to have when the user authorization, callback domain name settings, that is, after the user authorization, the page will jump to where. The specific configuration is as follows:

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

Fill in the domain name of the callback:

Domain name is the root domain name configured above, if you above "interface configuration information" Fill in the URL for 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.

Attention:

1, here is the domain name (is a string), rather than the URL, so do not add http://and other protocol headers;
2, Authorization callback domain name configuration specification for the full domain name, such as the need for Web page authorized domain name: www.qq.com, after the configuration of this domain Name page http://www.qq.com/music.html, http://www.qq.com/login.html Can carry out OAuth2.0 authentication. But http://pay.qq.com, http://music.qq.com, http://qq.com can't carry out OAuth2.0 authentication.

Here, we completed the public number test account acquisition and configuration, the user has been concerned about the micro-credit public number.

4, through AppID and Appsecret to obtain our Access_token

Access_token is the public number's globally unique ticket, the public number calls each interface to use the Access_token. Developers need to be properly saved. Access_token storage should be reserved for at least 512 character spaces. The validity period of the Access_token is currently 2 hours and needs to be refreshed periodically, and repeated acquisition will cause the last acquired access_token to fail.

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 micro-letter will return the following JSON packet to the public number:

Copy Code code as follows:
{"Access_token": "Access_token", "expires_in": 7200}

In the case of an error, the micro-letter returns information such as the error code, and the JSON packet example is as follows (the example is an AppID invalid error):

Copy Code code as follows:
{"Errcode": 40013, "errmsg": "Invalid AppID"}

Example:

Get Access_token:

Copy 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 Access_token mass message

On the public platform Web site, the subscription number provides a daily one of the mass permissions, for the service number of the monthly (natural month) 4 of mass permissions. And for some people with the ability to develop the public number 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 the certification service number Although developers use the high-level mass interface of the daily call limit to 100 times, but users can only receive 4 a month, whether in the public platform site, or the use of interface group, users can only receive 4 mass messages per month, more than 4 of the mass will send to the user failed;
3, with micro-credit to pay the authority of the public number, in the use of mass interface upload, mass message type, you can use the <a> tag to add outside the chain;
4, developers can use the Preview interface proofing message style and layout, through the preview interface can send edited messages to the specified user calibration effect.

1 according to the group for mass "subscription number and service number after authentication are available"

Call Interface:

Copy Code code as follows:
HTTP request mode: Posthttps://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=access_token

In the body add the following data (in JSON format data sent)-send other format data, only need to change the parameters in the information can be specific to see the micro-letter official documents:

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

Parameter description:

Example: Send to everyone

Url:

Copy 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: sent 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 resolution

Use the postman analog HTTPS request to send the following image:

2 According to the OpenID list mass "The subscription number is not available, the service number is certified after the"

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

Copy Code code as follows:
HTTP request mode: Posthttps://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=access_token

Data format:

Copy Code code as follows:
{"Touser": ["OPENID1", "OPENID2"], "Msgtype": "Text", "text": {"content": "Hello from Boxer."}}

Where OPENID1 and OPENID2 are the micro-mail users we want to send OpenID (the user's only indication).

Example:

Send "Of3pcsnsrmijzewalzzbafwqpxci","OF3PCSHH1CUIHR_WYAU6SWUIPZLW" two users.

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

Url:

Copy 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 to Cirvik College </a> "}
}

Return Data:

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

Use postman to simulate sending requests as follows:

What the micro-signal receives:

Thanks: Thank you for your reading!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.