phpgt / curlinterface
v3.1.1
2023-04-29 17:28 UTC
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- phpgt/json: ^1.2
Requires (Dev)
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7
- dev-master
- v3.1.1
- v3.1.0
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.x-dev
- v2.1.0
- v2.0.0
- v1.1.0
- v1.0.0
- dev-dependabot/composer/phpunit/phpunit-10.1.3
- dev-dependabot/composer/phpunit/phpunit-10.1.2
- dev-test-github
- dev-codesniffer
- dev-54-httplug
- dev-circleci
This package is auto-updated.
Last update: 2023-05-12 03:58:25 UTC
README
此库通过对象包装PHP的cURL扩展函数,以增强代码的可读性和可测试性。
为什么?我们希望为PHP.Gt/Fetch(我们使用cURL创建异步HTTP调用并使用承诺的PHP实现)提供一个面向对象的基础。
示例用法:从远程源获取JSON对象
在处理HTTP调用时,与JSON一起工作非常常见。此库通过缓冲exec()
调用的输出,使用output()
或outputJson()
轻松检索,从而减少了大量样板代码的需求。
使用PHP.Gt/Curl的示例
$curl = new Curl("https://catfact.ninja/fact"); $curl->exec(); $json = $curl->outputJson(); echo "Here's a cat fact: {$json->getString("fact")}"; echo PHP_EOL; echo "The fact's length is {$json->getInt("length")} characters."; echo PHP_EOL;
使用PHP的本地curl_*
函数的相同示例
// Using native functionality to achieve the same: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://catfact.ninja/fact"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); if(false === $result) { die("CURL error: " . curl_error($ch)); } $json = json_decode($result); if(is_null($json)) { die("JSON decoding error: " . json_last_error_msg()); } // Note: No type checks are made on the `fact` and `length` properties here. echo "Here's a cat fact: {$json->fact}"; echo PHP_EOL; echo "The fact's length is {$json->length} characters."; echo PHP_EOL;