0

Why am I not getting a response when using multi curl? When making a single post request, everything works as expected. Is there something I am missing in the code below?

$urls = array(
  '', 
  'http://example.com', 
  'http://example.com',
  'http://example.com', 
  'http://example.com', 
  'http://example.com',
);
$url_count = count($urls);

$ch = array();
$mh = curl_multi_init();

for($i = 1; $i < $url_count; $i++) {
  
  $url = $urls[$i];
  $ch[$i] = curl_init($url);

  curl_setopt_array($ch[$i], array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'<?xml version="1.0" encoding="utf-8"?>
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
          <GetItemInfoPage xmlns="http://www.example.com/">
            <itemno></itemno>
            <custno></custno>
            <webdist></webdist>
            <prodcat>005</prodcat>
            <pageno>' . $i . '</pageno>
          </GetItemInfoPage>
        </soap:Body>
      </soap:Envelope>',
    CURLOPT_HTTPHEADER => array(
      'Content-Type: text/xml'
    ),
  ));

  curl_multi_add_handle($mh, $ch[$i]);
}

do {
    $status = curl_multi_exec($mh,$running);
} while(0 < $running);


for($i = 1; $i < $url_count; $i++) {
    $results[] = curl_multi_getcontent($ch[$i]);
}

var_dump($results);

this is what is returned:

array(5) { [0]=> string(1881375) "" [1]=> string(2751705) "" [2]=> string(3043756) "" [3]=> string(3306605) "" [4]=> string(2584404) "" }

Any help would be greatly appreciated.

jasone
  • 55
  • 7
  • Compare your while loop with the documentation example https://www.php.net/manual/en/function.curl-multi-init.php and the first user comment. – Markus Zeller Aug 27 '22 at 19:11
  • @MarkusZeller thanks for response but that did not change anything. – jasone Aug 27 '22 at 19:21
  • Did you close the handles before accessing the content? When I look at the "what is returned", it is not empty, you got all the results. None of them is empty. It looks like the var_dump cuts the display, because of its stings size. Do an `echo $results[0];` to look at the first one. – Markus Zeller Aug 27 '22 at 19:31
  • As I supposed, it will be too big for var_dump. As you can see `strlen($results[0])` is 1881375. – Markus Zeller Aug 27 '22 at 19:35
  • Why do you call this? $string is nowhere defined. What does the `echo $results[0];` output? – Markus Zeller Aug 27 '22 at 19:41
  • @MarkusZeller echo $results[0]; outputs nothing. – jasone Aug 27 '22 at 19:43
  • I can not believe there is no output. The var_dump clearly shows there's data inside. Put it in a file like `file_put_contents('result_0.html', $results[0]);` and open with an editor. – Markus Zeller Aug 27 '22 at 19:45
  • @MarkusZeller Well, that is strange... It did in fact write to file. So why when echoing, screen is blank? – jasone Aug 27 '22 at 19:47
  • Because it may output HTML/XML inside of HTML and mixing the doctypes will make the browser ignore "errors". You could try display the "empty" content with rightclick view source. – Markus Zeller Aug 27 '22 at 19:48

1 Answers1

1

As your var_dump() shows, the content is not empty.

array(5) { [0]=> string(1881375) "" [1]=> string(2751705) "" [2]=> string(3043756) "" [3]=> string(3306605) "" [4]=> string(2584404) "" }

The strings are too big, so they are not displayed or the content may break HTML/XML mixings.

If you want to see it in the browser you could escape it before outputting.

 echo htmlentities($results[0]);
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35