PHP micro-Credit Advanced Interface Invocation method (custom menu interface, customer service interface, two-dimensional code) _php instance

Source: Internet
Author: User
Tags cdata curl openid php code ticket

How to invoke the micro-letter Advanced Interface

The difference between micro-letter advanced interface and micro-letter Normal interface

The backend server can invoke the communication of the micro-letter interface with the micro-trust user, and the behavior is to invoke the micro-letter interface, which is the basic interface, you do not need any pay behavior or identity authentication behavior can be invoked. However, there are some advanced interfaces, your micro-credit public number must reach certain permissions such as the use of micro-letter authentication to invoke custom menus, micro-payment and other advanced functions.
However, the micro-credit public account test system can be applied to these advanced interfaces (micro-credit payment, such as transactions involving the interface, except).

Call of micro-credit Advanced interface

The call to the micro-credit advanced interface calls for a token_access interface to be invoked before the other advanced interfaces can be invoked.
as follows: Schematic diagram of connected advanced interface


Call token_access need to use AppID and Appsecreset (the origins of both are already described in the micro-trust platform Development (i))

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 section of the string {"Access_token": " NU7KR6V9L9TQAQM5NE3OTPCTTZX797WXW4SND2WL2HHBQLCIXLDVOW2L-SE0I-WMOLLNIAYLAWZHBYHXNJB "}
This is the access we're going to get It's token. You rely on it when calling the advanced functionality interface. This process is used when the direct reference is good, do not need to delve, this curl system related functions a little more and complex.

$ch = Curl_init ()///Initialize
curl_setopt ($ch, Curlopt_url, $url);//Create 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, get interface data access Token
curl_close ($ch);//close session
$jsoninfo = Json_ Decode ($output, true);//decode interface data, convert JSON format string to PHP variable or array. The default is a variable, plus true is the array.
$access _token = $jsoninfo ["Access_token"];

? >

Calling the micro-credit Advanced interface

1), Invoke custom menu function

 Create a JSON string for a custom menu $jsonmenu = ' {button ': [{' Name ': ' About us ', ' Sub_button ': [{' Type ': ' Click ', ' n Ame ":" Company Profile "," Key ":" Company Profile "}, {" Type ":" click "," Name ":" Social Responsibility "," key ":" Social Responsibility "}, {" type ":" Cl
    Ick "," name ":" Contact Us "," key ":" Contact Us "}}, {" Name ":" Product Services "," Sub_button ": [{" Type ":" Click ", "Name": "Micro-platform", "key": "Micro-Platform"}, {"Type": "click", "Name": "Microblogging Application", "Key": "Microblogging Application"}, {"Type": "
    Click "," Name ":" Mobile Web Site "," Key ":" Mobile Web Site "}}, {" Name ":" Technical Support "," Sub_button ": [{" Type ":" Click ", "Name": "Document Download", "key": "Document Download"}, {"Type": "click", "Name": "Technology community", "key": "Technical 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);//Interface established session var_dump ($RESULT);
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;

 //Add this code to the above code that calls the access token interface to implement the function of adding menus to the micro-public number interface.

When we add a menu for the micro-public number, how to set the Click menu when the corresponding effect will appear?
This involves another type of XML data transfer:

<xml>
<tousername><![ Cdata[gh_82479813ed64]]></tousername>
<fromusername><![ Cdata[ojpx_jig-gyi3_q9fhxq4rdhniqs]]></fromusername>
<createtime>1392297442</createtime >
<msgtype><![ Cdata[event]]></msgtype>
<event><![ Cdata[click]]></event>
<eventkey><![ Cdata[Company Profile]]></eventkey>
</xml>/
/above is a data delivery type that clicks on the Click menu, the data is sent to the backend server, and the server responds.

There are a variety of menu types, different types of XML, details can be viewed on the micro-public platform to view the appropriate document.

* the point here is that as long as you have AppID and Appsecret of the micro-credit, the PHP code in any server space can go into the micro-trust server to invoke the corresponding function, not necessarily in the token verified server running. Token authentication is for the background server to determine whether the data source is from the micro-trust server, and to invoke the micro-server's advanced interface does not have much association.
The php file must be running on the server before it will have an effect.

Calls to other advanced interfaces are the same as calls to custom menus.

2), call Customer service interface

Micro-trust will push the message data to the developer when the micro-credit user initiates the message to the micro-trust public account (including sending information, clicking the custom menu click event, subscribing to the event, scanning the two-dimensional code, and paying the success event). The developer can invoke the customer interface message over a period of time and send a message to the user by post a JSON packet.

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

The following figure shows the way that OpenID is obtained.

$data = ' {
 "touser": "'. $openid. '",
 "Msgtype": "Text",
 "text":
 {
   "content": "Hello World"
 }
the data sent through the underlying message interface is in XML format, but the data sent by the call service interface is formatted as JSON data and 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, 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 see the development Help document on the micro-credit public platform.
The customer interface can be mixed with the message interface.

You may not understand why you need such a customer interface, since you can send XML data directly to the user via a passive response message interface. It can be understood that passive response messages are one-time and can only reply to the same message once. If you enter a singer name on a music platform, messages sent in a passive response will always reply to a song that is the same. But through the customer interface, you can reply to different songs each time, which involves MySQL database.

To be simpler, a micro-trust public platform that looks at the delivery address. Each time you enter the same order number, the background is able to reply to the order every time the location (for the same text but can make a different response) is like a manual response, this is the customer interface.

3), generate two-dimensional code interface

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

$access _token = "Xdx0pd_zvxkhm3oeu5ogjdt1_9hxla-9g0vtr6mz-v4r7mpvzyc4ee4oxn97lr4irkpke94tzbuhpzg_

OVQAC3D3XAWJIGIN0EEIZNFAOFO1C3LNZGPHD_REV3PIIMSW9LO-4FOW6D44T3SNSQ5YXQ ";//Assume that access token is the code.
Temporary two-dimensional code $qrcode = ' {expire_seconds ': 1800, "Action_name": "Qr_scene", "Action_info": {"SCENE": {"scene_id": 10000}}} ';

Permanent two-dimensional 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 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 ticket string $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=". UrlEncode ($ticket);//ticket two-dimensional code picture 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 (the ' body ' => $package), Array (' header ' => $httpinfo));

 }

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

Access to non-micro-function interface, such as access to traffic information, weather forecast.

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.