curl_multi_getcontent returns empty string

2019-09-14 01:34发布

问题:

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

回答1:

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.