简体   繁体   中英

curl_multi_getcontent returns empty string

This question is very similar to PHP curl_multi_gecontent returns null , but I could not find a solution there. If I try to echo the result of the function, which should contain the request response, I get an empty string ("") instead.

Surely I am missing something wrong in my code but I can't put my finger on it. Can anyone help?

$id = "stuff";
$password = "mcmuffin";
$data = json_decode(file_get_contents('php://input'), true);
$ch = array();

// build the individual requests, but do not execute them
for($i = 0; $i < sizeof($data); $i++){
    $searchText = $data[$i];
    $type = "ligne3;pdi;voie;commune;cedexa";
    $word = "Contient";
    $option = "AND_OR_RES";
    $format = "json";
    $url = "http://somewebservice/service?chaineRecherche=".urlencode($searchText)."&typeRecherche=".urlencode($type)."&optionMot=".urlencode($word)."&optionRecherche=".urlencode($option)."&typeResultat=".urlencode($format)."&idClient=".urlencode($id)."&passwdClient=".urlencode($mcmuffin);


    $currentCurl = curl_init($url);
    curl_setopt($currentCurl, CURLOPT_HEADER, 0);
    curl_setopt($currentCurl, CURLOPT_RETURNTRANSFER, true);
    array_push($ch, $currentCurl);
}

// build the multi-curl handle, adding all $ch
$mh = curl_multi_init();
for($i = 0; $i < sizeof($ch); $i++){
    curl_multi_add_handle($mh, $ch[$i]);
}

// execute all queries simultaneously, and continue when all are complete
$active = null;
do {
  $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);


// all of our requests are done, we can now access the results
for($i = 0; $i < sizeof($ch); $i++){
    echo "bonjour"; //does output
    $response = curl_multi_getcontent($ch[$i]); //empty??
    echo json_encode($response);
}

//close the handles
for($i = 0; $i < sizeof($ch); $i++){
    curl_multi_remove_handle($mh, $ch[$i]);
}
curl_multi_close($mh);

Thanks

It was a proxy issue. This cURL option fixes the problem.

curl_setopt($ch, CURLOPT_PROXY, 'proxy url');

On a colleague's computer on a nearby network we received the proxy rejectal instead of an empty string. The reason why my computer would receive an empty string instead of the proxy message will forever remain a mystery.

If target redirects, you'll receive an empty response. Try:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Alternatively, if you already done but have set CURLOPT_MAXREDIRS , it may be more redirecting more times than you have allowed. Try increasing it.

Seemanual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM