Как получить информацию о погоде с ipaddress

Я работаю в Yii+extjs. Я создаю модуль погоды. Я хочу получить информацию о погоде с IP-адреса пользователя. я получил ссылочный код с веб-сайта = "http://www.geoplugin.com/examples". Как упоминалось там, я создал функцию getweather as =

                  public function actionGetWeather()
        {
            $user_ip = $_SERVER['REMOTE_ADDR'];
            //The Data Science Toolkit URL
            $url = 'http://www.datasciencetoolkit.org/ip2coordinates/';
            //Find the user's location from their IP.
            //*** You need the get_data function from the sample code
            $raw_geocode = json_decode( get_data( $url . $user_ip) );
            //Check if the user is in the US
            if ('US' === $raw_geocode->$user_ip->country_code) {
                //If yes, store their zip code in a variable, and print it
                $zip_code = $raw_geocode->$user_ip->postal_code;
                printf('<p>Your zip code is: %s</p>', $raw_geocode->$user_ip->postal_code);
            } else {
                //If the user isn't in the US, set a sip code that will work.
                $zip_code = '97211';
                //and print an error
                printf('<p>Sorry, this app does not work in %s.</p>', $raw_geocode->$user_ip->country_name);
            }

            //Print the raw data for debugging.
            printf('<pre>%s</pre>', print_r($raw_geocode, true));
        }

я также включил класс ParseXml в свой проект. Но приведенный выше код выдает ошибку как неустранимая ошибка: вызов неопределенной функции get_data() в строке "$raw_geocode = json_decode(get_data($url. $user_ip));" функции getweather. Итак, какие изменения мне были нужны? Помогите пожалуйста мне


person user1636115    schedule 26.12.2012    source источник
comment
Вместо того, чтобы публиковать весь этот несвязанный код, просто сосредоточьтесь на реальной проблеме, которая get_data не определена.   -  person    schedule 26.12.2012


Ответы (1)


Если вы используете curl, включите определение get_data() как..

function get_data($url)
{
     $ch = curl_init();
     $timeout = 5;
     curl_setopt($ch,CURLOPT_URL,$url);
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
     curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
     $data = curl_exec($ch);
     curl_close($ch);
     return $data;
}

Или просто заменить

     $raw_geocode = json_decode( get_data( $url . $user_ip) );

с...

     $raw_geocode = json_decode(file_get_contents( $url . $user_ip) );
person sundeep    schedule 26.12.2012