PHP HTTP Client and framework: Guzzle

Source: Internet
Author: User
Tags oauth
Guzzle is a PHP HTTP client and framework for building RESTful Web service clients.
    • All the power of CURL with a simple interface.
    • Persistent connections and parallel requests
    • Streams Request and response bodies
    • Service descriptions for quickly building clients.
    • Powered by the Symfony2 Eventdispatcher.
    • Use any 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 (The 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
Copy Code

Project home:http://www.open-open.com/lib/view/home/1392714245460

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.