[英]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.