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'];