首页 » Ecshop, PHP » 如何制作Ecshop可用的移动app的api标准接口

如何制作Ecshop可用的移动app的api标准接口

14122 3

标题上加了Ecshop,其实也只是个噱头,增加搜索量而已,本文写的内容并不局限于Ecshop上。API接口,通常是供移动APP端调用的,制作api的前提是必须对业务逻辑和代码逻辑十分熟悉了,不然可能会事倍功半,甚至是中途夭折。

首先制作的语言仍旧是PHP,API的返回数据用的是JSON,没有用XML,为什么要用JSON而不用XML,这个问题,懂的人自然懂。先来创建JSON的model。

// 描述:内部使用API JSON类      
//  名称:json                    
//  作者:tiandi                  
//  版本:0.0.1                   
//  生成时间:2015.4.23           
//  修订时间:2015.4.23           
class json {
// status : string : 状态码 
// msg : string : 说明 
// content: array : 内容 
    var $status;
    var $msg;
    var $content;
function json(){
}

function set_status($status) {
$this->status = $status;
}

function set_msg($msg) {
$this->msg = $msg;
}

function set_content($content) {
$this->content = $content;
}

function create_json() {
$arr = array();
$arr['api_status'] = $this->status;
$arr['api_msg'] = $this->msg;
if($arr['api_status'] == '0') {
array_unshift($this->content,$arr);
echo urldecode(json_encode($this->content));
}
else
{
echo urldecode(json_encode($arr));
}
}

function check_env($request){
//check appid
if(!isset($request['appid'])) {
$this->set_status("99");
$this->set_msg("Need appid.");
echo $this->create_json();
exit;
}
elseif(!$this->compare($request['appid'],MY_APPID)) {
$this->set_status("98");
$this->set_msg("Appid is invalid.");
echo $this->create_json();
exit;
}
//check timestamp
elseif(!isset($request['timestamp'])) {
$this->set_status("97");
$this->set_msg("Need timestamp.");
echo $this->create_json();
exit;
}
//check sign
elseif(!isset($request['sign'])) {
$this->set_status("96");
$this->set_msg("Need sign.");
echo $this->create_json();
exit;
}
elseif(!$this->compare($request['sign'],$this->create_sign($request))) {
$this->set_status("95");
$this->set_msg("Sign is invalid.");
echo $this->create_json();
exit;
}

}

function compare($str1,$str2) {
if($str1 == "'".$str2."'" || $str1 == $str2 || "'".$str1."'" == $str2)
return true;
else
return false;
}

/************************** 生成签名 ***************************/

function create_sign($request) {
//签名方法
}

然后用下面方法生成json接口数据,$arr为数据库查询返回的数组。

$json->set_status("0");
$json->set_msg("success");
$json->set_content($arr);
$json->create_json();
文章评分5次,平均分3.4

本文原始地址:https://www.tiandiyoyo.com/2015/04/how-to-create-api-for-app-of-ecshop/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

评论前先开启评论开关:


3 Comments

  1. 你的内容不是 不局限于ecshop,是特么跟ecshop根本就没一点关系

  2. 既然用ecshop增加搜索量,就不能好好的写一点与ecshop有关的API?

载入分页评论...