Php curl error : Hostname was NOT found in DNS cache

Php curl error : Hostname was NOT found in DNS cache

我上传到专用网络服务器的 php 脚本中的 curl 请求有问题。我知道这个 curl 请求工作正常,因为我已经在我的本地机器上测试过。我认为这个问题来自我的 apache 配置或我的 DNS。但我不确定。

当我使用 curl_setopt($ch, CURLOPT_VERBOSE, true); 时,我在我的专用服务器上的 test_curlog.log 中收到以下错误消息:

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

这是我使用的代码:

<?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;

在我的本地计算机上,我在浏览器上收到以下响应:

所以我应该在我的专用服务器上获得相同的结果,但我收到此错误:Maximum execution time of 60 seconds exceeded 因为 curl 请求似乎没有找到 url。

任何帮助将不胜感激。

终于,我找到了解决办法。它包括在 while 循环中添加此条件:

$mrc === CURLM_CALL_MULTI_PERFORM || $active

像这样:

$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);
    }
}

我不知道为什么,但它有效