WeChat public platform development 4-Example of Long-chain connection to short-chain interface call (including source code), 4-including source code

Source: Internet
Author: User
Tags website server

Public platform development 4-Example of long chain connection to short chain interface call (including source code), 4-including source code

Public platform development-access_token acquisition and application (including source code)
Author: Meng xianglei-public platform development example tutorial

Convert a long link to a short link. The original link used by the developer to generate QR codes (products, payment QR codes, etc.) is too long, leading to a decrease in scanning speed and success rate, converting the original long link into a short link and generating a QR code through this interface will greatly increase the scanning speed and success rate.

I. instance call

Interface Description

Http Request Method: GET

Interface call address:

Https://api.weixin.qq.com/cgi-bin/shorturl? Access_token = ACCESS_TOKEN

The request parameters are described in the following table:

Parameters

Required?

Description

Access_token

Yes

Interface call credential

Action

Yes

Long2short is entered here, which indicates the long chain to short link

Long_url

Yes

Long link to be converted. It supports URLs in the format of http: //, https: //, and weixin: // wxpay.

Return description:

Under normal circumstances, a JSON data packet is returned to the public number, as shown below:

{"Errcode": 0, "errmsg": "OK", "short_url": "http: \// w.url.cn \/s \/AvCo6Ih "}

The following table describes the response parameters:

Parameters

Description

Errcode

Error code.

Errmsg

Error message.

Short_url

Short link.

Use the program call interface to obtain:

<? Php/** long chain to short link */require ('wei _ function. php '); $ appid = "wx78478e595939c538"; $ secret = "5540e8ccab4f71dfad752f73cfb85780"; $ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ appid. "& secret = ". $ secret. ""; $ output = getdata ($ url); $ tokenarr = (array) json_decode ($ output); $ token = $ tokenarr ['Access _ token']; $ date = '{"action": "long2short", "long_url": "https://item.jd.com/12041625.html"}'; $ your url = "https://api.weixin.qq.com/cgi-bin/shorturl? Access_token = ". $ token. ""; $ shortarr = (array) json_decode (getshort ($ date, $ response url); echo $ shortarr ['short _ url']; function sendcontent ($ date, $ url) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ date); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); $ data = curl_exec ($ ch); curl_close ($ ch); return $ data ;}?>

2. Code Parsing

Require ('wei _ function. php '); contains wei_function.php. After obtaining the access_token using the getdata () function, we need to send the specified data to the corresponding website server and then obtain the data returned by the server.

$ Date = '{"action": "long2short", "long_url": "http://www.jikexueyuan.com/course/1578.html "}';

The format of the data to be sent is JSON. A long link converted here is:

Https://item.jd.com/12041625.html

Send data to: $ response url = "https://api.weixin.qq.com/cgi-bin/shorturl? Access_token = ". $ token ."";

In addition to the getdata () function, a sendcontent () function is added. The difference between this function and getdata () is that, apart from sending a link request to the server, you can also send separate data to the other server. The other server then returns the corresponding results based on the data sent.

Similarly, write the sendcontent () function to the wei_function.php file. In this case, the functions in the wei_function.php File Include object_array (), get_weather (), getdata (), and sendcontent (), for this file, see page 95th of "public platform development instance". The optimized code is as follows:

<? Php/** long chain to short link */require ('wei _ function. php '); $ appid = "wx78478e595939c538"; $ secret = "5540e8ccab4f71dfad752f73cfb85780"; $ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ appid. "& secret = ". $ secret. ""; $ output = getdata ($ url); $ tokenarr = (array) json_decode ($ output); $ token = $ tokenarr ['Access _ token']; // send xml data $ date = '{"action": "long2short", "long_url": "https://item.jd.com/12041625.html "}'; // long chain to short Link interface address $ hyperlink url = "https://api.weixin.qq.com/cgi-bin/shorturl? Access_token = ". $ token. ""; $ shortarr = (array) json_decode (sendcontent ($ date, $ response url); echo $ shortarr ['short _ url'];?>

Run the file to obtain the message that the persistent link is successfully converted to a transient link.

 

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.