Detailed graphic and text explanation of WeChat server IP interface examples developed by WeChat public platform (with code)

高洛峰
Release: 2017-03-18 09:28:36
Original
1951 people have browsed it

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

##Return instructions:

Under normal circumstances, WeChat will return the

JSON data packet to the official account, as shown in the figure :

Detailed graphic and text explanation of WeChat server IP interface examples developed by WeChat public platform (with code)

Return information parameter description, as shown in the table:


Parameters

Description

ip_list

WeChat server IP address list

Code:

<?php
/*
 *获取微信服务器IP地址
*/
require(&#39;wei_function.php&#39;);
$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[&#39;access_token&#39;];
//获取微信服务器IP接口地址
$ipurl="https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$token."";
$iparr=(array)json_decode(getdata($ipurl));
foreach ($iparr[&#39;ip_list&#39;] as $key => $value) {
  echo $value."<br>";//用循环的方式打印IP集合
}
?>
Copy after login

Code analysis:

require('wei_function. php'); Contains wei_function.php

Use 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 a

two-dimensional array, as shown in the figure ;

Detailed graphic and text explanation of WeChat server IP interface examples developed by WeChat public platform (with code)

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[&#39;ip_list&#39;] as $key => $value)
{
echo $value."<br>";//用循环的方式打印IP集合
}
Copy after login


The result of running the program calling interface is as shown in the figure.

Detailed graphic and text explanation of WeChat server IP interface examples developed by WeChat public platform (with code)

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!