如何通过块中的curl请求来获取响应以获取响应

时间:2018-04-19 14:43:40

标签: php httprequest httpwebrequest php-curl

您好我正在开发一个项目,需要使用curl对存储在数组中的每个id点击一个url。然后每个请求的响应进一步用于执行一些计算。我面临的唯一问题是大小数组是巨大的,即约25000所以基本上我必须做出大约25000个卷曲请求,这是我认为是不可行所以我希望无论如何我可以将数组分成更小的块,并按块打击网址

  $array=[1,2,3,4,5,6,7,8,9,10........25000];
  $chunk1=[1,2,...1000];
  $chunk2=[1000,1001....2000];

等等

1 个答案:

答案 0 :(得分:0)

我希望这有助于解决您的要求

    <?php
    $input_array = array('a', 'b', 'c', 'd', 'e'); //  $array=[1,2,3,4,5,6,7,8,9,10........25000];

    print_r(array_chunk($input_array, 2)); //In your case $arraychunk=array(); $arraychunk=array_chunk($array,1000); you will find the chunked array ranging from $arraychunk[0] to $arraychunk[24] each of thousand values
    print_r(array_chunk($input_array, 2, true)); //In your case $arraychunk=array(); $arraychunk=array_chunk($array,1000,true); you will find the chunked array ranging from $arraychunk[0] to $arraychunk[24] each of thousand values here it preserves keys
    ?>


    //output

    Array
    (
        [0] => Array
            (
                [0] => a
                [1] => b
            )

        [1] => Array
            (
                [0] => c
                [1] => d
            )

        [2] => Array
            (
                [0] => e
            )

    )
    Array
    (
        [0] => Array
            (
                [0] => a
                [1] => b
            )

        [1] => Array
            (
                [2] => c
                [3] => d
            )

        [2] => Array
            (    
                [4] => e
            )

    )



     //You may use curl like this
        <?php 

    function fun($id){
    // create curl resource 
    $url='http://example.com?id=$id';
    $ch = curl_init(); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, $url); 

    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    // $output contains the output string 
    $output = curl_exec($ch); 

    // close curl resource to free up system resources 
    curl_close($ch);    

    }

    //similarly for all chunked arrays
    foreach($arraychunk[0] as $key=>$value)  {

        $values=$value;
        func($values);
    }
    ?>