尝试操作从 cURL 调用返回的 json


Trying to manipulate the returned json from a cURL call

这个问题让我感到困惑。这是我的代码:

try {
    //API Url
    $url =   'https:<REST OF URL>';
    //Initiate cURL.
    $ch = curl_init($url);
    //The JSON data.
    $jsonData = array(
        'UserName' => '<HIDDEN>',
        'Password' => '<HIDDEN>'
    );
    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);
    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);
    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //Execute the request
    $content = curl_exec($ch);
catch(Exception $e) {   
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}

我使用浏览器运行 php 脚本,输出似乎是 json 格式,这就是我需要的。但是,当我添加 json 解码并尝试print_r或var_dump输出时,我只是得到一个布尔值(true) 或 int(1),它不想将数据放入数组中。同样奇怪且最有可能相关的是,上面的代码不使用print_r、var_dump甚至回显,但它仍然将 json 格式打印到屏幕上?

任何帮助将不胜感激。

编辑

以下是屏幕上显示的 json 输出:

 {"AvailableAdvisers":[{"AdviserId":"1345678","BusinessEntityName":"Bob Smith Finance","FullName":"Bob Smith"},{"AdviserId":"12345678","BusinessEntityName":"Globe Home Loans Pty Ltd","FullName":"Jane Doe"}],"FirstName":"Sam","LastName":"Sung","LendingPartnerStaffId":"12345674356","Locations":[{"LendingPartnerLocationId":"123467867647","Name":"Bank"},{"LendingPartnerLocationId":"12324354545","Name":"Jane Smith"}],"UserName":"Username"}

好吧,看来我明白你的意思了。

因此,要将 curl 响应放入变量$content您可能需要添加行:

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

在您curl_exec之前

但是卷曲响应如何出现在页面上的奇迹是真正的谜:-)