[eval()与file_get_contents()..不同的结果

问题描述 投票:0回答:1

我正在尝试将eval()与json_decode(file_get_contents())一起使用,并且在评估和硬编码值时得到的结果似乎有所不同。我只是缺少一些简单的东西吗?

我正在使用PHP版本5.3.24,目前还不能更改。如果对eval()的支持至关重要,那么我可以开始进行更改的过程。我没有发现任何暗示我在当前的PHP实现中不支持eval()的信息。

当我运行Example1('key')时,我从eval()函数返回了NULL。当我运行Example2('key')时,我会返回一个基于json数据的数组,如下所示:

{ key_list: [ { data1_list: [ { subkey1: "data", subkey2: 0 }, .. ], .. ] }

这里是Example1():

function Example1($key) {

    $endPoint = 'http:'.'//some.website.com/json/'.$key;

    $evalCommand = sprintf('json_decode(file_get_contents("%s"))->%s_list[0];', $endPoint, $key);

    echo '$evalCommand = |'.$evalCommand.'|<br />';

    $resultsArray = eval($evalCommand);

    return $resultsArray;
}

这里是Example2():

function Example2($key) {

    $endPoint = 'http:'.'//some.website.com/json/'.$key;

    $resultsArray = json_decode(file_get_contents($endPoint))->key_list[0];

    return $resultsArray;
}
php json eval file-get-contents
1个回答
0
投票

而不是使用eval,它可以使用更多标准代码并使用动态对象访问。

URL的名称是可以的,但是对于该字段来说,有些杂耍。这在$field中设置了名称,但是我无法用它在一行中完成该部分和数组部分的操作。在这里,它使用->$field来获取数据,并且[0]在返回位置...

function Example2($key) {
    $endPoint = 'http://some.website.com/json/'.$key;

    $field = $key.'_list';
    $resultsArray = json_decode(file_get_contents($endPoint))->$field;

    return $resultsArray[0];
}
© www.soinside.com 2019 - 2024. All rights reserved.