This article mainly introduces php advanced interface calling methods, including custom menu interface, customer service interface, and QR code. it has some reference value. interested friends can refer to it.
How to call advanced interfaces
Differences between advanced and common interfaces
The interfaces that can be called by the background server to communicate with users. such actions are called interfaces, which are basic interfaces, you can call it without any payment or authentication. However, there are some advanced interfaces. your public account must have certain permissions, such as passing authentication to call advanced functions such as custom menus and payments.
However, the test number system of a public account can apply these advanced interfaces (except for interfaces involving transactions such as payment ).
Call of advanced interfaces
You must call a token_access interface before calling an advanced interface.
The advanced connectivity interface is as follows:
AppID and appsecreset are required to call token_access (the origins of the two are described in public account platform development (1)
The call code is as follows:
<? Php $ appid = "wxbad0b4x543aa0b5e"; $ appsecret = "ed222a84da15cd24c4bdfa5d9adbabf2"; $ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = $ appid & secret = $ appsecret "; // The following is a cURL session Process. you can use this session to return a string {" access_token ": "NU7Kr6v9L9TQaqm5NE3OTPctTZx797Wxw4Snd2WL2HHBqLCiXlDVOw2l-Se0I-WmOLLniAYLAwzhbYhXNjb"} That's the Access Token we're getting. It is used to call advanced function interfaces. When using this process, you can directly reference it without further research. This cURL system has many and complex functions. $ Ch = curl_init (); // initialize curl_setopt ($ ch, CURLOPT_URL, $ url); // create a dialogue with the url curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE ); // Configure curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE); // Configure curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // Configure $ output = curl_exec ($ ch); // execute the dialog and obtain the interface data Access Tokencurl_close ($ ch); // close the session $ jsoninfo = json_decode ($ output, true); // decodes interface data and converts a json string to a php variable or array. The default value is a variable, and the value true is an array. $ Access_token = $ jsoninfo ["access_token"];?>
Call advanced interfaces
1) call the custom menu function
// Create a json string $ jsonmenu = '{"button": [{"name": "About Us", "sub_button": [{"type ": "click", "name": "Company Profile", "key": "company profile" },{ "type": "click", "name ": "Social Responsibility", "key": "social responsibility" },{ "type": "click", "name": "Contact Us", "key ": "Contact Us"}] },{ "name": "Product Service", "sub_button": [{"type": "click", "name": "platform ", "key": "platform" },{ "type": "click", "name": "weibo application", "key": "weibo application "}, {"type": "click", "name": "Mobile website", "key": "Mobile website"}] },{ "name": "technical support ", "sub_button": [{"type": "click", "name": "document download", "key": "document download" },{ "type ": "click", "name": "Technology Community", "key": "Technology Community" },{ "type": "click", "name ": "service hotline", "key": "service hotline"}]} '; $ url =" https://api.weixin.qq.com/cgi-bin/menu/create?access_token= ". $ Access_token; // interface address $ result = https_request ($ url, $ jsonmenu); // establish a session var_dump ($ result) with the interface; function https_request ($ url, $ data = null) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data);} curl_setopt ($ curl, expires, 1 ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output ;} // add this code to the above code that calls the Access Token interface to add a menu on the public account interface.
After adding a menu for the public account, how can we set the corresponding effect when clicking the menu?
Another xml type of data transmission is involved here:
gh_82479813ed64
ojpX_jig-gyi3_Q9fHXQ4rdHniQs
1392297442
event
CLICK
公司简介
// Click the data transfer type in the click menu. The data is sent to the backend server and then the server responds.
There are many menu types and different xml types. for details, you can view the relevant documents on the public account platform.
*It should be noted that as long as you have the appID and appsecret of the public account, you can run this php code in any server space to access the server to call the corresponding functions, it is not necessary to run the task on the server that has token verification. Token verification is used to determine whether the data source is from the server on the background server. it is not closely related to calling the server's advanced interface.
Php files must be run on the server to produce results.
The call of other advanced interfaces is the same as that of custom menus.
2) call the customer service interface
When a user actively sends a message to a public account (including sending information, clicking a custom menu click event, subscribing to an event, scanning the QR code, and successful payment event), the message data is pushed to the developer. The developer can call the customer service interface message within a period of time and send the message to the user by posting a JSON packet.
The code is as follows:
$ Access_token = "nFX6GFsspSLBKJLgMQ3kj1YM8_FchRE7vE2ZOIlmfiCOQntZKnBwuOen2GCBpFHBYS4QLGX9fGoVfA36tftME2sRiYsKPzgGQKU-ygU7x8cgy_1tlQ4n1mhSumwQEGy6PK6rdTdo8O8GROuGE3Hiag ";
$ Openid = "o7Lp5t6n59DeX3U0C7Kric9qEx-Q"; // The user has an openID
See the openID acquisition method.
$ Data = '{"touser ":"'. $ openid. '"," msgtype ":" text "," text ": {" content ":" Hello World "}}'; // The data sent through the basic message interface is in XML format, but the data sent by calling the customer service interface is in json format, which is easier to transmit. $ Url = "https://api.weixin.qq.com/cgi-bin/message/custom/send? Access_token = ". $ access_token; $ result = https_request ($ url, $ data); var_dump ($ result); function https_request ($ url, $ data) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, expires, FALSE); curl_setopt ($ curl, expires, FALSE); curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); $ result = curl_exec ($ curl ); if (curl_errno ($ curl) {return 'errno '. curl_error ($ curl);} curl_close ($ curl); return $ result ;}
For details about how to send text message, music message, and video message formats through the customer service interface, see the development help documentation on the public platform.
The customer service interface can be used together with the message interface.
We may not understand why such a customer service interface is needed since xml data can be directly sent to users through the passive response interface? It can be understood that a passive response message can only reply to the same message once. If you enter a name for a singer on a music platform, messages sent in a passive response will always reply to the same song. However, you can reply to different songs each time through the customer service interface, which involves the MySQL database.
Simply put, a public platform that allows you to view the express delivery addresses. Every time you enter the same order number, the background is able to reply to the order location each time (for the same text, you can make different responses), just like a manual reply, this is the customer service interface.
3) QR code generation interface
Two types of QR codes are available: temporary QR code eh and permanent QR code permanent. The former has an expiration time of up to 1800 s.
To generate a QR code, you need to call three interfaces,
The first one is access_token.
The second is to generate the ticket interface.
The third is the ticket generated through the second interface in exchange for the QR code image.
$ Access_token = "xDx0pD_ZvXkHM3oeu5oGjDt1_9HxlA-9g0vtR6MZ-v4r7MpvZYC4ee4OxN97Lr4irkPKE94tzBUhpZG_OvqAC3D3XaWJIGIn0eeIZnfaofO1C3LNzGphd_rEv3pIimsW9lO-4FOw6D44T3sNsQ5yXQ"; // assume that the obtained access token is this code. // Temporary QR code $ qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id ": 10000 }}'; // permanent QR code $ qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id ": 1000 }}'; $ url =" https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token= $ Access_token "; // Create a ticket interface $ result = https_request ($ url, $ qrcode); $ jsoninfo = json_decode ($ result, true ); $ ticket = $ jsoninfo ["ticket"]; function https_request ($ url, $ data = null) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data);} curl_setopt ($ curl, expires, 1 ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output;} $ ticket = "response ="; // Obtain the string of ticket $ url =" https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket= ". Urlencode ($ ticket); // the image code of the QR code opposite to ticket. $ ImageInfo = downloadWeixinFile ($ url); $ filename = "qrcode.jpg"; $ local_file = fopen ($ filename, 'w'); if (false! ==$ Local_file) {if (false! = Fwrite ($ local_file, $ imageInfo ["body"]) {fclose ($ local_file) ;}} function downloadWeixinFile ($ url) {$ ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_NOBODY, 0 ); // only the body header curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ ch, success, FALSE); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); $ package = curl_exec ($ ch); $ httpinfo = curl_getinfo ($ ch); curl_close ($ ch); return array_merge (array ('body' => $ package ), array ('header' => $ httpinfo ));}
When you run this code in the server space, the browser will generate a QR code image.
This API is used to obtain non-functional interfaces, such as traffic information and weather forecasts.
The above is all the content of this article. I hope it will be helpful to everyone's learning, and I hope you can support your own home.