简体   繁体   中英

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:

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
    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. Why do you need to use file_get_contents if the script is on the same server? A solution would be to remove the output buffering and just require index.php.

A side note, you will not be able to json_decode the response since you output html (the pre tag).

file_get_contents requires the full url. not just the file path.

eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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