I'm trying to use the full calendar from www.fullcalendar.io passing a URL that returns a JSON formatted data:
[
{"id":"29041412","start":"2029-04-14T12:00","end":"2029-04-14T1:00","title":"Victor"},
{"id":"28041411","start":"2028-04-14T11:00","end":"2028-04-14T2030:00","title":"nani"},
{"id":"15051417","start":"2015-05-14T17:00","end":"2015-05-14T2029:00","title":"nani"},
{"id":"29041411","start":"2029-04-14T11:00","end":"2029-04-14T2016:00","title":"papapa"},
{"id":"30041411","start":"2030-04-14T11:00","end":"2030-04-14T2030:00","title":"baiak"},
{"id":"30041417","start":"2030-04-14T17:00","end":"2030-04-14T2031:00","title":"iepwpamxb"},
{"id":"15051412","start":"2015-05-14T12:00","end":"2015-05-14T2031:00","title":"lala"}
]
I try to follow and edit a demo that is inside the package of full calendar but it show example with json file, not with URL. I not find the solution for pass the URL that returns this data and the full calendar put events in the calendar.
I take format of my json from the json explained in the web.
this is my php code:
// Read and parse our events JSON file into an array of event data arrays.
$json = file_get_contents('http://***/diary_service_natursais.php?method=getEntireBooking');
$input_arrays = json_decode($json, true);
// Accumulate an output array of event data arrays.
$output_arrays = array();
foreach ($input_arrays as $array) {
// Convert the input array into a useful Event object
$event = new Event($array, $timezone);
// If the event is in-bounds, add it to the output
if ($event->isWithinDayRange($range_start, $range_end)) {
$output_arrays[] = $event->toArray();
}
}
First of all, you need to define Event object. You can try to include this file as follow:
require './utils.php';
then you need to define $range_start and $range_end variables with date format as yyyy-mm-dd
You can check this example.
Related
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;
}
Could someone tell me how i request json from a php file located on another server that gives it to our asp.net?
It includes ~10 variables we want to use again.
I don't want just a string, but an array.
so this wont work:
//We don't actually use the given url of course
string url = "url";
var json = new WebClient().DownloadString(url);
Response.Write(json);
I'd like to use it somewhere like this jsonname[2]
Output a JSON array from your php page like this
$myArr = array("John", "Mary", "Peter", "Sally");
$myJSON = json_encode($myArr);
header('Content-Type: application/json');
echo $myJSON;
In your c# code you then need to deserialize the string returned by DownloadString into a type that matches the format of the JSON data. If your JSON is just an array of strings, then use JsonConvert.DeserializeObject<List<string>>(json) from the Newtonsoft.Json package. Otherwise you need to replace List with something that matches the Json data, for example a new class with all the same property names and types. In this case
string url = "url";
var json = new WebClient().DownloadString(url);
Response.Write(json);
var myArr = JsonConvert.DeserializeObject<List<string>>(json);
Response.Write(myArr[1]); // will output "Mary"
If you don't have Newtonsoft.Json, this question has some options for obtaining it
I need a simpler explanation than How do I extract data from JSON with PHP? And, I also need to spit the date away from the timestamp in the final PHP.
I can grab the "Test article" metadata in PHP via the Wikipedia JSON API this way:
<?php
$json_string = file_get_contents("https://en.wikipedia.org/w/api.php?action=query&titles=Test_article&prop=revisions&rvlimit=1&format=json");
print $json_string;
?>
Which gives me this:
{"continue":{"rvcontinue":"20161025140129|746140638","continue":"||"},"query":
{"normalized":[{"from":"Test_article","to":"Test article"}],"pages":{"29005947":
{"pageid":29005947,"ns":0,"title":"Test article","revisions":
[{"revid":746140679,"parentid":746140638,"user":"Theblackmidi72",
"timestamp":"2016-10-25T14:01:47Z","comment":"Undid revision 746140638 by
[[Special:Contributions/Theblackmidi72|Theblackmidi72]] ([[User
talk:Theblackmidi72|talk]])"}]}}}}
But how to I get and echo/print just the date from timestamp, i.e. the "2016-10-25" from "timestamp":"2016-10-25T14:01:47Z", and just that string from the whole JSON string?
I assume I need to first grab the full string 016-10-25T14:01:47Z and then strip the T14:01:47Z from it.
Edit 11/25/16 Jeff's answer works great, and I converted the function into a shortcode so I can insert it into post/page content.
function wikipedia_article_date() {
$url = "https://en.wikipedia.org/w/api.php?action=query&titles=Test_article&prop=revisions&rvlimit=1&format=json";
$data = json_decode(file_get_contents($url), true);
$date = $data['query']['pages']['746140638']['revisions'][0]['timestamp'];
$date = new DateTime($date);
return $date->format('m-d-Y');
}
add_shortcode('article_date','wikipedia_article_date');
But now I get a PHP Warning:
file_get_contents(https://en.wikipedia.org/w/api.php?action=query&
amp;titles=Test_article&prop=revisions&rvlimit=1&format=json):
failed to open stream: no suitable wrapper could be found in
/functions/shortcodes.php
is this an issue with my shortcode or with the original function?
json_decode converts JSON into a native PHP array for easy manipulation.
print_r will recursively print the array so that you can easily read it manually to discover the structure of the document.
DateTime::format is useful for converting date/time formats.
<?php
$url = "https://en.wikipedia.org/w/api.php?action=query&titles=Test_article&prop=revisions&rvlimit=1&format=json";
$data = json_decode(file_get_contents($url), true);
// this will show you the structure of the data
//print_r($data);
// just the value in which you're interested
$date = $data['query']['pages']['29005947']['revisions'][0]['timestamp'];
// cast to the format you want
$date = new DateTime($date);
echo $date->format('Y-m-d');
2016-10-25
I'm creating a CMS for my client to work with his photographs and to sell them on his site. For the CMS end as well as the front end, which both will be all AJAX, it'd be nice to get a JSON feed setup so that I can just use the same feed to generate new "pages" and "views" with JS.
So this example feed would have like {[name:'A Photo',description:'lorem ipsum...'],[...]} and then with jQuery or JS i can create a table of all his photographs, pages, etc. How can I set this up for myself?
Should I just create a PHP file that gets all the data from the MongoDB put's it in an array than convert the array to JSON?
$cursor = $this->collection->find($params);
$return = array();
$i=0;
while( $cursor->hasNext() )
{
$return[$i] = $cursor->getNext();
// key() function returns the records '_id'
$return[$i++]['_id'] = $cursor->key();
}
return json_encode($return);
That is how I return JSON frrom Mongo.
I did it this way:
$cursor = $collection->find($params);
if($cursor->hasNext())
{
return json_encode(iterator_to_array($cursor));
}
If you need the _ids, check this answer to see why you're not getting them:
MongoDB PHP: How do I get ObjectId with a JSON feed? (it's blank)
This is what I do :
$data = json_decode(json_encode(iterator_to_array($cursor, true)));
this way I make sure I have an array of objects that you can properly call :
foreach($data as $doc){
echo 'title:'.$doc->title.'<br>';
}
I'm trying to access the individual member-fields of a JSON object in PHP from a JSON string but I can't to access the inner-json, all I get is Array.
This is the JSON string
data = (
{
"created_time" = "2018-10-07T04:42:39+0000";
id = 1069496473131329;
name = "NAME_0";
},
{
"created_time" = "2018-09-09T10:31:50+0000";
id = 955684974605664;
name = "NAME_1";
},
At the moment my code is:
$nameString = $_POST["nameData"];
$nameJsonString = json_encode($nameString, JSON_FORCE_OBJECT);
$jsonNameObj = json_decode($nameJsonString, true);
I've been trying to access the individual entry with:
$element = $jsonNameObj['data'][0];
But only receive Array.
Any help would be greatly appreciated,
Cheers :)
After checking the inputted JSON data, I've realised that it doesn't have a consistent form. As opposed to the overall structure being:
JSON -> List -> JSON
Instead, it's:
JSON -> List
The list contains individual elements that can be in a different order. Consequently, calling:
$element = $jsonNameObj['data'][0]['created_time'];
Works sometimes. As there are three-values/object, I can congregate these values into a trio.
I'm sure there's a way to condense this list into a fixed-JSON format but I'm not familiar with how I'd go about that.
At the moment, with a bit of logic on the back-end, I can retrieve the values.
Thanks for your help #Difster and #Osama!