Rumah > pembangunan bahagian belakang > tutorial php > PHP HTTP 客户端和框架:Guzzle

PHP HTTP 客户端和框架:Guzzle

WBOY
Lepaskan: 2016-07-25 09:12:29
asal
2109 orang telah melayarinya
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



sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan