api/app/controllers/UrlsController.php
2023-05-31 14:23:25 +08:00

107 lines
3.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Tiffany SMS API重构-接口类
* @author QZQ
*/
header("Content-Type:text/html; Charset=utf-8");
class UrlsController extends ControllerBase{
function initialize(){
parent::initialize();
}
/**
* 测试方法
* 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'] = "c82fee53ecc8aaa37b3e1584a51448f1";
$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;
}
/**
* 长链转短链Long URL shortening
* http://127.0.0.1/tiffany/api/urls/shorturl?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/urls/shorturl?qaz=wsx
*/
function shorturlAction(){
//如果是GET请求
if($this->request->isGet()){
//告知请求的资源不支持 http 方法“GET”。
$rs['Message'] = "请求的资源不支持 http 方法“GET”。";
}
//如果不是GET请求
else{
//校验是否有收到请求数据
$rs = $this->__check_request_arr();
//如果有收到请求数据
if(!empty($rs['data']['request_arr'])){
//获取请求数据
$request_arr = $rs['data']['request_arr'];
//准备校验基本参数是否都不为空
$params_arr[] = "appid";
$params_arr[] = "url";
$params_arr[] = "access_token";
//校验基本参数是否都不为空
$rs = $this->__check_params_arr($request_arr, $params_arr);
//如果基本参数都不为空
if(empty($rs['errcode'])){
//准备校验所传appid是否有效
$appid = $request_arr['appid'];
//校验所传appid是否有效
$rs = $this->__check_appid($appid);
//如果所传appid有效
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有效
if(empty($rs['errcode'])){
//设置生成长链接
$post_arr['urls'] = json_encode(array($request_arr['url']));
//设置生成TokenID
$post_arr['TokenID'] = WE_SURL_API_TIFFANY_TOKEN;
//准备生成短链接
$url = WE_SURL_API_GET_URL;
//获取生成结果
$json = $this->__http_post_request($url, $post_arr);
//转换生成结果
$array = json_decode($json, true);
//如果生成失败
if(!(strpos(strtolower($json), "ok")!==false)){
//告知用户"服务器异常,err detail:短链转换失败"
$rs['errmsg'] = "服务器异常,err detail:短链转换失败";
$rs['errcode'] = 50000;
$rs['data'] = null;
}
//如果生成成功
else{
//告知用户"ok"
$rs['errmsg'] = "ok";
$rs['errcode'] = 0;
$rs['data'] = array();
$rs['data']['shorturl'] = $array['urls'][0]['surl'];
}
}
}
}
}
}
//输出结果
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
}
}
?>