標籤:href 驗證 格式 form 自動 _id multipart 儲存 log
手機APP介面
介面:用戶端發送一個請求到伺服器
介面:1.URL地址:http://網域名稱/控制器/方法 或者 https://
2.test.php處理用戶端發送資料
3.資料返回(json,xml)
json_encode()將數群組轉換為字串
<xml version=’1’>
<root>
<username>張三</username>
</root>
介面:1.開放性介面
2.非開放性介面(例如:公眾號)需要api_token
使用者需要到介面平台上註冊一個介面帳號,介面平台會給開發人員一個用
戶uid和使用者密鑰secret
Api_token :md5(uid+secret+目前時間)
http://test.com/ api_token 到期時間
http://test.com/返回資料?Uid=xx & api_token=xx
User_token 使用者驗證是否登入
介面請求方式:get post<form></form>
測試載入器
用戶端發送請求內容格式:
1.請求資料格式:Content-type:application/json
請求資料:{‘username’:bobo,’password’:123456}
伺服器端:$data = file_get_content(“php://input”)
Json_decode($data,true) 解析json字串為數組
2.請求資料格式:Content-type:application/x-www-form-urlencoded
相當於form表單,Php會自動解析填充$_POST
請求資料:Username=bobo&password=123456
伺服器端擷取資料:1.$_POST
2.file_get_contents(‘php://input’)
3.伺服器端通過form表單上傳圖片時:
Content-type:multipart/form-data 圖片 二進位流格式
伺服器端:$xrl = file_get_contents(‘php://input’)
File_put_contents(‘圖片.jpg’,$xrl);圖片儲存到伺服器
伺服器端接收資料:
php://input 擷取來源於任何裝置的請求資料
File_get_contents(‘php://input’)
User_id = “wx”.md5(username.rand())
User_secret = md5(username.time().隨機數)
rand()產生隨機數
使用非公用介面
1.先到介面平台註冊帳號,介面平台分配使用者uid和使用者密鑰secret
2.根據使用者id和使用者密鑰secret產生一個api憑證 api_token
http://xxx.test.com/api/index/create_api_token?uid=xx&secret=xx
請求方式:get
傳回值:json格式 成功{“status”:200,”api_token”:xxx}
失敗{“status”:201}
備忘: api_token 有效期間
3.請求介面時,需要帶上api_token
http://xxx.test.com/api/index/get_goods_list?api_token=xxx
請求方式:get
傳回值:成功 {“status”=>200} 失敗
4.用戶端向伺服器端發送資料 api_token
http://xxx.test.com/api/index/add_goods?api_token=xxx
描述:添加商品資訊
資料格式:json {‘goods_name’:商品名稱,’goods_price’:商品價格}
請求方式:post
5.登入成功產生使用者憑證user_token
http://xxx.test.com/api/index/login?Api_token=xxx
資料格式:{“username”:使用者名稱,”password”:密碼} post請求
請求成功:{“status”:200m,”token”:使用者憑證,“user”:”使用者資訊”}
6.驗證使用者登入
http://xxx.test.com/api/index/getinfo?User_token=xxx&user_id=xxx
資料格式:json application/json
請求方式:post
非對外介面
1.建立一張儲存介面使用者(開發人員)的表 tb_api_user
2.Tb_api_token 用來驗證開發人員是否有使用介面的許可權
對外介面(公用介面)
http://www.test.com/ 或者 https://
返回到移動端的資料格式都是用json,是一種跨平台的資料格式
Json_encode(數組) 把數組轉化為json字串
json_decode(json字串,true) 把json字串轉換為數組
User_token 使用者憑證:用來驗證使用者是否登入
User_token = md5(uid.time().隨機數.網站密鑰(固定字串)) //唯一
移動端API開發