Guzzle是一个PHP HTTP 客户端和框架,用于构建 RESTful web service 客户端。
- All the power of cURL with a simple interface.
- 持久连接和并行请求
- Streams request and response bodies
- Service descriptions for quickly building clients.
- Powered by the Symfony2 EventDispatcher.
- Use all of the code or only specific components.
- Plugins for caching, logging, OAuth, mocks, and more
- Includes a custom node.js webserver to test your clients.
-
require_once 'vendor/autoload.php';
- use guzzle\http\client;
-
- // create a client and provide a base url
- $client = new client('https://api.github.com');
- // create a request with basic auth
- $request = $client->get('/user')->setauth('user', 'pass');
- // send the request and get the response
- $response = $request->send();
- echo $response->getbody();
- // >>> {"type":"user", ...
- echo $response->getheader('content-length');
- // >>> 792
-
- // create a client to work with the twitter api
- $client = new client('https://api.twitter.com/{version}', array(
- 'version' => '1.1'
- ));
-
- // sign all requests with the oauthplugin
- $client->addsubscriber(new guzzle\plugin\oauth\oauthplugin(array(
- 'consumer_key' => '***',
- 'consumer_secret' => '***',
- 'token' => '***',
- 'token_secret' => '***'
- )));
-
- echo $client->get('statuses/user_timeline.json')->send()->getbody();
- // >>> {"public_gists":6,"type":"user" ...
-
- // create a tweet using post
- $request = $client->post('statuses/update.json', null, array(
- 'status' => 'tweeted with guzzle, http://guzzlephp.org'
- ));
-
- // send the request and parse the json response into an array
- $data = $request->send()->json();
- echo $data['text'];
- // >>> tweeted with guzzle, http://t.co/kngjmfrk
复制代码
项目主页:http://www.open-open.com/lib/view/home/1392714245460
|