Home > Software engineering >  php showing error but all things are good
php showing error but all things are good

Time:01-22

please help me.

this is the php code:

<?php

$path_to_file = 'https://kamboz.000webhostapp.com/OurGrocery/OurItemsGet/all_items.php';
$file_contents = file_get_contents($path_to_file);

$json = json_decode($file_contents);

foreach ($json->data as $item) {
    echo $item->id;
}
?>

when i run this it shows this error:

Notice: Trying to get property 'data' of non-object in /storage/ssd4/665/18079665/public_html/OurGrocery/TESTING_FOLDER/testing.php on line 28

Warning: Invalid argument supplied for foreach() in /storage/ssd4/665/18079665/public_html/OurGrocery/TESTING_FOLDER/testing.php on line 28

this is the json link: https://kamboz.000webhostapp.com/OurGrocery/OurItemsGet/all_items.php

all things are good. checked it manually and use many websites for chcek json is valid or not. it is valid. but i think it is 'not a object' error. please anyone help me.

CodePudding user response:

The JSON generated on this page is, as per the previous comment, not valid. Using json_decode on the seemingly good string will yield NULL because of various faults in the encoding. There are various faults with the data which should have been fixed using json_encode though perhaps the newlines would require further tweaks beforehand.

Using a regex to replace certain patterns on the above (sortof) json string allows the data to be parsed correctly

// fetch the "broken" JSON string
$url='https://kamboz.000webhostapp.com/OurGrocery/OurItemsGet/all_items.php';
$content=file_get_contents( $url );

// patterns of "broken" json to look for
$pttns=array(
    '@\r\n\r\n"@m',
    '@\s?\r\n\s?(\w)@m',
    '@\r\n\s?"@m'
);
// replace matches with these
$replacements=array(
    '"',
    '$1',
    '"'
);
// do the string fudging
$content=preg_replace( $pttns, $replacements, $content );

// now decoding json **should** be ok (?)
$json=json_decode( $content );

if( is_null( $json ) )echo 'Bad JSON';
else{
    
    $data=$json->data;
    
    // process the json data however you need.
    printf('<h1>It works now... <span style="color:red">%s</span></h1>',$data[0]->name);
    
    foreach( $data as $i => $obj ){
        printf('<h2>Record: #%d</h2><pre>%s</pre>',$i,print_r($obj,true));
    }
}

This yields:

It works now... Colgate Active Salt Tooth Paste 100g TUBE
Record: #0
stdClass Object
(
    [id] => 1
    [img_url] => https://kamboz.000webhostapp.com/OurGrocery/img_product/1.png
    [name] => Colgate Active Salt Tooth Paste 100g TUBE
    [category] => Colgate Active Salt Tooth Paste 100g TUBE 
    [description] => good quality.
    [description_full] => this is Colgate Active Salt Tooth Paste 100g TUBE. you can use it in your daily life.
    [rating] => 5.0
    [discount] => 7
    [price] => 64.32
)
Record: #1
stdClass Object
(
    [id] => 2
    [img_url] => https://kamboz.000webhostapp.com/OurGrocery/img_product/2.png
    [name] => Colgate Active Salt 200gm TUBE
    [category] => Colgate Active Salt 200gm TUBE
    [description] => good quality.
    [description_full] => this is Colgate Active Salt 200gm TUBE. you can use it in your daily life.
    [rating] => 5.0
    [discount] => 4
    [price] => 111.82
)
Record: #2
stdClass Object
(
    [id] => 3
    [img_url] => https://kamboz.000webhostapp.com/OurGrocery/img_product/3.png
    [name] => Patanjali DISH WASH BAR
    [category] => Patanjali DISH WASH BAR shabun sabun sabn 
    [description] => good quality.
    [description_full] => this is Patanjali DISH WASH BAR. you can use it in your daily life.
    [rating] => 5.0
    [discount] => 5
    [price] => 9.99
)

None of that string fudging would be necessary if the JSON were properly generated initially and this does not constitute a robust solution - focus on creating valid json initally and the rest is simple.

  • Related