0

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&amp;prop=revisions&amp;rvlimit=1&amp;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?

Community
  • 1
  • 1
BlueDogRanch
  • 721
  • 1
  • 16
  • 43
  • 2
    Possible duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – scrowler Nov 06 '16 at 20:28
  • Though you've said that the linked question isn't simple enough, it is comprehensive. All you need to know is how to reference elements of objects and arrays, for example `$foo = $foo_object->bar` or `$foo = $foo_array['bar']`. The most naive data stripping you can do would be `$foo = date('Y-m-d', strtotime($timestamp));` – moopet Nov 06 '16 at 21:36

1 Answers1

4
  1. json_decode converts JSON into a native PHP array for easy manipulation.

  2. print_r will recursively print the array so that you can easily read it manually to discover the structure of the document.

  3. 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

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • Thanks, that works great. It's interesting how the data is grabbed and I used print_r to see the array. And also the PHP function to change the date format. This also works fine for different pages, as long as I also find and change the page ID in the $data line (by looking in the array for the page ID) as well as the page title in $url. – BlueDogRanch Nov 06 '16 at 22:04
  • That still works great, but I took another step and made it into a shortcode. But now I get a PHP warning, which I know isn't serious, but I'd like to be able to fit it; I edited my question. – BlueDogRanch Nov 25 '16 at 16:17