Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于接收到的记录计数递归发出请求的API方法_Php_Joomla3.0 - Fatal编程技术网

Php 基于接收到的记录计数递归发出请求的API方法

Php 基于接收到的记录计数递归发出请求的API方法,php,joomla3.0,Php,Joomla3.0,我正在研究一个API集成,并试图找出实现这一点的最佳方法。API请求返回两个数组键='recsindb'和'recsonpage'。我最多可以请求500条记录('no-of-records=500','page no=1'),但如果有更多的记录,我需要再次请求('no-of-records=500','page no=2')。我正在将这些记录保存到joomla数据库中,因此我不太担心将两个数组集连接在一起-我只需要进行调用,插入它,然后再次执行 到目前为止,我在代码中的位置是这样的……这对前50

我正在研究一个API集成,并试图找出实现这一点的最佳方法。API请求返回两个数组键='recsindb'和'recsonpage'。我最多可以请求500条记录('no-of-records=500','page no=1'),但如果有更多的记录,我需要再次请求('no-of-records=500','page no=2')。我正在将这些记录保存到joomla数据库中,因此我不太担心将两个数组集连接在一起-我只需要进行调用,插入它,然后再次执行

到目前为止,我在代码中的位置是这样的……这对前500条记录很有效,但我需要一些帮助来弄清楚如何使下500条记录有效

function getData($api) {
    $this->add("auth-userid", $this->params->get('auth_user'));
    $this->add("api-key", $this->params->get('auth_key'));

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

/*
 * Methods used for building API URI 
 * For example, domain availability check allows multiple domain-name and tld pairs
 *
 * Example usage:
 * $qs->trigger('add', array('filter', '1'));
 * $qs->trigger('add', array('filter', '2'));
 * var_dump($qs->buildstring()); // filter=1&filter=2
 */

private $parts = array();

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

public function buildstring($separator = '&', $equals = '=') {
    $queryString = array();

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

    return implode($separator, $queryString);
}
然后为了得到数据,我会这样做:

JPluginHelper::importPlugin('authentication');
$dispatcher = JEventDispatcher::getInstance();
$profile = JUserHelper::getProfile();
// Process the api plugin.
$dispatcher->trigger('add', array('no-of-records', 500));
$dispatcher->trigger('add', array('page-no', 1));
$dispatcher->trigger('add', array('customer-id', $profile->profile['customerid']));
$data = $dispatcher->trigger('getData', 'contacts/search.json');
编辑: 仍在努力解决这个问题,因此我将上述方法引入到本地xampp环境中,而不是Joomla

$trigger = new customClass;
$page = 1;
$records = "records.txt";
$fh = fopen($records, 'a') or die("can't open file");
do {
    $trigger->add('no-of-records', 1);
    $trigger->add('page-no', $page);
    $data = $trigger->getData('billing/customer-transactions/search.json');
    fwrite($fh, print_r($data,true));
    $page++;
} while ($page * $data['recsonpage'] <= $data['recsindb']);

echo "<pre>" . print_r($data, true) . "</pre>";
$trigger=新的customClass;
$page=1;
$records=“records.txt”;
$fh=fopen($records,'a')或die(“无法打开文件”);
做{
$trigger->add('no-of-records',1);
$trigger->add('page-no',$page);
$data=$trigger->getData('billing/customer transactions/search.json');
fwrite($fh,print_r($data,true));
$page++;

}虽然($page*$data['recsonpage']我找到了为什么我8次得到1条记录的原因。我在循环中使用的方法不是每次都被重新创建,因此它只是添加到已经存在的内容上,因此API只查看第一组要响应的请求

我添加了一个方法来取消设置部件数组,并将其添加到循环的末尾…修复了

function end() {
    unset($this->parts);
}