2012-03-22 79 views
0
<?php 
    $pro = $_GET['pro']; 
    $pro = mysql_real_escape_string($pro); 
    $jsonurl = "index.php?z=".$pro; 
    $json = file_get_contents($jsonurl) or die('Failed'); 
     $obj = json_decode($json); 
    echo $obj->{'name'}     
?> 

我試圖調用一個JSON文件,它是我的服務器上,但每次我嘗試我得到這個錯誤:PHP打電話給我自己的JSON

Warning: file_get_contents(index.php?z=1) [function.file-get-contents]: failed to open stream: No error in C:\wamp\www\dir\demo.php on line 5 
Failed 

我的索引文件的PHP:

<?php 
    ob_start(); 
     header("Access-Control-Allow-Origin: *"); 
     include 'server.php'; 
     include 'functions.php'; 
     //SQL and Vars Here 
     echo '<pre style="word-wrap: break-word; white-space: pre-wrap;">'.json_encode($array).'</pre>'; 
ob_flush(); 
?> 

回答

0

file_get_contents需要完整的網址。不只是文件路徑。

如:

$jsonurl = "http://www.yourwebsiteurl.co.uk/index.php?z=".$pro; 
1

的file_get_contents期望一個絕對URL,您使用的是相對的。如果腳本位於同一臺服務器上,爲什麼需要使用file_get_contents?一個解決方案是刪除輸出緩衝,只是require index.php。

一個方面說明,你將不能json_decode響應,因爲你輸出的HTML(前標籤)。