multiple.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. // First, include Requests
  3. include('../library/Requests.php');
  4. // Next, make sure Requests can load internal classes
  5. Requests::register_autoloader();
  6. // Setup what we want to request
  7. $requests = array(
  8. array(
  9. 'url' => 'http://httpbin.org/get',
  10. 'headers' => array('Accept' => 'application/javascript'),
  11. ),
  12. 'post' => array(
  13. 'url' => 'http://httpbin.org/post',
  14. 'data' => array('mydata' => 'something'),
  15. ),
  16. 'delayed' => array(
  17. 'url' => 'http://httpbin.org/delay/10',
  18. 'options' => array(
  19. 'timeout' => 20,
  20. ),
  21. ),
  22. );
  23. // Setup a callback
  24. function my_callback(&$request, $id) {
  25. var_dump($id, $request);
  26. }
  27. // Tell Requests to use the callback
  28. $options = array(
  29. 'complete' => 'my_callback',
  30. );
  31. // Send the request!
  32. $responses = Requests::request_multiple($requests, $options);
  33. // Note: the response from the above call will be an associative array matching
  34. // $requests with the response data, however we've already handled it in
  35. // my_callback() anyway!
  36. //
  37. // If you don't believe me, uncomment this:
  38. # var_dump($responses);