简体   繁体   中英

Extract online specific value from JSON

Hello I have a script which gets the JSON output in an array but I want to extract only the name field. It is acutally HostBill API, and I just want to print the domain TLDs. I tried all the ways but I am getting this error Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\hostbill\\check\\index.php on line 22

I don't know whats wrong with the code.

My required Output is: Product Name: ".com"; Which comes from this JSON as this "name": ".com",

My PHP code is:

<?php
   $url = 'http://localhost/hostbill/admin/api.php';
   $post = array(
      'api_id' => '4c76ed9e1d48cc4f5d56',
      'api_key' => '2056c9b03cc95ce625b1',
      'call' => 'getProducts',
      'id'=>1
   );
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_TIMEOUT, 30);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
   $data = curl_exec($ch);
   curl_close($ch);
   $return = json_decode($data, true);
   print_r($return);
   $product_name = $return->name;
   echo "Product Name: ".$product_name;
?>

Hostbill JSON Response Sample:

   {
      "success": true,
      "products": {
         "236": {
            "id": "236",
            "type": "9",
            "name": "VPS-1024-w-sliders",
            "stock": "0",
            "paytype": "Free",
            "description": "",
            "m_setup": "0.00",
            "q_setup": "0.00",
            "s_setup": "0.00",
            "a_setup": "0.00",
            "b_setup": "0.00",
            "t_setup": "0.00",
            "d_setup": "0.00",
            "w_setup": "0.00",
            "h_setup": "0.00",
            "m": "30.00",
            "q": "0.00",
            "a": "0.00",
            "s": "0.00",
            "b": "0.00",
            "t": "0.00",
            "d": "0.00",
            "w": "0.00",
            "h": "0.00",
            "qty": "0",
            "visible": "1",
            "ptype": "onappcloud",
            "accounts": "1"
         },
         "235": {
            "id": "235",
            "type": "9",
            "name": "VPS-512-w-sliders",
            "stock": "0",
            "paytype": "Regular",
            "description": "",
            "m_setup": "0.00",
            "q_setup": "0.00",
            "s_setup": "0.00",
            "a_setup": "0.00",
            "b_setup": "0.00",
            "t_setup": "0.00",
            "d_setup": "0.00",
            "w_setup": "0.00",
            "h_setup": "0.00",
            "m": "20.00",
            "q": "0.00",
            "a": "0.00",
            "s": "0.00",
            "b": "0.00",
            "t": "0.00",
            "d": "0.00",
            "w": "0.00",
            "h": "0.00",
            "qty": "0",
            "visible": "1",
            "ptype": "onappcloud",
            "accounts": "3"
         }
      },
      "call": "getProducts",
      "server_time": 1317712858
   }

Products are in array , so use foreach to iterate over all products.

foreach($return['products'] AS $product){
    echo "Product Name: ".$product['name'];
}

You should also check if API response is valid. If API dosen't respond with array of products or responds with empty response, you would get error. This would do it.

if(isset($return['products']) && is_array($return['products'])){
    foreach($return['products'] AS $product){
        echo "Product Name: ".$product['name'];
    }
}

Extract only specific category

To get only certain products, just use if statement. For example to get products from category_id 1 and type 9 :

foreach($return['products'] AS $product){
    if($product['category_id'] == 1 && $product['type'] == 9){
        echo "Product Name: ".$product['name'];
    }
}

You will get that error because it is not an object. It's an array.

So it should be:

 foreach($return['products'] as $key => $product){
  echo "Product Name: ".  $product['name'];
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM