如何在php中同步多个curl_multi请求?

发布于 2024-11-02 16:31:50 字数 2875 浏览 1 评论 0原文

我想使用curl_multi来执行一些异步请求,但我想同时进行一组请求,因为当请求的响应满足我的需求时,我打算停止所有请求。这是我的代码:

//chunks the array into minor arrays
$chunks = array_chunk($likes, 8);
//Counter var
$cont = 0;
//Array of results
$ofertas = array();
foreach ($chunks as $likes)      
{
   $codCategoria = $categorias[$categoria];      
   //In this function 'getOfertas' is the multi_curl request
   $respostas = getOfertas($likes, $codCategoria);        
   //Iterates in the array of results of the multi_curl      
   foreach ($respostas as $json)      
   {   
       $resposta = json_decode($json, true);
       //If the json obtained is no empty     
       if ($resposta['num_and'] > 0)    
       {
           echo "cont: $cont <br>";
           //Increment counter and put the response into the array $ofertas
           $cont++;
           $i = array_rand($resposta['ofertas']);
           $oferta = $resposta['ofertas'][$i];
           array_push ($ofertas, $oferta);
           //If i have already 4 ou more results exit the loop, because i only need 4 of then
           if ($cont >= 4)
              break; 
       }

    }
}

return $ofertas;

这是 getOfertas() 内部的函数和curl_multi代码

function parallelGet($urls)
{
    print_r($urls);
    $res = array();
    // Create get requests for each URL
    $mh = curl_multi_init();
    foreach($urls as $i => $url)
    {
      $ch[$i] = curl_init($url);
      curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
      curl_multi_add_handle($mh, $ch[$i]);
      #echo "request ";
    }

    // Start performing the request
    do {
        $execReturnValue = curl_multi_exec($mh, $runningHandles);
    } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
        // Loop and continue processing the request
    while ($runningHandles && $execReturnValue == CURLM_OK) {
      // Wait forever for network
      $numberReady = curl_multi_select($mh);
      if ($numberReady != -1) {
        // Pull in any new data, or at least handle timeouts
        do {
          $execReturnValue = curl_multi_exec($mh, $runningHandles);
        } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
      }
    }

    // Check for any errors
    if ($execReturnValue != CURLM_OK) {
      trigger_error("Curl multi read error $execReturnValue\n", E_USER_WARNING);
    }

    // Extract the content
    foreach($urls as $i => $url)
    {
      // Check for errors
      $curlError = curl_error($ch[$i]);
      if($curlError == "") {
        $res[$i] = curl_multi_getcontent($ch[$i]);
      } else {
        print "Curl error on handle $i: $curlError\n";
      }
      // Remove and close the handle
      curl_multi_remove_handle($mh, $ch[$i]);
      curl_close($ch[$i]);
    }
    // Clean up the curl_multi handle
    curl_multi_close($mh);

    // Print the response data
    return $res;
}

i want to use curl_multi to perform some asynchronous requisitions, but i want to make a group of requisitions at the time, because when the response of the requisitions fullfills my needs i intend to stop the all the request. Here is my code:

//chunks the array into minor arrays
$chunks = array_chunk($likes, 8);
//Counter var
$cont = 0;
//Array of results
$ofertas = array();
foreach ($chunks as $likes)      
{
   $codCategoria = $categorias[$categoria];      
   //In this function 'getOfertas' is the multi_curl request
   $respostas = getOfertas($likes, $codCategoria);        
   //Iterates in the array of results of the multi_curl      
   foreach ($respostas as $json)      
   {   
       $resposta = json_decode($json, true);
       //If the json obtained is no empty     
       if ($resposta['num_and'] > 0)    
       {
           echo "cont: $cont <br>";
           //Increment counter and put the response into the array $ofertas
           $cont++;
           $i = array_rand($resposta['ofertas']);
           $oferta = $resposta['ofertas'][$i];
           array_push ($ofertas, $oferta);
           //If i have already 4 ou more results exit the loop, because i only need 4 of then
           if ($cont >= 4)
              break; 
       }

    }
}

return $ofertas;

Here is the function inside getOfertas() with the curl_multi code

function parallelGet($urls)
{
    print_r($urls);
    $res = array();
    // Create get requests for each URL
    $mh = curl_multi_init();
    foreach($urls as $i => $url)
    {
      $ch[$i] = curl_init($url);
      curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
      curl_multi_add_handle($mh, $ch[$i]);
      #echo "request ";
    }

    // Start performing the request
    do {
        $execReturnValue = curl_multi_exec($mh, $runningHandles);
    } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
        // Loop and continue processing the request
    while ($runningHandles && $execReturnValue == CURLM_OK) {
      // Wait forever for network
      $numberReady = curl_multi_select($mh);
      if ($numberReady != -1) {
        // Pull in any new data, or at least handle timeouts
        do {
          $execReturnValue = curl_multi_exec($mh, $runningHandles);
        } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
      }
    }

    // Check for any errors
    if ($execReturnValue != CURLM_OK) {
      trigger_error("Curl multi read error $execReturnValue\n", E_USER_WARNING);
    }

    // Extract the content
    foreach($urls as $i => $url)
    {
      // Check for errors
      $curlError = curl_error($ch[$i]);
      if($curlError == "") {
        $res[$i] = curl_multi_getcontent($ch[$i]);
      } else {
        print "Curl error on handle $i: $curlError\n";
      }
      // Remove and close the handle
      curl_multi_remove_handle($mh, $ch[$i]);
      curl_close($ch[$i]);
    }
    // Clean up the curl_multi handle
    curl_multi_close($mh);

    // Print the response data
    return $res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2024-11-09 16:31:50

你试过吗
http://www.phpclasses.org/package/4091 -PHP-Retrieve-multiple-pages-simultaneously.html

您应该扩展并实现回调事件的方法

/**
     * OnLoad callback event.
     *
     * @param string $url URL for downloading.
     * @param string $content Downloaded content.
     * @param array $info CURL session information.
     */
    protected abstract function onLoad($url, $content, $info);

did you try
http://www.phpclasses.org/package/4091-PHP-Retrieve-multiple-pages-simultaneously.html

you should extend and implement method for callback event

/**
     * OnLoad callback event.
     *
     * @param string $url URL for downloading.
     * @param string $content Downloaded content.
     * @param array $info CURL session information.
     */
    protected abstract function onLoad($url, $content, $info);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文