Herbert
Herbert

Reputation: 5645

curl_multi_select consistently fails for unknown reason

I created a php script to run some google queries in order to get acquainted with the concept of multiple parallel requests in curl. As a basis, I used example #1 on this page: http://php.net/manual/en/function.curl-multi-exec.php

I found that curl_multi_select in the provided example always returns -1. The documentation states that this indicates some error happened (by the underlying system call), but there seems to be no way to deduce what went wrong.

Code

$queries = array("Mr.", "must", "my", "name", "never", "new", "next", "no", "not", "now");

$mh = curl_multi_init();
$handles = array();
foreach($queries as $q)
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://www.google.nl/#q=" . $q);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_multi_add_handle($mh,$ch);
  $handles[] = $ch;
}

echo "created\n";

$active = null;
$mrc = curl_multi_exec($mh, $active);
if ($mrc != CURLM_OK)
  throw new Exception("curl_multi_exec failed");

while ($active && $mrc == CURLM_OK) {
  $select = curl_multi_select($mh);
  if ($select != -1) {
    do {
      $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
  } else throw new Exception("curl_multi_select failed (it returned -1)");
}

// removed cleanup code for briefety.

Question

How can I find out why curl_multi_select returns -1 or why does it return -1? Do I need some special configuration of php to allow for this multithreading?

Upvotes: 1

Views: 1080

Answers (1)

karakani
karakani

Reputation: 356

As a comment says at http://php.net/manual/en/function.curl-multi-init.php#115055, there's a problem on the official document.

I don't know why and don't have enough knowledge on libcurl, but I understand that curl_multi_select($mh) always have a chance to return -1;

So, this snippets (from the above url) works for me.

<?php
while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) == -1) {
        usleep(100);
    }
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
?>

Upvotes: 2

Related Questions