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
Im trying to load search result from an library api using Search and Retrieve via URL (SRU) at : https://data.norge.no/data/bibsys/bibsys-bibliotekbase-bibliografiske-data-sru
If you see the search result links there, its looks pretty much like XML but when i try like i have before with xml using the code below, it just returns a empty object,
SimpleXMLElement {#546}
whats going on here?
My php function in my laravel project:
public function bokId($bokid) {
$apiUrl = "http://sru.bibsys.no/search/biblio?version=1.2&operation=searchRetrieve&startRecord=1&maximumRecords=10&query=ibsen&recordSchema=marcxchange";
$filename = "bok.xml";
$xmlfile = file_get_contents($apiUrl);
file_put_contents($filename, $xmlfile); // xml file is saved.
$fileXml = simplexml_load_string($xmlfile);
dd($fileXml);
}
If i do:
dd($xmlfile);
instead, it echoes out like this:
Making me very confused that i cannot get an object to work with. Code i present have worked fine before.
It may be that the data your being provided ha changed format, but the data is still there and you can still use it. The main problem with using something like dd() is that it doesn't work well with SimpleXMLElements, it tends to have it's own idea of what you want to see of what data there is.
In this case the namespaces are the usual problem. But if you look at the following code you can see a quick way of getting the data from a specific namespace, which you can then easily access as normal. In this code I use ->children("srw", true) to say fetch all child elements that are in the namespace srw (the second argument indicates that this is the prefix and not the URL)...
$apiUrl = "http://sru.bibsys.no/search/biblio?version=1.2&operation=searchRetrieve&startRecord=1&maximumRecords=10&query=ibsen&recordSchema=marcxchange";
$filename = "bok.xml";
$xmlfile = file_get_contents($apiUrl);
file_put_contents($filename, $xmlfile); // xml file is saved.
$fileXml = simplexml_load_string($xmlfile);
foreach ( $fileXml->children("srw", true)->records->record as $record) {
echo "recordIdentifier=".$record->recordIdentifier.PHP_EOL;
}
This outputs...
recordIdentifier=792012771
recordIdentifier=941956423
recordIdentifier=941956466
recordIdentifier=950546232
recordIdentifier=802109055
recordIdentifier=910941041
recordIdentifier=940589451
recordIdentifier=951721941
recordIdentifier=080703852
recordIdentifier=011800283
As I'm not sure which data you want to retrieve as the title, I just wanted to show the idea of how to fetch data when you have a list of possibilities. In this example I'm using XPath to look in each <srw:record> element and find the <marc:datafield tag="100"...> element and in that the <marc:subfield code="a"> element. This is done using //marc:datafield[#tag='100']/marc:subfield[#code='a']. You may need to adjust the #tag= bit to the datafield your after and the #code= to point to the subfield your after.
$fileXml = simplexml_load_string($xmlfile);
$fileXml->registerXPathNamespace("marc","info:lc/xmlns/marcxchange-v1");
foreach ( $fileXml->children("srw", true)->records->record as $record) {
echo "recordIdentifier=".$record->recordIdentifier.PHP_EOL;
$data = $record->xpath("//marc:datafield[#tag='100']/marc:subfield[#code='a']");
$subData=$data[0]->children("marc", true);
echo "Data=".(string)$data[0].PHP_EOL;
}
I have the following code below, which I am trying to run to save the key in the json to a PHP varible. However, for some reason it's just not doing anything - although it seems my code is right, any ideas where I am going wrong?
<?php
$json = file_get_contents('url');
$data = json_decode($json,true);
$id = $data['s_id'];
echo($id);
?>
and the json from the external URL looks like this
[{"s_id":"20063"}]
You have an array wrapping the object
$id = $data[0]['s_id'];
I'm trying to deserialize this json.
Actual I stay using the simple html dom library for get the web content, so the next step that I do is using the json_decode() function. But when I'll print the value returned by the function I get NULL. This is the code:
<?php
require_once("simplehtmldom_1_5/simple_html_dom.php");
$html = file_get_html('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable¶ms=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>
What's wrong in my code? Maybe this isn't the best way for doing this? Hint me.
It seems that your file_get_html function is not working properly, you can get the content of a web with file_get_contents
<?php
$html = file_get_contents('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable¶ms=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>
Im trying to achieve an output like this
{"status":"ok","0":{"id":"11","title":"digg","url":"http://www.digg.com"}}
but instead i am getting this
{"status":"ok","0":{"id":"11","title":"digg","url":"http:\/\/www.digg.com"}}
this is the php code im using to generate the json
$links = array('id'=>'11','title'=>'digg','url'=>"http://www.digg.com");
$msg = array('status'=>'ok',$links);
echo json_encode($msg);
any idea what is causing this?
UPDATE
i should have been more clear
if you notice the actual url, its inserting "\" before the "/" in the output. Is this supposed to happen, or is there a way to stop this?
They're both equivalent valid JSON, so it shouldn't matter. The JSON strings:
"http://www.digg.com"
and
"http:\/\/www.digg.com"
both decode to:
"http://www.digg.com"
This is a separate issue, but I would prefer:
$links = array(array('id'=>'11','title'=>'digg','url'=>"http://www.digg.com"));
$msg = array('status'=>'ok', 'links'=>$links);
echo json_encode($msg);
{"status":"ok","links":[{"id":"11","title":"digg","url":"http:\/\/www.digg.com"}]}
This makes more sense to me than having a "0" key, and it extend well if you add more sites:
$links = array(array('id'=>'11','title'=>'digg','url'=>"http://www.digg.com"),
array('id'=>'12','title'=>'reddit','url'=>"http://www.reddit.com"));
$msg = array('status'=>'ok', 'links'=>$links);
echo json_encode($msg);
{"status":"ok","links":[{"id":"11","title":"digg","url":"http:\/\/www.digg.com"},
{"id":"12","title":"reddit","url":"http:\/\/www.reddit.com"}]}
Yes. The JSON specs.