簡體   English   中英

卷曲無效,錯誤:無法連接到主機

[英]Curl not working, error: couldn't connect to host

我正在使用curl從特定站點api.wunderground.com獲取天氣信息,問題是它無法正常工作。 我也嘗試過使用file_get_contents函數,但是它也不起作用。 這是我的curl代碼:

function get_web_page($url)
        {
            //echo "curl:url<pre>".$url."</pre><BR>";
        $options = array(
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_USERAGENT      => "spider", // who am i
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 15,      // timeout on connect
            CURLOPT_TIMEOUT        => 15,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
            CURLOPT_PROXY          => null,

        );                  

        $ch         = curl_init($url);
        curl_setopt_array( $ch, $options );
        $content    = curl_exec( $ch );
        $err        = curl_errno( $ch );
        $errmsg     = curl_error( $ch );
        $header     = curl_getinfo( $ch,CURLINFO_EFFECTIVE_URL );
        curl_close( $ch );

        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;

        //change errmsg here to errno
        if ($errmsg)
        {
            echo "CURL:".$errmsg."<BR>";
        }
        return $content;
        }

        $url        =   "http://api.wunderground.com/api/67927f145c532a19/geolookup/conditions/q/uae/dubai.json";

        get_web_page($url);

我已經檢查了服務器設置,啟用了curl功能,並且服務器正在使用端口80。有人可以幫我這個忙嗎?

您只需要回顯輸出即可。

echo get_web_page($url);

編輯:

您也可以使用file_get_contents

<?php
$url="http://api.wunderground.com/api/67927f145c532a19/geolookup/conditions/q/uae/dubai.json";
echo file_get_contents($url);
?>

代碼更新:

<?php
function get_web_page($url)
{    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $curl_result = curl_exec($ch);
    curl_close ($ch);
    return $curl_result;
}

$url="http://api.wunderground.com/api/67927f145c532a19/geolookup/conditions/q/uae/dubai.json";
echo get_web_page($url);
?>

我嘗試了相同的代碼並得到了回應

{“ response”:{“ version”:“ 0.1”,“ termsofService”:“ http://www.wunderground.com/weather/api/d/terms.html ”,“ features”:{“ geolookup”:1 ,“條件”:1}

上面的代碼是正確的。 您簡單地忘記打印函數返回的值

該API似乎阻止了可疑的用戶代理。 嘗試使用標准瀏覽器用戶代理而不是spider

對於普通用戶(瀏覽器的用戶代理),API返回普通響應。

也許您通過代理連接到Internet。可以通過在curl_setopt函數中設置代理來獲得它。它對我來說很好。

只需添加

        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_PROXY => false,

它解決了問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM