微信服务器 IP 及长链接转短链接接口调用

1.微信服务器IP接口调动实例,一般用在安全方面,如果公众号基于安全等考虑,需要获知微信服务器的IP地址列表,以便进行相关限制,可以通过该接口获得微信服务器IP地址列表或者IP网段信息。

- 接口调用请求说明

1 http请求方式: GET
2 https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=ACCESS_TOKEN

- 例子:

 1 <?php
 2 $appId = "sdsd8706727";
 3 $appSecret = "9sddsdsdsdsdsdb79839d";
 4 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
 5 
 6 // $output = getToken($url); 
 7 // $token = (array)json_decode($output);
 8 // $accessToken = $token['access_token'];
 9 // echo $accessToken;
10 $accessToken = "sdfsdfsdfsdflG8a7YLEsdfsdfsdfsdfsd-ZfN1hEZOLSs5iOkEoCmI10Nh26-TwlXead-bHyKMAT0qmrsP_SmjtfkuRPEjACALJY";
11 
12 $ipUrl = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={$accessToken}";
13 
14 function getToken($url) {
15     $ch = curl_init();
16     curl_setopt($ch, CURLOPT_URL, $url);
17     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
18     curl_setopt($ch, CURLOPT_HEADER, 0);
19     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko)");
20     curl_setopt($ch, CURLOPT_ENCODING, "gzip");//加入gzip解析
21     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
22     $output = curl_exec($ch);
23     curl_close($ch);
24 
25     return $output;
26 }
27 
28 $output = getToken($ipUrl); 
29 
30 /*
31     json_decode — 对 JSON 格式的字符串进行解码
32     说明:
33     mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
34     接受一个 JSON 编码的字符串并且把它转换为 PHP 变量
35     参数:
36     json
37     待解码的 json string 格式的字符串。
38     这个函数仅能处理 UTF-8 编码的数据。
39 */
40 $ipArr = (array)json_decode(getToken($ipUrl));
41 
42 foreach ($ipArr['ip_list'] as $key => $value) {
43     // echo $value,"<br />";
44     $valueArr.=$value."#";
45 }
46 
47 //实际应用,判断某个IP是否包含在微信服务器 IP
48 
49 //获取到的ip
50 $getIp = "101.226.62.77";
51 
52 /*
53     strpos()查找字符串首次出现的位置
54     mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
55     返回 needle 在 haystack 中首次出现的数字位置。
56 
57     参数:
58     haystack
59     在该字符串中进行查找。
60 
61     needle
62     如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值。
63 
64     offset
65     如果提供了此参数,搜索会从字符串该字符数的起始位置开始统计。 如果是负数,搜索会从字符串结尾指定字符数开始。
66 */
67 if (strpos("##".$valueArr, $getIp) > 0){
68     echo "验证成功";
69 }else{
70     echo "非法请求";
71     exit;
72 }

2.长链接转短连接接口调用的实例

- 主要用于太长的链接导致相应速度变慢,此时可通过此接口将链接变短

- 开发者用于生成二维码的原链接(商品、支付二维码等)太长导致扫码速度和成功率下降,将原长链接通过此接口转成短链接再生成二维码将大大提升扫码速度和成功率。

- 接口调用请求说明,开发者可通过OpenID来获取用户基本信息。请使用https协议。

1 http请求方式: POST
2 https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN

- 实例:

<?php
$appId = "asdasdfasdfsadfas";
$appSecret = "asdsadasdfadsfasfadf";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";

// $output = getToken($url); 
// $token = (array)json_decode($output);
// $accessToken = $token['access_token'];
// echo $accessToken;

$accessToken = "asdasdasdfasdfasdfasdasdfasdfasdfasdfasdfasdfasdfaf";

$data = '{"action":"long2short","long_url":"https://www.cnblogs.com/fangfeiyue"}';
$shortUrl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token={$accessToken}";

function getShort($data, $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko)");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    $tmpInfo = curl_exec($ch);

    if (curl_errno($ch)){
        return curl_error($ch);
    }

    curl_close($ch);
    return $tmpInfo;
}

$shorurl = (array)json_decode(getShort($data, $shortUrl)) ;
echo $shorurl['short_url'];
原文地址:https://www.cnblogs.com/fangfeiyue/p/7411554.html