phpjsoncurlweb-crawlervar-dump

How to access product title from this JSON


I have retrieved some data like this:

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
                      ...

And the code behind this goes here:

$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));

Now I need to access each of these values separately. For example I want to get public 'title' => string 'Mug model series'. How can I do that?


Solution

  • You can tell json_decode() to return an array instead of an object:

    $response = json_decode($response,true);