2017-01-04 53 views
2

我正在做一个Ajax调用从我的网页,以获取从JSON格式的getWeather.php页面的响应。有时我会得到结果,但大多数情况下我没有。但是每当我浏览这个链接,它总是给我JSON格式的结果。我在getWeather.php中做错了什么?PHP:雅虎天气Api获取空结果与file_get_content

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%27lahore%27)%20AND%20u%3D%27c%27&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback= 

getWeather.php实现:

<?php 
$city = $_GET['city']; 

echo file_get_contents("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%27".$city."%27)%20AND%20u%3D%27c%27&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="); 


?> 

{ “查询”:{ “计数”:0, “创建”: “2017-01-03T04:50:27Z”,” lang“:”en-US“,”results“:null}}

+0

确保城市不是空的,否则这段代码是好的,并工作。 –

+0

我甚至在测试的查询中写了城市名称,但有时我得到的结果,有时我不是。 – MTA

回答