jowy / rest-client
PHP 5.3+ Rest 客户端
v0.1.3
2015-10-11 01:26 UTC
Requires
- php: >=5.3.0
- ext-curl: *
This package is not auto-updated.
Last update: 2024-09-24 02:31:22 UTC
README
适用于 PHP 5.3+ 的简单 cURL PHP Rest 客户端库
特性
- 设置
HTTP 身份验证
- 设置
HTTP 头部
- 设置
Curl 选项
GET
、POST
、PUT
、DELETE
方法
安装
此库可以通过Composer安装
$ php composer.phar require jowy/rest-client:@stable
用法
<?php include 'vendor/autoload.php'; use RestClient\CurlRestClient; $curl = new CurlRestClient(); $curl->setOptions([CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0]); var_dump($curl->executeQuery('http://www.google.com')); // OR.... $curl = new CurlRestClient('http://www.google.com', array( // Header data 'X-API-KEY: 16251821972', 'OUTPUT: JSON' ), array( // AUTH (curl) 'CURLOPT_HTTPAUTH' => CURLAUTH_DIGEST, 'username' => 'root', 'password' => 'toor' ), array( CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0 ) );
参数
url
描述目标 URLmethod
定义 HTTP 方法可以是GET
、POST
、PUT
、DELETE
。默认值是DELETE
header
包含头部数组列表data
包含数据数组列表auth
包含认证数组列表
示例完整用法
$curl->executeQuery('http://api.somewebsite.com', 'POST', array( 'X-API-KEY: 16251821972', 'OUTPUT: JSON' ), array( 'USERNAME' => 'jowy', 'SERVERID' => '192882' ), array( 'CURLOPT_HTTPAUTH' => CURL_AUTH_DIGEST, 'username' => 'jowy', 'password' => '123456' ) ); // OR USE CONVENIENT METHODS // GET $res = $curl->get('customer/details', array( 'customerId' => 55 )); // POST $res = $curl->post('customer', array( 'name' => 'Ole Nordmann', 'age' => 49, 'address' => 'Stortingsveien 5', 'zip' => '0120', 'city' => 'Oslo' ));