简体   繁体   中英

PHP cURL With a Loop

I am attempting to integrate a web service where the JSON response returns a URL for the next 1,000 results. What I have to do is loop the cURL to continue to process the request until the "next" variable is empty.

I am having an issue getting this done because I can not loop back up once i see the variable returned.

$url = "https://xxxxxxxxxxxxxx.com/process/api/transactions/from/$yesterday/to/$today/for/company/21?offset=0&limit=1000"; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);                                                              
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                                                                                    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: $cookie"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);                                                                                                                                            

$result = curl_exec($ch);

file_put_contents(rj_array, $result, FILE_APPEND);

$server = file_get_contents(rj_array);

$json_result = json_decode($server, true);

$next = $json_result['next'];

I was thinking of doing something like:

while(!empty($next)){
//Do the curl request again with the new URL $next
}

But I can not reset the $next variable each time. Does anyone have any ideas?

Why not

$base_url = "https://xxxxxxxxxxxxxx.com/process/api/transactions/from/$yesterday/to/$today/for/company/21?limit=1000";
$next = 0;
while( $next !== false ) {
    $url = $base_url . "&offset=" . $next;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);                                                              
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                                                                                    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: $cookie"));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);                                                                                                                                            

    $result = curl_exec($ch);

    file_put_contents(rj_array, $result, FILE_APPEND);

    $server = file_get_contents(rj_array);

    $json_result = json_decode($server, true);

    $next = $json_result['next'];
    if( /*test if next is empty*/ ) {
        $next = false;
    }
}

I do like the recursive function approach:

function crawler($url, $return) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: $cookie"));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    $result = curl_exec($ch);

    file_put_contents(rj_array, $result, FILE_APPEND);

    $server = file_get_contents(rj_array);

    $json_result = json_decode($server, true);
    $return[$url] = $json_result; //set your return information in some array

    $next = $json_result['next'];

    if(!empty($next)) {
        return crawler($next, $return); //call again same function with next url and array of data that you would use later
    }

    return $return;
}

$url = "https://xxxxxxxxxxxxxx.com/process/api/transactions/from/$yesterday/to/$today/for/company/21?offset=0&limit=1000";
$allJsonResults = crawler($url, []);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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