2012-12-26 99 views
0

我正在Yii + extjs工作。我正在創建天氣模塊。我想從用戶的ipaddress中檢索天氣信息。我從網站=「http://www.geoplugin.com/examples」獲得參考代碼。 按提到那裏,我創造的GetWeather功能在我的項目=如何從ipaddress檢索天氣信息

    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功能。那麼我需要什麼改變?如果找你使用curl請幫我

+0

而不是發佈所有這些不相關的代碼,只關注「真正的問題」,即'get_data'沒有被定義。 – 2012-12-26 06:10:02

回答

0

,包括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));