Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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 Curl api post请求只返回25个结果,但可能还有更多结果_Php_Api_Curl_Web Scraping_Php Curl - Fatal编程技术网

PHP Curl api post请求只返回25个结果,但可能还有更多结果

PHP Curl api post请求只返回25个结果,但可能还有更多结果,php,api,curl,web-scraping,php-curl,Php,Api,Curl,Web Scraping,Php Curl,我想从网站上搜集一些有用的数据。但是请求只返回了25个结果 为此: $url = 'https://api.test.org'; $ch = curl_init(); $jsonData = array( 'limit' => 100, //user inputs pages * 5 'listType' => 'taskSolutions', 'task' => $taskid //taken from

我想从网站上搜集一些有用的数据。但是请求只返回了25个结果

为此:

    $url = 'https://api.test.org';
    $ch = curl_init();

    $jsonData = array(
        'limit' => 100, //user inputs pages * 5
        'listType' => 'taskSolutions',
        'task' => $taskid //taken from input user substr($_POST['link'],28);
        //'skip' => 25 $variable that increases by 25
    ); 

    curl_setopt($ch, CURLOPT_URL, $url);
    $jsonDataEncoded = json_encode($jsonData);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); // loop adding 25 each time to skip
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = json_decode(curl_exec($ch),true);
现在我看了一下网站,他们有一个参数“skip”来获得更多的结果

但现在来问一个问题:

如何创建一个循环,将25添加到skip$变量,并重新发送CURLOPT_POSTFIELDS,然后将该数据添加到$data


变量$totalcount可用于检查有多少记录。

您可以通过循环来完成此操作。例如,将上述代码放入名为getData的函数中,并向其传递两个参数:$skip和$taskId:

函数getData($skip,$taskid) { $url='1https://api.test.org'; $ch=curl_init(); $jsonData=数组( '限制'=>100,//用户输入页*5 'listType'=>'taskSolutions', 'task'=>$taskid//取自输入用户substr($\u POST['link'],28); “skip”=>$skip ); curl_setopt($ch,CURLOPT_URL,$URL); $jsonDataEncoded=json_encode($jsonData); 卷曲设置($ch,卷曲设置桩,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonDataEncoded);//循环每次添加25以跳过 curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/json')); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 返回json_decode(curl_exec($ch),true); } 然后,您可以编写一个循环,将$skip变量递增25,直到它达到$totalCount。在每次迭代中,将返回的元素添加到$data数组中:

$data=[];
对于($skip=0;$skip<$totalCount;$skip+=25)
{
foreach(getData($skip,$taskid)作为$entry)
{
$data[]=$entry;
}
}