Home > Backend Development > PHP Tutorial > PHP HTTP client and framework: Guzzle

PHP HTTP client and framework: Guzzle

WBOY
Release: 2016-07-25 09:12:29
Original
2098 people have browsed it
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 GuzzleHttpClient;
  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 GuzzlePluginOauthOauthPlugin(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



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template