sonrisa / restclient-component
此包已被弃用且不再维护。没有建议的替代包。
轻量级RESTful客户端组件。适用于PHP5.4及以上版本。
1.2.0
2016-04-27 14:11 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- fabpot/php-cs-fixer: dev-master
- phpunit/phpunit: ~4
This package is not auto-updated.
Last update: 2017-12-15 07:13:19 UTC
README
RESTful客户端组件
RESTful客户端组件允许通过简单的接口与外部API进行通信。
使用CURL发起请求。如果CURL不可用,将尝试使用file_get_contents进行回退。
1. 安装
将以下内容添加到您的 composer.json
文件中
"sonrisa/restclient-component":"dev-master"
2. 可用方法
- $this->setHeader($field,$value);
- $this->setHeaders($headers);
- $this->setAcceptLanguage($lang = 'en');
- $this->setAcceptEncoding($value = 'gzip');
- $this->setUserAgent($agentString);
- $this->setBasicAuthorization($username,$password);
- $this->setKey($keyName,$value);
- $this->get($url,array $params=array());
- $this->post($url,array $params=array());
- $this->put($url,array $params=array());
- $this->patch($url,array $params=array());
- $this->delete($url,array $params=array());
- $this->options($url,array $params=array());
- $this->head($url,array $params=array());
- $this->other($methodName,$url,array $params=array());
CURL客户端独有方法
- $this->setTimeout($timeout);
- $this->setCurlOpt($name,$value);
3. 使用方法
使用非常直接。以下提供示例。
3.1 - 客户端请求
<?php use \Sonrisa\Component\RestfulClient\RestfulClient; $url = 'http://api.duckduckgo.com/?q=DuckDuckGo&format=json&pretty=1'; $client = new RestfulClient(); // (Optional) Directly set a header field $client->setHeader('Content-Type', 'application/json'); // (Optional) Set some headers with convenient functions $client->setAcceptLanguage('ca,en;q=0.8,es;q=0.6') ->setAcceptEncoding('gzip,deflate,sdch') ->setUserAgent('Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36'); // (Optional) Add the API key and the username for each request // Will result in: http://api.duckduckgo.com/?q=DuckDuckGo&format=json&pretty=1&api=$apiKey&username=$username $client->setKey('api',$apiKey); $client->setKey('username',$username); // Sending parameters is optional, // so $params is actually an optional argument. $params = array(); // Send a GET Request: $response = $client->get($url,$params); // Send a POST Request $response = $client->post($url ,$params); // Send a PUT Request $response = $client->put($url ,$params); // Send a PATCH Request $response = $client->patch($url ,$params); // Send a DELETE Request $response = $client->delete($url ,$params); // Send a HEAD Request $response = $client->head( $url ,$params); // Send a OPTIONS Request $response = $client->options($url ,$params); // Send a CUSTOM Request $response = $client->other('X-SonrisaCMS-Header', $url ,$params);
3.2 - 客户端响应
响应总是返回一个键值数组,其中键名是响应中返回的头信息名称。
该数组将始终包含3个主要键
$response['request']
: 包含由类构建的请求HTTP头信息$response['response']
: 包含查询URL返回的所有数据$response['headers']
: 包含响应的HTTP头信息。
响应示例
<?php //var_dump($response); array(3) { 'request' => array(3) { 'URL' => string(68) "http://api.duckduckgo.com/?q=DuckDuckGo&format=json&pretty=1?count=2" 'Host' => string(18) "api.duckduckgo.com" 'Accept' => string(3) "*/*" } 'response' => string(3807) "{ "Definition" : "", "DefinitionSource" : "", "Heading" : "DuckDuckGo", "AbstractSource" : "Wikipedia", "Image" : "https://i.duckduckgo.com/i/d9dea591.png", "RelatedTopics" : [ { "Result" : "<a href=\"http://duckduckgo.com/Duck%2C_duck%2C_goose\">Duck, duck, goose</a> - Duck, Duck, Goose or Duck, Duck, Gray Duck is a traditional children's game often first learned in pre-school or kindergarten then later adapted on the playground for early elementary stu...", "Icon"... 'headers' => array(13) { 'Protocol' => string(8) "HTTP/1.1" 'Status' => int(200) 'Server' => string(5) "nginx" 'Date' => string(29) "Mon, 03 Feb 2014 20:14:52 GMT" 'Content-Type' => string(39) "application/x-javascript; charset=UTF-8" 'Transfer-Encoding' => string(7) "chunked" 'Connection' => string(10) "keep-alive" 'X-DuckDuckGo-Results' => string(1) "1" 'Expires' => string(29) "Mon, 03 Feb 2014 20:14:53 GMT" 'Cache-Control' => string(9) "max-age=1" 'Strict-Transport-Security' => string(9) "max-age=0" 'X-DuckDuckGo-Locale' => string(5) "en_US" 'Request-Time' => string(16) "0.237205 seconds" } }
4. 待办事项
更好的方法
- 对于文件获取内容,遵循301和302代码,并在返回的URL上重新抛出请求。
更好的测试
- 使用 PHP作为服务器 创建更好的测试用例,等待请求并返回响应。
- 测试基于curl和基于file_get_contents的客户端类的multipart/data。
5. 作者
Nil Portugués Calderó