2012-02-07 53 views
2

不知道爲什麼,但我沒有從json調用中得到任何回覆。我回應了內容,並粘貼到我的瀏覽器的網址欄,它的工作原理。我需要解決一些域名問題嗎?從PHP進行JSON調用

$connect = open_db(); 
$result = mysql_query("SELECT search_id, search_term FROM search WHERE search_poi_id IS NULL"); 
while($row = mysql_fetch_assoc($result)){ 
    $call = "http://maps.google.com/maps/api/geocode/json?address=".$row["search_term"]."&sensor=false"; 
    $json = file_get_contents($call); 
    print_r($json); 
} 
close_db($connect); 
+0

是否*任何*網站的工作?端口80是否在您的服務器上被阻塞,用於您的網絡服務器? – Ryan 2012-02-07 15:04:59

+0

我的調試AJAX/PHP問題的解決方案是將所有的PHP代碼註釋掉,除了一個'echo('Hello World!');'然後試着讓JavaScript'alert()'迴應出。如果你能解決這個問題,那麼問題出現在你的PHP中。如果沒有,可能是您的JavaScript有錯誤 – 2012-02-07 15:08:29

+0

@watcher:OP已在使用'print_r'進行調試。 – Ryan 2012-02-07 15:09:56

回答

1

file_get_contents()可能並不總是適用於外部URL。從php.net:

如果fopen包裝已啓用,可以使用URL作爲該函數的文件名。有關如何指定文件名的更多詳細信息,請參閱fopen()。請參閱支持的協議和包裝以獲取有關各種包裝具有哪些功能的信息的鏈接,關於它們的用法的註釋以及它們可能提供的任何預定義變量的信息。

+0

你能鏈接到那個文檔嗎? – Ryan 2012-02-07 15:13:06

+0

我如何才能使其工作,或者如果他們是更好的方式,我將如何管理它? – 2012-02-07 15:23:10

+0

我試過... echo(file_get_contents('... google's address ...'));它沒有工作 – 2012-02-07 15:24:07

0

如果您想進一步處理數據,你可以做這樣的:

$data = array(); 
$connect = open_db(); 
$result = mysql_query("SELECT search_id, search_term FROM search WHERE search_poi_id IS NULL"); 
while($row = mysql_fetch_assoc($result)){ 
    $call = "http://maps.google.com/maps/api/geocode/json?address=".$row["search_term"]."&sensor=false"; 
    $data[] = json_decode(file_get_contents($call)); 
} 
close_db($connect); 

foreach($data as $d) 
    print_r($d); 

有了這個數據應該是一個數組,然後你可以做任何你想做的數組...

編輯:

如果file_get_contents()不工作,試試這個:

$data = array(); 
$connect = open_db(); 
$result = mysql_query("SELECT search_id, search_term FROM search WHERE search_poi_id IS NULL"); 
while($row = mysql_fetch_assoc($result)){ 
    $call = "http://maps.google.com/maps/api/geocode/json?address=".$row["search_term"]."&sensor=false"; 
    $handle = fopen($call, "rb"); 
    $contents = stream_get_contents($handle); 
    fclose($handle); 
    $data[] = json_decode($contents); 
} 
close_db($connect); 

foreach($data as $d) 
    print_r($d); 
+0

file_get_contents($ call)不會返回任何內容,但如果我將$ call的內容放入瀏覽器的url欄中,它將返回json代碼。 – 2012-02-07 15:22:26

+0

阿哈,我以爲file_get_contents()返回......將編輯我的帖子來嘗試其他方法。 – shadyyx 2012-02-07 15:33:48

+0

$ call =「http://maps.google.com/maps/api/geocode/json?address=&sensor=false」;返回{「results」:[],「status」:「ZERO_RESULTS」}其中as $ call =「http://maps.google.com/maps/api/geocode/json?address=TS25 4HQ&sensor = false」;除了當它被放置在瀏覽器中時我什麼也沒有返回,我得到了json代碼! – 2012-02-07 15:44:59