卷曲多个URL的API请求循环分页

时间:2019-12-24 14:19:02

标签: php api loops curl

我正在尝试使用cUrl_multi发出请求,它工作正常,但是如果找不到结果,我需要在API分页中进行循环。就我而言,是一个电子邮件地址。

我设法通过对一个URL的简单请求来做到这一点,但不能使用cUrl multi ...

API返回一个[“ pagination”] [“ current_cursor”]和[“ pagination”] [“ next_cursor”],我需要增加$ pagination_cursor。 如果有人可以帮助我,将不胜感激。

这是我的cUrl多重查询:

$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=d3147903-c987-4d16-bb08-404e2dc8eb8d&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=d24e3fe9-bc7a-43bc-ae98-2d9fb6e6006d&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=6bbc04b3-475a-4900-adf2-12ca0ab8a343&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=59bb5459-2d2c-4bd0-ba55-141a4a705987&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=6d5f6665-79c9-4279-ab24-81216cf6aa47&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=b121160a-c026-4c32-8916-796f6522f155&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=5b4c2487-1cb5-43b2-93d9-ca8448dfa29f&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=0ff6af42-d999-472a-9426-ee9904315e46&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=23fe0cc4-7db4-433e-ad66-6d126df705fb&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=a08934c1-ded4-416c-99e3-40ae0dec823f&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=c9c562d7-fda8-4ac5-a579-feac410b42dc&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=240af94e-c551-4c2c-8524-a4884c361f0d&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=ed11d2ed-6fa7-49d6-b18d-a03d02283252&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=750ecc8b-0c73-4895-a297-debdcb4c564c&cursor='.$pagination_cursor;
$urls[] = 'https://api.fountain.com/v2/applicants?api_token=*******************&funnel_id=52ecc855-2e44-441b-b8b1-900b07dbc424&stage_id=291ab46f-3d99-4e0d-be3b-b03e99bf9d97&cursor='.$pagination_cursor;

// Get cURL handles
foreach ($urls as $key => $url) {
$chs[$key] = curl_init();

// Set all your options for each connection here
curl_setopt($chs[$key], CURLOPT_URL, $url); 
}

//create the multiple cURL handle
$mh = curl_multi_init();

//add the handles
foreach ($chs as &$ch) {
curl_multi_add_handle($mh,$ch);
}   

$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($active > 0);

while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($active > 0);
}
}

 foreach ($chs as $url=>&$ch) {
 $rr = json_decode($ch, true);
 print_r($rr["pagination"]);

 curl_multi_remove_handle($mh, $ch);
 }

 curl_multi_close($mh);

这是我对单个请求执行的循环,它运行正常:

$find = 0;

$pagination_current = 0;

$pagination_last = 1;

$pagination_cursor; 


while( ($pagination_current != $pagination_last) && ($find==0) ){

    $c = curl_init();

    curl_setopt_array($c, array(

        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'https://api.fountain.com/v2/applicants?api_token=**********&funnel_id=***********************&stage_id=**************&cursor='.$pagination_cursor,
        CURLOPT_USERAGENT => 'Sample cURL Request'
    ));

    $r = curl_exec($c);
    $e = curl_error($c);

    if ($e) {

        $errMsg = "Oops, une erreur s'est produite. Veuillez réessayer";

    } else {

        $rr = json_decode($r, true);
        $email = $_POST["DriverMail"];

        foreach( $rr["applicants"] as $applic){ 
             if($applic["email"] == $email){

                print_r($applic["email"]); 

                $find=1;
                break;
            }
        }

        if( !empty($rr["pagination"]["current"])){

            $pagination_current = $rr["pagination"]["current"];

        } else {

            $pagination_current++;

        }

        if ( !empty($rr["pagination"]["last"])){
            $pagination_last = $rr["pagination"]["last"];

        }   

        $pagination_cursor = $rr["pagination"]["next_cursor"];
        print_r($url);

    }           
}

0 个答案:

没有答案