YurunDoc 接口合并請求

2018-10-12 13:20 更新

通過batch.php文件訪問,請求規(guī)則是POST提交json格式內(nèi)容。

配置文件:Config/batch.php

jQuery調(diào)用樣例代碼:

  1. // 設(shè)置跨域傳遞cookie,如果不需要可以去除
  2. $.ajaxSetup({
  3. xhrFields: {
  4. withCredentials: true
  5. },
  6. });
  7. $.ajax({
  8. // 在線演示地址,隨時可能崩掉,正式使用時請改成你自己的
  9. url: 'http://apiagent.toolapi.net/batch.php',
  10. method: 'post',
  11. data: JSON.stringify({
  12. /*aip: {
  13. url: 'ip', // 在apis中預定義的接口,傳別名即可
  14. getData: {ip: '218.4.255.255'},
  15. },*/
  16. aip: {
  17. url: 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json',
  18. getData: {ip: '218.4.255.255'}, // 這里的IP改變也會改變下面baike的結(jié)果哦
  19. },
  20. // weather2是錯誤的結(jié)果,會中斷
  21. /*weather2: {
  22. url: 'http://www.weather.com.cn/data/sk/1010101001.html',
  23. condition: { // 返回結(jié)果.weatherinfo不為null
  24. value: '{$weather2.weatherinfo}',
  25. regular: 'is not null',
  26. },
  27. },*/
  28. baike: {
  29. url: 'http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_length=600',
  30. // get參數(shù)
  31. getData: {
  32. bk_key: '{$aip.city}', // api接口中返回的數(shù)據(jù).city
  33. },
  34. // post參數(shù)
  35. postData: {},
  36. // GET請求數(shù)據(jù)類型
  37. getDataType: 'form',
  38. // POST請求數(shù)據(jù)類型
  39. bodyDataType: 'form',
  40. // 自定義header,不定義則使用默認
  41. header: {
  42. 'test': 'aaa',
  43. },
  44. // 驗證返回結(jié)果是否正確,不正確會中斷請求并返回
  45. condition: {
  46. value: '{$baike.errno}',
  47. regular: 'is not null',
  48. },
  49. },
  50. }),
  51. success: function(data) {
  52. console.debug(data);
  53. }
  54. });

接口合并請求返回結(jié)構(gòu)

  1. {
  2. // 是否成功
  3. "success": true,
  4. // 錯誤信息
  5. "message": "",
  6. // 返回正文數(shù)據(jù)
  7. "data": {
  8. "test": ""
  9. },
  10. // 返回結(jié)果
  11. "result": {
  12. // 鍵名=>返回頭數(shù)據(jù)
  13. "test": {
  14. // 狀態(tài)碼
  15. "status_code": 208,
  16. // 返回頭
  17. "header": {
  18. "Server": "squid/3.5.20",
  19. "Date": "Mon, 09 Oct 2017 07:38:54 GMT",
  20. "Content-Type": "application/octet-stream",
  21. "Content-Length": "0",
  22. "Connection": "keep-alive"
  23. },
  24. // 請求耗時
  25. "time": 0.047
  26. }
  27. }
  28. }

預定義驗證規(guī)則

代碼 含義
is null null === $value
is not null null !== $value
empty array 0 === count($value[0])
not empty array count($value[0]) > 0
true true === $value
false false === $value

除了預定義規(guī)則,你還可以編寫正則來驗證。如:\d+

dataType數(shù)據(jù)類型

名稱 含義
form 表單參數(shù)格式(如:a=1&b=2),默認
json 轉(zhuǎn)為json格式提交

不傳默認為form,其它名稱不對傳遞來的參數(shù)做任何處理

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號