PHP HTTP 用戶端與框架:Guzzle

WBOY
發布: 2016-07-25 09:12:29
原創
2079 人瀏覽過
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



來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板