mykehowells / httpeep
PHP HTTP 客户端,用于API端点/资源请求
1.0.0
2020-07-21 12:09 UTC
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-09-12 16:15:32 UTC
README
PHP HTTP 客户端,用于API端点/资源请求
设置
Composer
如果你使用Composer,请在终端中输入以下命令
composer require mykehowells/httpeep
然后,将require 'vendor/autoloader.php'添加到你的PHP项目中。
非Composer
要开始使用HttPeep,请将此仓库克隆到你的项目中;使用SPL自动加载器,将以下代码添加到你的项目中以加载所需的文件。
set_include_path( CLASSES_DIR ); spl_autoload_extensions('.class.php'); spl_autoload_register();
这将按你在项目中以及HttPeep文件中调用它们的方式包含文件。
用法
HttPeep的一个简单用例如下;
$client = new \HttPeep\Client( 'https://api.example.com' ); $response = $client->post( "/user/auth", [ 'token' => $_SESSION['api']['token'], 'username' => $_POST['username'], 'password' => md5( $_POST['password'] ) ] ); var_export( $response );
你还可以使用\HttPeep\Client->json()方法从响应体中返回JSON。
$client = new \HttPeep\Client( 'https://api.example.com' ); $response = $client->post( "/user/auth", [ 'token' => $_SESSION['api']['token'], 'username' => $_POST['username'], 'password' => md5( $_POST['password'] ) ] )->json(); var_export( $response );
方法
HttPeep提供以下方法:
- GET
- POST
- DELETE
- PUT
客户端配置
当实例化\HttPeep\Client()时,你可以传递一个包含cURL配置变量的数组;例如
$client = new \HttPeep\Client( 'https://api.example.com', [ 'curl' => [ 'followlocation' => true, // CURLOPT_FOLLOWLOCATION - follow any Location: header sent as part of the HTTP header 'maxredirs' => 10 // CURLOPT_MAXREDIRS - Maximum number of HTTP redirections to follow ] ] );
客户端内设置的默认CURLOPTS