Home > front end >  Why is Multi Curl returning empty string?
Why is Multi Curl returning empty string?

Time:08-28

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.

CodePudding user response:

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]);
  • Related