Maison >développement back-end >tutoriel php >PHP HTTP 客户端和框架:Guzzle

PHP HTTP 客户端和框架:Guzzle

WBOY
WBOYoriginal
2016-07-25 09:12:292108parcourir
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.

  1. require_once 'vendor/autoload.php';
  2. use Guzzle\Http\Client;
  3. // Create a client and provide a base URL
  4. $client = new Client('https://api.github.com');
  5. // Create a request with basic Auth
  6. $request = $client->get('/user')->setAuth('user', 'pass');
  7. // Send the request and get the response
  8. $response = $request->send();
  9. echo $response->getBody();
  10. // >>> {"type":"User", ...
  11. echo $response->getHeader('Content-Length');
  12. // >>> 792
  13. // Create a client to work with the Twitter API
  14. $client = new Client('https://api.twitter.com/{version}', array(
  15. 'version' => '1.1'
  16. ));
  17. // Sign all requests with the OauthPlugin
  18. $client->addSubscriber(new Guzzle\Plugin\Oauth\OauthPlugin(array(
  19. 'consumer_key' => '***',
  20. 'consumer_secret' => '***',
  21. 'token' => '***',
  22. 'token_secret' => '***'
  23. )));
  24. echo $client->get('statuses/user_timeline.json')->send()->getBody();
  25. // >>> {"public_gists":6,"type":"User" ...
  26. // Create a tweet using POST
  27. $request = $client->post('statuses/update.json', null, array(
  28. 'status' => 'Tweeted with Guzzle, http://guzzlephp.org'
  29. ));
  30. // Send the request and parse the JSON response into an array
  31. $data = $request->send()->json();
  32. echo $data['text'];
  33. // >>> Tweeted with Guzzle, http://t.co/kngJMfRk
复制代码

项目主页:http://www.open-open.com/lib/view/home/1392714245460



Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn