Php curl error : Hostname was NOT found in DNS cache

1.7k views Asked by At

I have a problem with a curl request in my php script that i have uploaded on my dedicated webserver. I know that this curl request work fine, because i have already tested on my local machine. I think this the problem come from my apache configuration or on my DNS. But i am not sure.

When i use curl_setopt($ch, CURLOPT_VERBOSE, true); i get the following error message in my test_curlog.log on my dedicated server:

* Rebuilt URL to: http://example.org/
* Hostname was NOT found in DNS cache
* Hostname was NOT found in DNS cache

Here is the code that i use:

<?php
var_dump("MULTI CURL REQUEST");
$error_curl_log = __DIR__ . '/../../../../web/test_curlog.log';
$fp = fopen($error_curl_log, 'a+');

// Création des ressources cURL
$ch2 = curl_init();
$ch3 = curl_init();

// Définit l'URL ainsi que d'autres options

curl_setopt($ch2, CURLOPT_URL, "http://example.org");
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_VERBOSE, true); //For debug
curl_setopt($ch2, CURLOPT_STDERR, $fp);

curl_setopt($ch3, CURLOPT_URL, "https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world");
curl_setopt($ch3, CURLOPT_HEADER, 0);
curl_setopt($ch3, CURLOPT_VERBOSE, true); //For debug
curl_setopt($ch3, CURLOPT_STDERR, $fp);

// Création du gestionnaire multiple
$mh = curl_multi_init();

// Ajoute les deux gestionnaires
curl_multi_add_handle($mh, $ch2);
curl_multi_add_handle($mh, $ch3);

$active = null;
// Exécute le gestionnaire
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}
// Ferme les gestionnaires
curl_multi_remove_handle($mh, $ch2);
curl_multi_remove_handle($mh, $ch3);
curl_multi_close($mh);
die;

On my local machine i get this response on my browser: enter image description here

So i should get the same on my dedicated server but i get this error: Maximum execution time of 60 seconds exceeded because the curl request doesn't seem to find the url.

Any help will be appreciate.

1

There are 1 answers

0
Thomas Melcer On BEST ANSWER

Finally, i found the solution. It consist to add this condition in the while loop:

$mrc === CURLM_CALL_MULTI_PERFORM || $active

Like this:

$active = null;
// Exécute le gestionnaire
do {
    $mrc = curl_multi_exec($mh, $active);
    // Vérification des erreurs
    if($mrc > 0) {
        // Affiche le message d'erreur
        echo "ERREUR !\n " . curl_multi_strerror($mrc);
    }
} while ($mrc === CURLM_CALL_MULTI_PERFORM || $active);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

I don't know why but it works