如何从此 JSON 访问产品标题

-4 php json curl web-crawler var-dump

我已经检索了一些这样的数据:

object(stdClass)[1]
  public 'status' => string 'ok' (length=2)
  public 'data' => 
    object(stdClass)[3]
      public 'sort_data' => 
        object(stdClass)[2]
          public 'sort_column' => string 'order_item_id' (length=13)
          public 'sort_order' => string 'asc' (length=3)
      public 'pager' => 
        object(stdClass)[4]
          public 'page' => int 1
          public 'item_per_page' => int 50
          public 'total_page' => int 1
          public 'total_rows' => int 21
      public 'form_data' => 
        array (size=0)
          empty
      public 'items' => 
        array (size=21)
          0 => 
            object(stdClass)[5]
              public 'order_item_id' => int 323360064
              public 'order_id' => int 111179028
              public 'variant' => 
                object(stdClass)[6]
                  public 'id' => int 17586275
                  public 'seller_id' => int 186764
                  public 'site' => string 'digikala' (length=8)
                  public 'is_active' => boolean true
                  public 'is_archived' => boolean false
                  public 'title' => string 'Mug' (length=115)
                  public 'product' => 
                    object(stdClass)[7]
                      public 'id' => int 3634323
                      public 'category_id' => int 6289
                      public 'title' => string 'Mug model series' (length=40)
                      public 'shipping_nature_id' => int 1
                      ...
Run Code Online (Sandbox Code Playgroud)

这背后的代码在这里:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'site_url',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json, application/json;charset=UTF-8',
    'Authorization: {{token}}'
  ),
));

$response = curl_exec($curl);

var_dump(json_decode($response));
Run Code Online (Sandbox Code Playgroud)

现在我需要分别访问这些值中的每一个。例如我想得到public 'title' => string 'Mug model series'. 我怎样才能做到这一点?

小智 5

你可以告诉 json_decode() 返回一个数组而不是一个对象:

$response = json_decode($response,true);
Run Code Online (Sandbox Code Playgroud)