- <?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;
- }
-
- ?>
posted @
2017-08-08 11:48
永杰本杰
阅读(
2144)
评论()
编辑
收藏
举报