fetch json and pretty print
Submitted by clemens on Mon, 2014/02/17 - 3:54pm
While working on Deploy RestWS for D7 project issue JSON I needed a pretty print for the node.json and scanner done for Drush Issue Queue Commands Fix for the new json structure on d.o D7. Both issues as still open so help needed.
Doing some research I learned about the PHP context or more specific the HTTP stream context
With this script you may query d.o for it's json contents when the fix is done.
You may test this with
php pretty-json.php http://build2be.com/sites/build2be.com/files/jsoneditoronline.json_.txt
or build your own json online with services like jsoneditoronline.org.
Note you need PHP 5.4 as listed on PHP json_encode
// pretty-json.php function fetchUrl($url) { $options = array( 'http' => array( 'method' => "GET", 'header' => join("\r\n", array( "Accept-language: en", "Accept: application/json", "User-Agent: Drush", )), ) ); $context = stream_context_create($options); $file = file_get_contents($url, false, $context); return $file; } function help() { global $argv; echo "Usages: $argv[0] list-of-urls"; } if (count($argv) < 2) { help(); } else { $runner = array_shift($argv); while ( $url = array_shift($argv)) { $content = fetchUrl($url); $json = json_decode($content); if ($json) { echo json_encode($json, JSON_PRETTY_PRINT); } else { echo "No json received for $url"; } } } echo "\n";
Attachment | Size |
---|---|
jsoneditoronline.json_.txt | 116 bytes |