This article mainly introduces detailed graphic and text examples of WeChat server IP interface development on WeChat public platform (with code), which has a good reference value. Let’s take a look with the editor below
After learning how to obtain and apply access_token, formally use access_token to call the interfaces of other WeChat public platforms to deepen our understanding and usage.
1. Example of obtaining the IP address of the WeChat server
(1) Interface introduction
If Due to security and other considerations, public accounts need to know the IP address list of the WeChat server in order to implement relevant restrictions. You can obtain the WeChat server IP address list or IP network segment information through this interface.
(2) Instance call
Interface description
http request method: GET
Interface call Address:
api.weixin.qq.com/cgi-bin/getcallbackip?access_token=ACCESS_TOKEN
Request parameter description, as shown in the table:
Parameters |
Is it required |
Description |
access_token |
is the access_token# of the |
official account |
JSON data packet to the official account, as shown in the figure :
Return information parameter description, as shown in the table:Parameters | Description |
ip_list | WeChat server IP address list |
<?php /* *获取微信服务器IP地址 */ 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); $token=(array)json_decode($output); //获取到access_token参数 $token=$token['access_token']; //获取微信服务器IP接口地址 $ipurl="https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$token.""; $iparr=(array)json_decode(getdata($ipurl)); foreach ($iparr['ip_list'] as $key => $value) { echo $value."<br>";//用循环的方式打印IP集合 } ?>
Code analysis:
require('wei_function. php'); Contains wei_function.phpUse the getdata() function. After obtaining the access_token, continue to replace the access_token value of the $ipurl value;$iparr=(array)json_decode(getdata( $ipurl)); Obtain the data of $ipurl through the getdata() function, and then process it through the json_decode function to obtain $iparr. At this time, the variable value is atwo-dimensional array, as shown in the figure ;
What we need is [ip_list] in the array, so we take out the array set of [ip_list] separately, and loop out each WeChat server IP through foreach, Code:foreach ($iparr['ip_list'] as $key => $value) { echo $value."<br>";//用循环的方式打印IP集合 }
The above is the detailed content of Detailed graphic and text explanation of WeChat server IP interface examples developed by WeChat public platform (with code). For more information, please follow other related articles on the PHP Chinese website!