api/app/controllers/UrlsController.php
2023-04-17 13:54:45 +08:00

109 lines
3.5 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'] = "9d21508a7531430c843cff8063dccb1c";
$post_arr['access_token'] = "ycq9t62einm7usfah1vk4z38djxp5gwl";
$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->__get_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 = $request_arr['appid'];
//更新接口请求次数
$rs = $this->__update_app_info($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'] = "TH3UOn1Z4p7aJFS5q8eE6Kmt9xWQsocw";
//开发阶段转为使用测试TokenID
$post_arr['TokenID'] = "DAdlkwahhODpoiQPKwndkwaMDsdIN59P";
//准备生成短链接
$url = "https://weapp.wemediacn.com/we/surl/api/get";
//获取生成结果
$json = $this->__http_post_request($url, $post_arr);
//转换生成结果
$array = json_decode($json, true);
//如果生成失败
if(!(strpos(strtolower($json), "ok")!==false)){
//告知用户"系统繁忙,请稍候再试"
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$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);
}
}
?>