2012-03-22 48 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(前标签)。