devgol / json
此包已被弃用且不再维护。未建议替代包。
从任何地方获取并保存json数据。
0.1
2015-07-14 11:47 UTC
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');
从任何资源获取json数据,并使其准备好在php中使用。直接将任何资源的json数据保存到文件中。只需传递资源位置,Json将处理剩余部分。
安装
composer require devgol/json
支持的资源
- API 任何公开可用的API。
- FILE 您电脑上的任何json文件。
- JSON_STRING 在您的php代码中创建一个准备解析的json字符串。
特性
- 实时验证json。
- 从任何地方获取json。
- 将json从任何地方保存到文件。
- 无需记住API,只需
getJson($resource)和saveJson($resource, $target_file, $rewrite = true)
高级使用
/*
* 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
}