简体   繁体   English

PHP调用我自己的JSON

[英]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'}                 
?>

I'm trying to call a JSON file that is on my server, but everytime I try I get this error: 我正在尝试调用服务器上的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

My index file PHP: 我的索引文件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 expects an absolute URL, you're using a relative. file_get_contents需要一个绝对URL,您正在使用一个相对URL。 Why do you need to use file_get_contents if the script is on the same server? 如果脚本位于同一服务器上,为什么需要使用file_get_contents? A solution would be to remove the output buffering and just require index.php. 一个解决方案是删除输出缓冲,只require index.php。

A side note, you will not be able to json_decode the response since you output html (the pre tag). 附带说明,由于您输出html(pre标记),因此将无法对响应进行json_decode。

file_get_contents requires the full url. file_get_contents需要完整的URL。 not just the file path. 不只是文件路径。

eg: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM