-
php 微信接口API之长链接转短链接代码示例
- <?php
- header("Content-Type: text/html; charset=utf-8");
- $longurl;
- if(isset($_POST['longurl'])){
- $longurl = $_POST['longurl'];
- }else{
- die("没有post值进来");
- }
-
-
- $id = "你的AppID";
- $secret = "你的AppSecret";
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$id."&secret=".$secret."";
-
- $token = getAccessToken($url);
- $data = '{"action":"long2short","long_url":"'.$longurl.'"}';
- $shorturl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=".$token."";
-
- echo httpPost($shorturl,$data);
-
- function getAccessToken($url) {
-
- $data = json_decode(file_get_contents("access_token.json"));
- if ($data->expire_time < time()) {
-
- $output = httpGet($url);
- $res = (array)json_decode($output);
- $access_token = $res['access_token'];
- if ($access_token) {
- $data->expire_time = time() + 7000;
- $data->access_token = $access_token;
- $fp = fopen("access_token.json", "w");
- fwrite($fp, json_encode($data));
- fclose($fp);
-
- }
- } else {
- $access_token = $data->access_token;
- }
- return $access_token;
- }
-
- function httpGet($url) {
-
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_URL, $url);
-
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
-
- function httpPost($url,$data){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
-
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
-
- ?>
-
相关阅读:
20 类中的函数重载
19 友元的尴尬能力
18 类的静态成员函数
17 类的静态成员变量
16 经典问题解析二
15 临时对象
Lucene4.6查询时完全跳过打分,提高查询效率的实现方式
Lucene4.6 把时间信息写入倒排索引的Offset偏移量中,并实现按时间位置查询
Lucene6去掉了Filter但是可以用BooleanQuery实现Filter查询
Dom4j解析语音数据XML文档(注意ArrayList多次添加对象,会导致覆盖之前的对象)
-
原文地址:https://www.cnblogs.com/yyjie/p/7306073.html
Copyright © 2020-2023
润新知