PHP WeChat Advanced Interface Invocation method (custom menu interface, customer service interface, QR code) _php instance

Source: Internet
Author: User
Tags cdata openid
how to invoke the Advanced interface

The difference between an advanced interface and a common interface

The backend server can call the interface to communicate with the user for the message, such behavior is in the calling interface, these interfaces are the basic interface, you do not need any pay behavior or authentication behavior can be called. But with some advanced interfaces, your public number must reach certain permissions, such as authentication, to invoke custom menus, payments, and other advanced features.
However, the test number system of the public account can be applied to these advanced interfaces (except those involving transactions such as payments).

Calls to advanced interfaces

The call to the advanced interface requires a token_access interface to be called before the other advanced interfaces can be called.
As follows: Connected Advanced interface


Calls to token_access need to use AppID and Appsecreset (in the Public Platform development (i) has already described the origin of both)

The calling 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 through which you can return a string {"Access_token": " NU7KR6V9L9TQAQM5NE3OTPCTTZX797WXW4SND2WL2HHBQLCIXLDVOW2L-SE0I-WMOLLNIAYLAWZHBYHXNJB "} This is the access token we're going to get. Rely on it when invoking the Advanced feature interface. This process is used when the direct reference is good, do not need to drill down, this curl system related functions are a bit more and more complex. $ch = Curl_init ();//Initialize curl_setopt ($ch, Curlopt_url, $url);//Establish dialog with 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 dialog to get interface data access Tokencurl_ Close ($ch);//closed Session $jsoninfo = Json_decode ($output, true);//decode interface data, convert JSON format string to PHP variable or array. The default is the variable, plus true after the array. $access _token = $jsoninfo ["Access_token"];? >

Calling the Advanced interface

1), call the custom menu function

 Create a JSON string for a custom menu $jsonmenu = ' {"button": [{"Name": "About Us", "Sub_button": [{"Type": "click", "Name": "Company Jane "," "Key": "Company Profile"}, {"Type": "click", "Name": "Social Responsibility", "key": "Social Responsibility"}, {"Type": "click", "Name": "union   US "," key ":" Contact Us "}]}, {" Name ":" Product Service "," Sub_button ": [{" Type ":" click "," Name ":" Platform "," key ":" Platform "    }, {"Type": "click", "Name": "Weibo app", "key": "Weibo App"}, {"Type": "click", "Name": "Mobile website", "Key": "Mobile Website" }]}, {"Name": "Technical Support", "Sub_button": [{"Type": "click", "Name": "Document Download", "key": "Document Download"}, {"Type" : "Click", "Name": "Technical 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 session var_dump ($result) with 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, Curlopt_returntransfer, 1); $output = curl_exec ($curl); Curl_close ($curl); return $output;} Adding this code to the code above to invoke the access token interface allows you to add a menu to the public number interface.

When we add a menu to the public number, how do I set the Click menu to have a corresponding effect?
This involves another type of XML data transfer:

 
  
  
    gh_82479813ed64 1392297442
  
   
  
    ojpX_jig-gyi3_Q9fHXQ4rdHniQs
  
   
  
   
  
   
  
    event
  
   //above is the type of data transfer that is clicked on the Click menu and the data is sent to the backend server
  
    CLICK
  
   
  
    公司简介
  
   
 
   The server then responds.

There are a variety of menu types, different types of XML, and details can be viewed on the public number platform.

* This is to illustrate that as long as you have the public number of AppID and Appsecret, in any server space running this PHP code can go to the server to invoke the corresponding function, not necessarily in the token authentication of the server run. Token verification is for the background server to determine whether the data source is from the server, and the call to the server's advanced interface is not much association.
PHP files must be run on the server to produce an effect.

Calls to other advanced interfaces are the same as invoking a custom menu.

2), call the customer service interface

When a user proactively sends a message to a public account (including sending messages, clicking a custom menu click event, subscribing to an event, scanning a QR code, and paying a success event), the message data is pushed to the developer. Developers can call the customer service interface message for a period of time and send a message to the user via post with a JSON packet.

Copy the Code code as follows: $access _token = "nfx6gfsspslbkjlgmq3kj1ym8_ Fchre7ve2zoilmficoqntzknbwuoen2gcbpfhbys4qlgx9fgovfa36tftme2sriyskpzggqku-ygu7x8cgy_ 1tlq4n1mhsumwqegy6pk6rdtdo8o8grouge3hiag ";
$openid = "o7lp5t6n59dex3u0c7kric9qex-q";//Users have an OpenID

This is how OpenID is acquired.

$data = ' {' Touser ': '. $openid. ' "," Msgtype ":" Text "," text ": {   " content ":" Hello World "}} ';//data sent via the underlying message interface is in XML format, But the data sent by the call-to-service interface is JSON data format, which is easier to transfer. $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, Curlopt_ssl_verifypeer, FALSE); curl_setopt ($curl, Curlopt_ssl_verifyhost, 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;}

Customer Service Interface send text message, music message, video message specific format please refer to the development Help document on the public platform.
The customer interface can be mixed with the messaging interface.

It may not be clear to all of you that since you can send XML data directly to the user through the interface of the passive response message, why do you need such a customer interface? It can be understood that a passive response message is a one-time reply to the same message only once. If you enter a pop star name to a music platform, the message sent by passive response will always only reply to your song. But through the customer interface method can reply to different songs each time, this involves the MySQL database.

A little simpler, a public platform that looks at the courier including the address. You enter the same order number each time, the background can reply to the order each time the location (for the same text but can make a different response) like a manual reply, this is the customer service interface.

3), generate two-dimensional code interface

Two-dimensional code types, respectively, are temporary two-dimensional code eh and permanent Two-dimensional code, the former and expiration time, the longest is 1800s.
To generate a QR code you need to call 3 interfaces,
The first one is Access_token.
The second one is to generate the ticket interface
The third one is the ticket generated by the second interface in exchange for the QR code image.

$access _token = "Xdx0pd_zvxkhm3oeu5ogjdt1_9hxla-9g0vtr6mz-v4r7mpvzyc4ee4oxn97lr4irkpke94tzbuhpzg_ OVQAC3D3XAWJIGIN0EEIZNFAOFO1C3LNZGPHD_REV3PIIMSW9LO-4FOW6D44T3SNSQ5YXQ ";//Assume that access token obtained is this piece of 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": +}} '; $url = "https:// api.weixin.qq.com/cgi-bin/qrcode/create?access_token= $access _token ";//Create 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, Curlopt_returntransfer, 1); $output = curl_exec ($curL); Curl_close ($curl); return $output;} $ticket = "gqhi8doaaaaaaaaaasxodhrwoi8vd2vpeglulnfxlmnvbs9xl0uwetnxni1sdla3rklyrnnkbufvaaieldnuugmeaaaaaa==";// Gets the string of ticket = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=". UrlEncode ($ticket);//ticket Two-dimensional code image code. $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 take body head curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE); curl_setopt ($ch, Curlopt_ssl_verifyhost, 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) '); }

Running this code in server space, the browser generates a two-dimensional code image.

Get non-functional interfaces, such as getting traffic information, weather forecasts.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the script home.

  • 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.