YurunDoc 接口合并請(qǐng)求

2018-10-12 13:20 更新

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

配置文件:Config/batch.php

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

// 設(shè)置跨域傳遞cookie,如果不需要可以去除
$.ajaxSetup({
    xhrFields: {
        withCredentials: true
    },
});
$.ajax({
    // 在線(xiàn)演示地址,隨時(shí)可能崩掉,正式使用時(shí)請(qǐng)改成你自己的
    url: 'http://apiagent.toolapi.net/batch.php',
    method: 'post',
    data: JSON.stringify({
        /*aip: {
            url: 'ip', // 在apis中預(yù)定義的接口,傳別名即可
            getData: {ip: '218.4.255.255'},
        },*/
        aip: {
            url: 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json',
            getData: {ip: '218.4.255.255'}, // 這里的IP改變也會(huì)改變下面baike的結(jié)果哦
        },
        // weather2是錯(cuò)誤的結(jié)果,會(huì)中斷
        /*weather2: {
            url: 'http://www.weather.com.cn/data/sk/1010101001.html',
            condition: { // 返回結(jié)果.weatherinfo不為null
                value: '{$weather2.weatherinfo}',
                regular: 'is not null',
            },
        },*/
        baike: {
            url: 'http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_length=600',
            // get參數(shù)
            getData: {
                bk_key: '{$aip.city}', // api接口中返回的數(shù)據(jù).city
            },
            // post參數(shù)
            postData: {},
            // GET請(qǐng)求數(shù)據(jù)類(lèi)型
            getDataType: 'form',
            // POST請(qǐng)求數(shù)據(jù)類(lèi)型
            bodyDataType: 'form',
            // 自定義header,不定義則使用默認(rèn)
            header: {
                'test': 'aaa',
            },
            // 驗(yàn)證返回結(jié)果是否正確,不正確會(huì)中斷請(qǐng)求并返回
            condition: {
                value: '{$baike.errno}',
                regular: 'is not null',
            },
        },
    }),
    success: function(data) {
        console.debug(data);
    }
});

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

{
    // 是否成功
    "success": true,
    // 錯(cuò)誤信息
    "message": "",
    // 返回正文數(shù)據(jù)
    "data": {
        "test": ""
    },
    // 返回結(jié)果
    "result": {
        // 鍵名=>返回頭數(shù)據(jù)
        "test": {
            // 狀態(tài)碼
            "status_code": 208,
            // 返回頭
            "header": {
                "Server": "squid/3.5.20",
                "Date": "Mon, 09 Oct 2017 07:38:54 GMT",
                "Content-Type": "application/octet-stream",
                "Content-Length": "0",
                "Connection": "keep-alive"
            },
            // 請(qǐng)求耗時(shí)
            "time": 0.047
        }
    }
}

預(yù)定義驗(yàn)證規(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

除了預(yù)定義規(guī)則,你還可以編寫(xiě)正則來(lái)驗(yàn)證。如:\d+

dataType數(shù)據(jù)類(lèi)型

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

不傳默認(rèn)為form,其它名稱(chēng)不對(duì)傳遞來(lái)的參數(shù)做任何處理

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)