简体   繁体   English

基于api记录计数响应的php递归函数

[英]php recursive function based on api record count response

I'm trying to pull data from several API requests into a single array (for display), and the API's limit the quantity of records I can pull at a time. 我正在尝试将来自多个API请求的数据提取到一个数组中(用于显示),并且API限制了我一次可以提取的记录数量。 Unfortunately I don't have enough client purchases to test my recursion, so I'm hoping someone can look at my test class to see if it should work. 不幸的是,我没有足够的客户购买量来测试我的递归,所以我希望有人可以看看我的测试班,看看它是否应该工作。

Here's what I have so far. 到目前为止,这就是我所拥有的。 The request method takes parameters $service and $page, and then increments $page depending on how many 'recsindb' there are. request方法接受$ service和$ page参数,然后根据有多少“ recsindb”来递增$ page。 For example, if recsindb = 50, then $page should increment 5 times with 10 records in each set. 例如,如果recsindb = 50,则$ page应该增加5次,每组10条记录。

Here is the code I have written: 这是我编写的代码:

$check = new testClass;

// API services to loop through
$services = array(
    "dns" => "domains/search.json",
    "webservices" => "webservices/search.json",
    "singledomainhostinglinuxus" => "singledomainhosting/linux/us/search.json",
    "singledomainhostinglinuxuk" => "singledomainhosting/linux/uk/search.json"
);

// foreach service, assign a key to identify the data in the display
foreach ($services as $key => $value) {
    $data[$key] = $check->getData($value);
}

// Let's see what we got
echo "<pre>" . print_r($data, TRUE) . "</pre>";

class testClass {

    function getData($api) {
        $fullurl = "https://myapipath/" . $api . "?" . $this->buildstring();
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_URL, $fullurl);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($ch);
            curl_close($ch);
        return json_decode($data, true);
    }

    // Array key => value pairs
    private $parts = array();

    public function add($key, $value) {
        $this->parts[] = array(
            'key' => $key,
            'value' => $value
        );
    }

    // Build the query string
    public function buildstring($separator = '&', $equals = '=') {
        $queryString = array();

        foreach ($this->parts as $part) {
            $queryString[] = urlencode($part['key']) . $equals . urlencode($part['value']);
        }

        return implode($separator, $queryString);
    }

    // recursive function
    public function request($service, $page) {
        $count = 10; // 10 records is the minimum allowed to request
        $this->add(array('no-of-records', $count));
        $this->add(array('page-no', $page));
        $data = $this->getData(array($service, TRUE));

        if ($data[0]['recsindb'] > $page * $count) {
            $data = $this->request($service, $page + 1);
        }
        return $data;
    }
}

I ended up creating a database and API service to simulate process. 我最终创建了一个数据库和API服务来模拟过程。 So far this request method with array_merge seems to be what I'm looking for: 到目前为止,这个带有array_merge的请求方法似乎是我正在寻找的东西:

// recursive function
public function request($service, $page) {
    $count = 10;
    $this->add('no-of-records', $count);
    $this->add('page-no', $page);
    $data = $this->getData($service);

    if ($data['recsindb'] > $page * $count) {
        $data = array_merge($data, $this->request($service, $page + 1));
    }
    return $data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM