W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
有句話說(shuō)得好,最可怕的事情不是別人比你優(yōu)秀,而是優(yōu)秀的人竟然還比你更努力。 --《考拉小巫的留學(xué)成長(zhǎng)日志》
此篇章主要是講述接口統(tǒng)一請(qǐng)求的方式,以及提供一個(gè)PHP實(shí)現(xiàn)的簡(jiǎn)單客戶端。
我們統(tǒng)一固定用service參數(shù)來(lái)表示需要請(qǐng)求獲得的服務(wù),并通過(guò)GET方式傳遞,即請(qǐng)求的URI格式為:
接口域名 + 入口路徑 + ?service=XXX.XXX
如:
http://dev.phalapi.com + /demo/ + ?service=User.GetBaseInfo
當(dāng)我們?cè)跒g覽器以GET方式請(qǐng)求時(shí),可以在nignx看到這樣的日志:
127.0.0.1 - - [07/Feb/2015:22:46:46 -0800] "GET /demo/?service=User.GetBaseInfo&sign=&user_id=1 HTTP/1.1" 200 107 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0"
如果通過(guò)接口用POST方式請(qǐng)求,則會(huì)看到:
127.0.0.1 - - [07/Feb/2015:19:32:05 -0800] "POST /demo/?service=User.GetBaseInfo&sign= HTTP/1.1" 200 135 "-" "-"
這里service的名稱,開頭不區(qū)分大小寫,建議統(tǒng)一以大寫開頭,以顯得專業(yè)。對(duì)應(yīng)的接口是:
class Api_User extends PhalApi_Api {
public function getBaseInfo() {
}
}
在一個(gè)項(xiàng)目中,會(huì)有很多公共的接口參數(shù),如客戶端、版本號(hào)、密鑰等。這些同樣可以納入GET參數(shù)里面,或者也可以放到POST里面。
溫馨提示:
這樣要求是有目的的,因?yàn)檫@樣的話可以在nginx的access日志里面查看來(lái)自客戶端的快照信息,以便統(tǒng)計(jì)或者定位問(wèn)題。
特別地,接口參數(shù)我們建議統(tǒng)一使用POST方式傳遞,理由很簡(jiǎn)單:
每個(gè)請(qǐng)求都有自己的Header頭,獲取Header頭信息可以判斷請(qǐng)求來(lái)源或者是把一些敏感的內(nèi)容存放到Header頭信息中(如:加密后的參數(shù)),框架也提供了方便快捷獲取Header的方法:
DI()->request->getHeader("Host");
默認(rèn)地,PhalApi框架會(huì)將$_REQUEST作為接口參數(shù)的來(lái)源:
DI()->request = 'PhalApi_Request';
當(dāng)我們需要統(tǒng)一強(qiáng)制用$_GET,可以在init.php文件中這樣簡(jiǎn)單定制:
DI()->request = new PhalApi_Request($_GET);
同樣,也可以在init.php文件中強(qiáng)制用$_POST:
DI()->request = new PhalApi_Request($_POST);
在測(cè)試環(huán)境下,為了模擬接口請(qǐng)求,我們需要人工提供接口參數(shù),因此可以這樣輕松模擬:
$str = 'service=User.GetBaseInfo&user_id=1';
parse_str($str, $params);
DI()->request = new PhalApi_Request($params);
先看下調(diào)用和使用的代碼示例:
<?php
require_once './PhalApiClient.php';
$config = array(
'host' => 'http://dev.phalapi.com/demo',
'secrect' => '******'
);
$client = new PhalApiClient($config);
$rs = $client->request('User.GetBaseInfo', array('userId' => 1));
if ($client->getRet() == PhalApiClient::RET_OK) {
var_dump($rs);
} else {
var_dump($client->getMsg());
var_dump($client->getUrl());
}
附調(diào)用接口的客戶端源代碼:
//$ vim ./PhalApiClient.php
<?php
class PhalApiClient
{
protected $host;
protected $secrect = '';
protected $params = array();
protected $moreParams = array();
protected $url;
protected $ret;
protected $msg;
protected $data = array();
const RET_OK = 'OK';
const RET_WRONG = 'WRONG';
const RET_ERROR = 'ERROR';
public function __construct($config)
{
$this->host = rtrim($config['host'], '/') . '/';
$this->secrect = $config['secrect'];
}
public function request($service, $params = array(), $timeoutMs = 3000)
{
if (!empty($service)) {
$this->params['service'] = $service;
}
$this->params['sign'] = $this->encryptAppKey($params, $this->secrect);
$this->url = $this->host . '?' . http_build_query($this->params);
$this->moreParams = $params;
$rs = $this->doRequest($this->url, $params, $timeoutMs);
if ($rs === false) {
$this->ret = self::RET_ERROR;
$this->msg = '后臺(tái)接口請(qǐng)求超時(shí)';
return $this->getData();
}
$rs = json_decode($rs, true);
if (isset($rs['data']['code']) && $rs['data']['code'] != 0) {
$this->ret = self::RET_WRONG;
$this->msg = '接口調(diào)用失敗[code =' . $rs['data']['code'] . ']' . ', 錯(cuò)誤>信息:' . isset($rs['data']['msg']) ? $rs['data']['msg'] : '無(wú)';
return $this->getData();
}
$this->ret = intval($rs['ret']) == 200 ? self::RET_OK : self::RET_WRONG;
$this->data = $rs['data'];
$this->msg = $rs['msg'];
return $this->getData();
}
public function getRet()
{
return $this->ret;
}
public function getData()
{
return $this->data;
}
public function getMsg()
{
return $this->msg;
}
public function getUrl()
{
return $this->url . '&' . http_build_query($this->moreParams);
}
protected function encryptAppKey($params, $secrect)
{
return '';
}
protected function doRequest($url, $data, $timeoutMs = 3000)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $timeoutMs);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$rs = curl_exec($ch);
curl_close($ch);
return $rs;
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: