php介面demo PHP介面開發規範

2021-10-22 22:05:18 字數 2373 閱讀 4918

引數傳遞方式:

所有引數key、value鍵值對,轉json然後base64(urlencode(json))轉碼,傳遞給介面。

php介面端使用 file_get_contents('php://input') 接收後解碼

php接收端

$data=file_get_contents('php://input');

$data=json_decode(urldecode(base64_decode($data)),true);

php傳送端

$ch = curl_init(); //用curl傳送資料給api

引數驗證規則:

* 判定是否是正常的訪問

* @deprecated 根據引數來進行判定訪問的合法性

function isvalidrequire($requestkey = '')

if (empty($requestkey)) {

$requestkey = config('default_request_key');

$tempparams = array();

array_push($tempparams, $requestkey);

$params = get_input();

$publickey = $params['public_key']; // 內定的驗證字段 publickey

unset($params['public_key']);

ksort($params);

foreach ($params as $key => $value) {

if (is_array($value)) {

$value = json_encode($value);

array_push($tempparams, $key . '=' . $value);

$str_token = hash('md5', implode('&', $tempparams));

if ($str_token != $publickey) {

$this->error('非法訪問');

return true;

* 生成訪問的安全驗證token

* $params 引數

function buildvalidtoken(array $params, $requestkey = '')

$tempparams = array();

if (empty($requestkey)) {

$requestkey = config('default_request_key');

array_push($tempparams, $requestkey);

if (!empty($params)) {

ksort($params);

foreach ($params as $key => $value) {

if (is_array($value)) {

$value = json_encode($value);

array_push($tempparams, $key . '=' . $value);

return hash('md5', implode('&', $tempparams));

function encode_data($arr)

return base64_encode(urlencode(json_encode($arr)));

function get_input()

$data = file_get_contents('php://input');

return json_decode(urldecode(base64_decode($data)), true);

python之POST介面與GET介面開發簡單例子

我所使用的是python3.6.5,django版本是2.0.5 get請求 建立引數字典 result username request.get.get username telnum request.get.get telnum date request.get.get date result ...

request mysql 介面 TP5介面開發

開啟debug除錯模式 正式上線建議關閉 config.php 應用除錯模式 設定輸出型別 index.php class index public function index data name steven age 24 return json code 0,msg 操作成功 data dat...

php介面是什麼,php什麼是介面

php介面 使用介面 inte ce 可以指定某個類必須實現哪些方法,但不需要定義這些方法的具體內容。介面是通過 inte ce 關鍵字來定義的,就像定義乙個標準的類一樣,但其中定義所有的方法都是空的。實現 implements 要實現乙個介面,使用 implements 操作符。類中必須實現介面中...