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.

9
  • Compare your while loop with the documentation example php.net/manual/en/function.curl-multi-init.php and the first user comment. Commented Aug 27, 2022 at 19:11
  • @MarkusZeller thanks for response but that did not change anything. Commented Aug 27, 2022 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. Commented Aug 27, 2022 at 19:31
  • As I supposed, it will be too big for var_dump. As you can see strlen($results[0]) is 1881375. Commented Aug 27, 2022 at 19:35
  • Why do you call this? $string is nowhere defined. What does the echo $results[0]; output? Commented Aug 27, 2022 at 19:41

1 Answer 1

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

that did the trick... Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.