[英]PHP Calling my own JSON
<?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文件,但是每次尝试都出现此错误:
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();
?>
file_get_contents需要一个绝对URL,您正在使用一个相对URL。 如果脚本位于同一服务器上,为什么需要使用file_get_contents? 一个解决方案是删除输出缓冲,只require
index.php。
附带说明,由于您输出html(pre标记),因此将无法对响应进行json_decode。
file_get_contents需要完整的URL。 不只是文件路径。
例如:
$jsonurl = "http://www.yourwebsiteurl.co.uk/index.php?z=".$pro;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.