Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Interface call by php

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to implement the interface call of php, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.

The difference between Wechat Advanced Interface and Wechat ordinary Interface

The background server can call Wechat's interface to communicate with Wechat users, this behavior is to call Wechat's interface, these interfaces are basic interfaces, you do not need any payment behavior or identity authentication behavior can be called. But there are some advanced interfaces, your Wechat official account must achieve certain permissions, such as Wechat authentication in order to call custom menus, WeChat Pay and other advanced functions.

However, the test system of Wechat public account can apply these advanced interfaces (except those involving transactions such as WeChat Pay).

The call of Wechat advanced interface

To call the Wechat advanced API, you need to call a token_access API first, and only by calling this API can you call other high-level APIs.

As follows: schematic diagram of the high-level interface

AppID and appsecreset are required to call token_access (the origin of both has been described in the official Wechat platform Development (1)).

The calling code is as follows

Call the advanced interface of Wechat

1), call the custom menu function

/ / create a custom menu 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": "products and services", "sub_button": [{"type": "click", "name": "Wechat platform", "key": "Wechat 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": "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 with the interface 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 Access Token interface to add menus to the official account interface of Wechat.

When we add a menu to the official account of Wechat, how do we set up the click menu and the corresponding effect will appear?

Another xml type of data transfer is involved here:

1392297442 / the above is the data transfer type that clicks on the click menu, and the data is sent to the background server, and then the server responds.

There are many types of menus, but there are different xml types. For details, you can view the corresponding documents on the Wechat official account platform.

* it is important to note that as long as you have the appID and appsecret of Wechat official account, running this php code in any server space can go to Wechat server and invoke the corresponding functions. You don't have to run under a server that has been verified by token. Token verification is for the backend server to determine whether the data source is from the Wechat server, and it does not have much to do with the advanced interface that calls the Wechat server.

The php file must be run on the server to have an effect.

Calls to other high-level interfaces are the same as calling custom menus.

2) call the customer service API

When Wechat users actively send messages to Wechat public accounts (including sending messages, clicking custom menu click events, subscription events, scanning QR codes, payment success events) Wechat will push the message data to the developer. Within a period of time, the developer can call the customer service interface message and send the message to the user by post a JSON packet.

The copy code is as follows:

$access_token = "nFX6GFsspSLBKJLgMQ3kj1YM8_FchRE7vE2ZOIlmfiCOQntZKnBwuOen2GCBpFHBYS4QLGX9fGoVfA36tftME2sRiYsKPzgGQKU-ygU7x8cgy_1tlQ4n1mhSumwQEGy6PK6rdTdo8O8GROuGE3Hiag"

$openid = "o7Lp5t6n59DeX3U0C7Kric9qEx-Q"; / / Wechat users all have an openID

The following figure shows how to obtain openID.

$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 data 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, 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;}

For the specific format of the customer service interface to send picture and text messages, music messages and video messages, please refer to the development help documentation on the Wechat public platform.

The customer service interface can be mixed with the message interface.

You may not understand why you need such a customer service interface since you can send xml data to users directly through the interface that responds to messages passively. It can be understood that a passive response message is an one-time response that can only reply to the same message once. If you enter a singer's name into a music platform, messages sent through passive response will always reply to you with the same song. But through the customer service interface, you can reply to different songs each time, which involves the MySQL database.

To make it a little simpler, a Wechat public platform for checking couriers, including addresses. Every time you enter the same order number, the backend can reply to the location of the order each time (you can respond differently to the same text), just like a manual reply, this is the customer service interface.

3), generate QR code interface

There are two types of QR codes, temporary QR code eh and permanent QR code, the former and the expiration time, the longest is 1800s.

To generate the 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 by the second interface in exchange for QR code images.

$access_token = "xDx0pD_ZvXkHM3oeu5oGjDt1_9HxlA-9g0vtR6MZ-v4r7MpvZYC4ee4OxN97Lr4irkPKE94tzBUhpZG_OvqAC3D3XaWJIGIn0eeIZnfaofO1C3LNzGphd_rEv3pIimsW9lO-4FOw6D44T3sNsQ5yXQ"; / / assume that the acquired ACCESS TOKEN is this code. / / temporary 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 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=="; / / get the string of ticket $url = "QR code opposite the QR 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 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));}

Run this code in the server space and the browser will generate a QR code image.

Get non-Wechat functional interfaces, such as traffic information and weather forecast.

Thank you for reading this article carefully. I hope the article "how to implement php Interface call" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report