2013-07-15 51 views
0

你好傢伙我想獲取這個YQL查詢的天氣預報的代碼元素,問題是這會讓我返回null .. 任何人都可以幫助,請e。我在PHP用PHP和json回溯YQL

     php 
      var_dump(getResultFromYQL()); 

     function getResultFromYQL() { 

      $yql_query_url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D946738&format=json&diagnostics=true&callback=?"; 

      $session = curl_init($yql_query_url); 
      curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

      $json = curl_exec($session); 
      curl_close($session); 

      return json_decode($json); 
     } 
     ?> 

OK是小白我找到了解決辦法 PHP

  $Jakarta = 946738; /* Jakarta */ 


      /* Use cURL to query the API for some XML */ 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, 'http://weather.yahooapis.com/forecastrss?w='.$Jakarta.'&u=f'); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      $weather_rss = curl_exec($ch); 
      curl_close($ch); 

      /* Create an object of the XML returned */ 
      $weather = new SimpleXMLElement($weather_rss); 

      /* 
      * Since I don't want to figure out an image system, I'll use Weather.com's (what Yahoo does) 
      * by pulling the image directly out of the returned API request. This could be done better. 
      */ 
      $weather_contents = $weather->channel->item->description; 
      // preg_match_all('/<img[^>]+>/i',$weather_contents, $img); 
      // $weather_img = $img[0][0]; 

      /* Get clean parts */ 

      $weather_cond = $weather->channel->item->xpath('yweather:condition'); 


      /* Function to convert Wind Direction from given degree units into Cardinal units */ 

      ?> 


          hp 

             print $weather_cond[0]->attributes()->code; 

           ?> 
        </div> 
       </div> 
      </div> 
      </div> 

回答

1

callback=?查詢部分。它旨在用於JavaScript回調函數。

工作URL應該是:$yql_query_url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D946738&format=json&diagnostics=true";

UPDATE

工作代碼:(PHP的27年3月5日和5.4.17測試)

<?php 
function getResultFromYQL() { 
    $yql_query_url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D946738&format=json&diagnostics=true"; 

    $session = curl_init($yql_query_url); 
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

    $json = curl_exec($session); 
    curl_close($session); 
    return json_decode($json); 
} 

var_dump(getResultFromYQL()); 
?> 
+0

我甚至有越來越再次返回null 'callback =?'刪除...有一些錯誤... – user2304067

+0

不幸的是不工作我得到NULL,你會得到什麼?我甚至複製粘貼你的代碼...我在php 5+服務器上測試 – user2304067

+0

我的代碼中的錯字。它現在應該工作。 – Fracsi