devgol / json
Get and save json data from anywhere.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Watchers: 1
Open Issues: 0
pkg:composer/devgol/json
Requires
- guzzle/guzzle: ^3.9
- guzzlehttp/guzzle: 6.0
Requires (Dev)
- fzaninotto/faker: ^1.5
- mockery/mockery: ^0.9.4
- phpunit/phpunit: 4.7
This package is not auto-updated.
Last update: 2015-08-08 10:10:55 UTC
README
require "vendor/autoload.php";
// You can use a one fit all json() function
json($resource, $save_to_file = null, $create_file = true);
// Get the data
$data = getJson('https://api.github.com/users/oussamaelgoumri'); //API resource
$data = getJson('data/content.json'); //FILE resource
$data = getJson('{"name":"Oussama Elgoumri"}'); //JSON_STRING resource
var_dump($data['name']); //Access json as you normally do with PHP
// Save the data
saveJson('https://api.github.com/users/oussamaelgoumri', 'output/target_file.json'); //from API resource
saveJson('data/content.json', 'output/target_file.json'); //from FILE resource
saveJson('{"name":"Oussama Elgoumri"}', 'output/target_file.json'); //from JSON_STRING resource
// And of course, you can save data to a file directly from an array in php
$data = ['name' => 'Oussama Elgoumri'];
saveJson($data, 'output/target_file.json');
Get json data from any resource, and make it ready to be used from php. Directly save json data from any resource, to a file. Just pass the resource location, Json will take care of the rest.
Installation
composer require devgol/json
Supported resources
- API Any publicly available api.
- FILE Any json file on your computer.
- JSON_STRING make a json string in your php code ready to be parsed.
Features
- Validate json on the fly.
- Get json from anywhere.
- Save json from anywhere to a file.
- No api to memorise, just
getJson($resource)andsaveJson($resource, $target_file, $rewrite = true)
Advanced usage
/*
* json start by evaluating the resource, and fall back in this order if the resource is not valid
* api -> file -> json_string, so for eg. if you are sure that the resource is API and you want some feedback
* if it's not available, then:
*/
try {
$data = json(['https://api.github.com/users/oussamadev' => 'api']);
} catch( ... ) {
// throw: NotApiResourceException - if the resource can not be accessed.
// throw: ResourceIsNotJsonException - if the resource does not have json content.
}
/*
* in case you want to save json content to file, but you don't want the file to be created
* if it does not exist, then:
*/
try {
json('{"name":"Oussama Elgoumri"}', 'target_file.txt', false);
} catch ( ... ) {
// throw: SaveToFileNotFoundException - if the target file does not exists
}