api/app/controllers/UrlsController.php

108 lines
3.4 KiB
PHP
Raw Normal View History

2023-04-07 19:05:18 +08:00
<?php
/**
* Tiffany SMS API重构-接口类
* @author QZQ
*/
header("Content-Type:text/html; Charset=utf-8");
class UrlsController extends ControllerBase{
function initialize(){
parent::initialize();
}
2023-04-14 16:13:30 +08:00
/**
* 测试方法
* http://127.0.0.1/tiffany/api/urls/test?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/urls/test?qaz=wsx
*/
function testAction(){
//$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/urls/shorturl";
$url = "http://127.0.0.1/tiffany/api/urls/shorturl?qaz=wsx";
$post_arr = array();
$post_arr['appid'] = "0e355010-67b9-4aa6-a53f-c92c972094a7";
$post_arr['access_token'] = "9d21508a7531430c843cff8063dccb1c";
2023-04-18 16:23:29 +08:00
$post_arr['access_token'] = "8dfd706ffcfce2c7067df73f8e122020";
2023-04-14 16:13:30 +08:00
$post_arr['url'] = "https://www.baidu.com/";
$post_json = json_encode($post_arr);
$result = $this->__http_post_request($url, $post_json, true);
print_r($result); die;
}
2023-04-07 19:05:18 +08:00
/**
* 长链转短链Long URL shortening
* http://127.0.0.1/tiffany/api/urls/shorturl?qaz=wsx
2023-04-14 16:13:30 +08:00
* http://weapp.wemediacn.net/d/tiffany/api/urls/shorturl?qaz=wsx
2023-04-07 19:05:18 +08:00
*/
function shorturlAction(){
2023-04-14 16:13:30 +08:00
//如果是GET请求
if($this->request->isGet()){
//告知请求的资源不支持 http 方法“GET”。
$rs['Message'] = "请求的资源不支持 http 方法“GET”。";
}
2023-04-17 13:52:24 +08:00
//如果不是GET请求
2023-04-14 16:13:30 +08:00
else{
2023-04-17 14:12:35 +08:00
//校验是否有收到请求数据
$rs = $this->__check_request_arr();
//如果有收到请求数据
2023-04-17 13:52:24 +08:00
if(!empty($rs['data']['request_arr'])){
2023-04-17 14:12:35 +08:00
//获取请求数据
2023-04-17 13:52:24 +08:00
$request_arr = $rs['data']['request_arr'];
2023-04-17 14:12:35 +08:00
//准备校验基本参数是否都不为空
2023-04-17 13:52:24 +08:00
$params_arr[] = "appid";
$params_arr[] = "url";
$params_arr[] = "access_token";
2023-04-17 14:12:35 +08:00
//校验基本参数是否都不为空
2023-04-17 13:52:24 +08:00
$rs = $this->__check_params_arr($request_arr, $params_arr);
2023-04-17 14:12:35 +08:00
//如果基本参数都不为空
2023-04-17 13:52:24 +08:00
if(empty($rs['errcode'])){
//准备校验所传appid是否有效
2023-04-14 16:13:30 +08:00
$appid = $request_arr['appid'];
//校验所传appid是否有效
$rs = $this->__check_appid($appid);
//如果所传appid有效
2023-04-17 13:52:24 +08:00
if(empty($rs['errcode'])){
//获取接口信息
$AppInfo = $rs['data']['AppInfo'];
//准备校验所传access_token是否有效
$access_token = $request_arr['access_token'];
//校验所传access_token是否有效
$rs = $this->__check_access_token($appid, $access_token);
//如果所传access_token有效
2023-04-14 16:13:30 +08:00
if(empty($rs['errcode'])){
2023-04-17 13:52:24 +08:00
//设置生成长链接
$post_arr['urls'] = json_encode(array($request_arr['url']));
//设置生成TokenID
2023-04-18 17:49:02 +08:00
$post_arr['TokenID'] = WE_SURL_API_TIFFANY_TOKEN;
2023-04-17 13:52:24 +08:00
//准备生成短链接
2023-04-18 11:47:18 +08:00
$url = WE_SURL_API_GET_URL;
2023-04-17 13:52:24 +08:00
//获取生成结果
$json = $this->__http_post_request($url, $post_arr);
//转换生成结果
$array = json_decode($json, true);
//如果生成失败
if(!(strpos(strtolower($json), "ok")!==false)){
2023-04-18 15:31:29 +08:00
//告知用户"服务器异常,err detail:短链转换失败"
$rs['errmsg'] = "服务器异常,err detail:短链转换失败";
$rs['errcode'] = 50000;
2023-04-14 16:13:30 +08:00
$rs['data'] = null;
}
2023-04-17 13:52:24 +08:00
//如果生成成功
2023-04-14 16:13:30 +08:00
else{
2023-04-17 13:52:24 +08:00
//告知用户"ok"
$rs['errmsg'] = "ok";
$rs['errcode'] = 0;
2023-04-17 13:54:45 +08:00
$rs['data'] = array();
2023-04-17 13:52:24 +08:00
$rs['data']['shorturl'] = $array['urls'][0]['surl'];
2023-04-14 16:13:30 +08:00
}
}
}
}
}
}
//输出结果
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
2023-04-07 19:05:18 +08:00
}
}
?>