要在另一个文件中使用的JSON编码值

时间:2015-01-17 12:15:17

标签: php json

<?php

    echo $xml = file_get_contents("C:\wamp\www\Sample API\index.php");
    $result = json_decode($xml);
    var_dump ($result);

?>

的index.php:

<?php

    $servername = "localhost";
    $username = "root";
    $password = "";

    // Create connection
    $conn = new mysqli($servername, $username, $password);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    $db=mysqli_select_db($conn,"login");
    $sql ="SELECT * FROM loginuser WHERE ID=11"  or die('MySQL Error.'); 
    $result=$conn->query($sql);
    $data = array();
    while($data1 = $result->fetch_assoc()) 
    {
    $data[] = array('post'=>$data1);
    }

    $output = (array('posts' => $data));
    $out =json_encode($output);
    echo $out;

?>

我想在这个文件中获取index.php的输出值,我正在尝试这个给定的代码,但$ result返回null值。所包含的文件返回所需的值,但在这种情况下,json_decode返回null值

1 个答案:

答案 0 :(得分:0)

你可以改变这个:

echo $out;

为:

return $out;

然后你就可以将文件放到另一个文件中了:

$xml = require_once("C:\wamp\www\Sample API\index.php");
$result = json_decode($xml);
var_dump($result);