PHP HTTP 客户端和框架:Guzzle
jopen
11年前
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.
<?php 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 推ter API $client = new Client('https://api.推ter.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