如何使用PHP访问嵌套的JSON对象

时间:2017-07-20 01:51:12

标签: php json object

我通过访问API的登录生成了这个嵌套对象。 每个用户登录的“令牌”将有所不同。要访问API的其他部分,用户必须先登录。为此,需要“令牌”值。

所以现在我试图从嵌套对象中提取“Token”值并将其放在POST请求的标题部分。知道如何访问嵌套对象吗?

非常感谢。

{
  "Data": {
    "Id": 2,
    "Token": "2:BxeFG9E28lnG5gB5GiB1NgxXBPGjTl",
    "Name": "Administrator",
    "ClinicID": 0,
    "Admin": true,
    "UserAccessRight": {
      "id": 1,
      "dateCreated": "0001-01-01T00:00:00",
      "lastUpdated": "0001-01-01T00:00:00",
      "userid": 2,
      "waitingQueue": 2,
      "patient": 2,
      "appointment": 2,
      "consultation": 2,
      "dispensary": 2,
      "inventory": 2,
      "report": 2,
      "setting": 2
    }
  },
  "Success": true
}

我试过这个但是失败了

<?php require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;

$client = new Client([
 'headers' => [ 'Content-Type' => 'application/json' ]
 ]);

$response = $client->post('http://localhost:81/Login',
 ['body' => json_encode(
    [
        "username" => "admin",
        "Password" => "123456"
    ]
    )]
   );

  echo '<pre>' . var_export($response->getStatusCode(), true).'</pre>';
  echo '<pre>' . var_export($response->getBody()->getContents(), 
   true) . '</pre>';


      $object = '<pre>' . var_export($response->getBody()->getContents()
     , true) . '</pre>';
      echo $object;

     $yummy = json_decode("$object");

      echo $yummy['Data'][1]->Token; 

0 个答案:

没有答案