打印REST API GET调用的结果


Print the result of the REST API GET call

我正在使用PHP执行REST API GET调用。有什么简单的方法可以浏览$result并将其内容保存到数组中吗。

    <?php
        $url = '...';
        try 
        {
            $result = file_get_contents( $url );
        } 
        catch (Exception $e) 
        {
            die('ERROR: ' . $e->getMessage());
        }
        var_dump($result);
    ?>

这是一个输出(只是一些第一行):

字符串(7272)

您应该使用json_decode

<?php
    $url = '...';
    try 
    {
        $result = file_get_contents( $url );
        $obj = json_decode($result);
    } 
    catch (Exception $e) 
    {
        die('ERROR: ' . $e->getMessage());
    }
    echo '<pre>';
    var_dump($obj);
?>

<?php
    $url = '...';
    try 
    {
        $result = file_get_contents( $url );
        $array = json_decode($result, true);
    } 
    catch (Exception $e) 
    {
        die('ERROR: ' . $e->getMessage());
    }
    echo '<pre>';
    var_dump($arr);
?>