How get data from url to array? - php
I use php pretty badly. I'm not a programmer just doing something for my private things.
I have such a problem I would like to download data from my PV production. The data is in the form as below. How to download data from a url in php, respectively, to group them and send them to the array ??
Thank you in advance for all your help.
{"sid":62923,"dataunit":"kWh","data":[{"time":"2019-08-01","no":"1","value":"27.7"},{"time":"2019-08-02","no":"2","value":"24.0"},{"time":"2019-08-03","no":"3","value":"19.9"},{"time":"2019-08-04","no":"4","value":"25.3"},{"time":"2019-08-05","no":"5","value":"0.0"},{"time":"2019-08-06","no":"6","value":"0.0"},{"time":"2019-08-07","no":"7","value":"0.0"},{"time":"2019-08-08","no":"8","value":"0.0"},{"time":"2019-08-09","no":"9","value":"0.0"},{"time":"2019-08-10","no":"10","value":"0.0"},{"time":"2019-08-11","no":"11","value":"0.0"},{"time":"2019-08-12","no":"12","value":"0.0"},{"time":"2019-08-13","no":"13","value":"0.0"},{"time":"2019-08-14","no":"14","value":"0.0"},{"time":"2019-08-15","no":"15","value":"0.0"},{"time":"2019-08-16","no":"16","value":"0.0"},{"time":"2019-08-17","no":"17","value":"0.0"},{"time":"2019-08-18","no":"18","value":"0.0"},{"time":"2019-08-19","no":"19","value":"0.0"},{"time":"2019-08-20","no":"20","value":"0.0"},{"time":"2019-08-21","no":"21","value":"0.0"},{"time":"2019-08-22","no":"22","value":"0.0"},{"time":"2019-08-23","no":"23","value":"0.0"},{"time":"2019-08-24","no":"24","value":"0.0"},{"time":"2019-08-25","no":"25","value":"0.0"},{"time":"2019-08-26","no":"26","value":"0.0"},{"time":"2019-08-27","no":"27","value":"0.0"},{"time":"2019-08-28","no":"28","value":"0.0"},{"time":"2019-08-29","no":"29","value":"0.0"},{"time":"2019-08-30","no":"30","value":"0.0"},{"time":"2019-08-31","no":"31","value":"0.0"}]}
Try this
<?php
$json_url = file_get_contents('Your url goes here');
$data = json_decode($json_url,true);
var_dump($data);
// You can print all of your data here to see if it works
foreach ($data as $items) {
// You can loop trough the data and get it like $items->sid etc
var_dump($items);
}
?>
You need to grab the URL with 'file_get_contents' and than you need to decode it with json_decode
and then you print it out with the for each function
Related
how to print all first parameter json
how to get all data i have some problem when i want to print my link in database and the format data in database is saved from json_encode() the data is ([{"link":"google.com"},{"link":"facebook.com"},{"link":"instagram.com"}]), and i want to print as google.com, facebook.com, instagram.com in my web, i use codeigniter framework i have tried this $link = json_decode($row['LINK'], true); $link[0]["link"]; and the result just google.com
You can do this way to get the domain link, <?php $row = '[{"link":"google.com"},{"link":"facebook.com"},{"link":"instagram.com"}]'; $array = json_decode($row,true); foreach($array as $key=>$value){ echo $value['link'].PHP_EOL; } ?> DEMO: https://3v4l.org/dElvA OR array_colunn() to get all the domains in an array, print_r(array_column($array,'link'));
How to extract data from this code (never seen this format before)?
I am using a plugin in my wordpress that stores my data in a specific way. I have a problem on how to extract that data into an array using php. The data currently saved in database is in this kind of format which i have never seen before. I want to get the name, price and the referral amount from this data. I have tried using php foreach looping to extract data but it wont recognize the foreach function. a:2:{i:0;a:4:s:4:"name";s:14:"Tower";s:2:"id";i:4177;s:5:"price";i:500;s:15:"referral_amount";s:3:"20";}i:1;a:4:s:4:"name";s:25:"Square";s:2:"id";i:3998;s:5:"price";i:178;s:15:"referral_amount";s:4:"87.4";}} I wanted the array to store the data in this kind of way: { name:'Tower', price:500, referral_amount:20, } Does anyone knew how to extract this kind of data using php?
$data = 'a:2:{i:0;a:4:{s:4:"name";s:14:"Tower";s:2:"id";i:4177;s:5:"price";i:500;s:15:"referral_amount";s:3:"20";}i:1;a:4:{s:4:"name";s:25:"Square";s:2:"id";i:3998;s:5:"price";i:178;s:15:"referral_amount";s:4:"87.4";}}'; $data = preg_replace_callback('!s:(\d+):"(.*?)";!', function ($match) { return 's:'.strlen($match[2]).':"'.$match[2].'";'; }, $data); $data_array = unserialize($data); $final_data = json_encode($data_array); echo $final_data;
Httpful JSON inside an array
I've been trying for 3 days to get PHP to show the ID from this test JSON using PHP and httpful. Anyone have any ideas as i've tried loads of different combinations and even tried created a handler to decode as an array... I just suck at PHP ? // Make a request to the GitHub API with a custom // header of "X-Trvial-Header: Just as a demo". <?php include('\httpful.phar'); $url = "https://jsonplaceholder.typicode.com/posts"; $response = Httpful\Request::get($url) ->expectsJson() ->send(); echo "{$response[0]['id']}"; ?> My output... still nothing
You are not properly indexing the return value. The response is a mixed value. So you should do something like below to get what you are looking for. <?php include('\httpful.phar'); $url = "https://jsonplaceholder.typicode.com/posts"; $response = Httpful\Request::get($url) ->expectsJson() ->send(); echo $response->body[0]->id; ?>
Trouble outputting json to php
One of my sources for data recently changed how they are providing a json file to me, they added something before the actual output, and I am having trouble getting the values to display on my landing page. Old json output string(6596) "[{"id":239,"solution_id":3486," etc... New json output string(6614) "{"picker_offers":[{"id":239,"solution_id":3486," etc... For my landing page I am using the following: $datastream = json_decode($result); foreach($datastream as $component) { $productid = $component->id; I was able to successfully output the data to php from their old output, but I am not sure how to get around the value "picker_offers" that is being passed as part of the json file, but it isn't part of the actual data to output. How can I not include that "picker_offers", or what can I do to be able to read the data? With this new output there is an extra curly bracket wrapper called "picker_offers" around the entire output. Thank you very much
Solution 1 : if you want to remove picker_offers $datastream = json_decode($result); $picker_offers = $datastream->picker_offers; unset($datastream->picker_offers); $datastream = $picker_offers; foreach($datastream as $component) { $productid = $component->id; } Solution 2 : if you don't want to remove picker_offers $datastream = json_decode($result); foreach($datastream->picker_offers as $component) { $productid = $component->id; }
json_decode to an array?
foreach ($likes as $like) { // Extract the pieces of info we need from the requests above $id = idx($like, 'id'); $item = idx($like, 'name'); fwrite($fileout,json_encode($like)); fwrite($fileout,PHP_EOL ); } $json_string = file_get_contents('testson.json'); $get_json_values=json_decode($json_string,true); foreach ($get_json_values as $getlikes) { ?> <li> <a href="https://www.facebook.com/<?php echo $getlikes['id']; ?>" target="_top"> </li> <?php } When opening the browser, there is a Warning: Invalid argument supplied for foreach(). I don't understand why would my arguments be invalid. If I add the if, nothing happens, which shows what the actual problem is. But the question is WHAT IS THE PROPER WAY TO DO THIS? I'm pretty sure it's very simple, but i've been struggling with this for more than an hour. My json files has fields like this, so I don't think there would be the problem: {"category":"Musician\/band","name":"Yann Tiersen (official)","id":"18359161762"} Please help me, I really got tired with it, and I don't know what to do. So... how can I decode the file into an array?
You need the contents of the testson.json file not just the name! Receive the contents via PHP: $json_string = file_get_contents('testson.json'); Make sure there are valid JSON contents in the file by testing it via var_dump(json_decode($json_string)); And then call foreach (json_decode($json_string) as $getlikes) { ?> Update: When you save the file and access it miliseconds later it might be that the filesystem is too slow and not showing you the correct content. Add fclose($fileout); clearstatcache(); before the file_get_contents(); call! I recommend to use file_put_contents() and file_read_contents() to get rid of such problems!
json_decode is expecting a string as it's first parameter. You are passing what I'm guessing is a filename. You should load the file first like so... $json = file_get_contents('testson.json'); $data = json_decode($json);