naivic/curl
最新稳定版本:1.2.0
Composer 安装命令:
composer require naivic/curl
包简介
Naivic cURL envelope
关键字:
README 文档
README
Some examples:
Simplest GET query - get book information from the openlibrary.org
$url = "http://openlibrary.org/search.json"; $needle = "Alice Wonderland"; $curl = new \Naivic\CURL(); $res = $curl->query( "GET", $url, [ "q" => $needle ] ); echo "Total time: ".$res["info"]["total_time"]." sec\n"; echo "Year: ".$res["data"]["docs"][0]["first_publish_year"]."\n";
result
Total time: 21.788099 sec
Year: 1889
Simple REST API - POST, GET, PUT, PATCH, DELETE queries
Used https://restful-api.dev/ to illustrate functionality
Lets prepare to make series of REST API requests:
$url = "https://api.restful-api.dev/objects"; $hdr = [ "Content-Type: application/json" ]; $hero = [ "name" => "The Cat", "data" => [ "type" => "animal", "location" => "World", ], ]; $curl = new \Naivic\CURL();
Ok, now put The Cat to World, and store ID from server response
$res = $curl->query( "POST", $url, $hero, $hdr ); $id = $res["data"]["id"];
$res["data"]:
Array
(
[id] => ff80818191ad7c2f0191af6b4a550247
[name] => The Cat
[createdAt] => 2024-09-01T21:08:49.883+00:00
[data] => Array
(
[type] => animal
[location] => World
)
)
Then, move The Cat to Wonderland
$hero["data"] = [ "type" => "sapiens", "location" => "Wonderland", ]; $res = $curl->query( "PUT", $url."/".$id, $hero, $hdr );
$res["data"]:
Array
(
[id] => ff80818191ad7c2f0191af6b4a550247
[name] => The Cat
[updatedAt] => 2024-09-01T21:08:51.547+00:00
[data] => Array
(
[type] => sapiens
[location] => Wonderland
)
)
And... swap The Cat to Alice!
$res = $curl->query( "PATCH", $url."/".$id, ["name" => "Alice"], $hdr ); print_r( $res["data"] );
$res["data"]:
Array
(
[id] => ff80818191ad7c2f0191af6b4a550247
[name] => Alice
[updatedAt] => 2024-09-01T21:08:52.623+00:00
[data] => Array
(
[type] => sapiens
[location] => Wonderland
)
)
Knock-knock, wake up Alice
$res = $curl->query( "DELETE", $url."/".$id, hdr: $hdr );
$res["data"]:
Array
(
[message] => Object with id = ff80818191ad7c2f0191af6b4a550247 has been deleted.
)
Where is Alice? ("who the ... is Alice?")
$res = $curl->query( "GET", $url."/".$id, hdr: $hdr );
$res["data"]:
Array
(
[error] => Oject with id=ff80818191ad7c2f0191af6b4a550247 was not found.
)
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-01