<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                _有句話說得好,最可怕的事情不是別人比你優秀,而是優秀的人竟然還比你更努力。 --《考拉小巫的留學成長日志》_ 此篇章主要是講述接口統一請求的方式,以及提供一個PHP實現的簡單客戶端。 ##1.13.1 指定接口服務:?service=XXX.XXX 我們統一固定用service參數來表示需要請求獲得的服務,并通過GET方式傳遞,即請求的URI格式為: ``` 接口域名 + 入口路徑 + ?service=XXX.XXX 如: http://dev.phalapi.com + /demo/ + ?service=User.GetBaseInfo ``` 當我們在瀏覽器以GET方式請求時,可以在nignx看到這樣的日志: ```javascript 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" ``` 如果通過接口用POST方式請求,則會看到: ```javascript 127.0.0.1 - - [07/Feb/2015:19:32:05 -0800] "POST /demo/?service=User.GetBaseInfo&sign= HTTP/1.1" 200 135 "-" "-" ``` 這里service的名稱,開頭不區分大小寫,建議統一以大寫開頭,以顯得專業。對應的接口是: ```javascript class Api_User extends PhalApi_Api { public function getBaseInfo() { } } ``` ##1.13.2 統一參數用GET 在一個項目中,會有很多公共的接口參數,如客戶端、版本號、密鑰等。這些同樣可以納入GET參數里面,或者也可以放到POST里面。 > 溫馨提示: > 這樣要求是有目的的,因為這樣的話可以在nginx的access日志里面查看來自客戶端的快照信息,以便統計或者定位問題。 ##1.13.3 接口參數用POST 特別地,接口參數我們建議統一使用POST方式傳遞,理由很簡單: + 1、相對保護上傳的數據,一如密碼; + 2、避免特殊字符或者過大數據包在GET下的限制; ##1.13.4 測試下的模擬參數 默認地,PhalApi框架會將$_REQUEST作為接口參數的來源: ```javascript DI()->request = 'PhalApi_Request'; ``` 當我們需要統一強制用$_GET,可以在init.php文件中這樣簡單定制: ```javascript DI()->request = new PhalApi_Request($_GET); ``` 同樣,也可以在init.php文件中強制用$_POST: ```javascript DI()->request = new PhalApi_Request($_POST); ``` 在測試環境下,為了模擬接口請求,我們需要人工提供接口參數,因此可以這樣輕松模擬: ```javascript $str = 'service=User.GetBaseInfo&user_id=1'; parse_str($str, $params); DI()->request = new PhalApi_Request($params); ``` ##1.13.5 PHP接口客戶端示例 先看下調用和使用的代碼示例: ```javascript <?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()); } ``` 附調用接口的客戶端源代碼: ```javascript //$ 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 = '后臺接口請求超時'; return $this->getData(); } $rs = json_decode($rs, true); if (isset($rs['data']['code']) && $rs['data']['code'] != 0) { $this->ret = self::RET_WRONG; $this->msg = '接口調用失敗[code =' . $rs['data']['code'] . ']' . ', 錯誤>信息:' . isset($rs['data']['msg']) ? $rs['data']['msg'] : '無'; 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; } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看