1: <?php
2:
3: namespace Budabot\Core;
4:
5: /**
6: * @Instance
7: */
8: class Http {
9:
10: /** @Inject */
11: public $timer;
12:
13: /**
14: * Requests contents of given $uri using GET method and returns AsyncHttp
15: * object which has additional methods for controlling how the query is done.
16: *
17: * You can get both HTTP and HTTPS URIs with method.
18: *
19: * For more info, see AsyncHttp class.
20: *
21: * Asynchronous example:
22: * <code>
23: * $this->http->get("http://www.google.com/")->withCallback(function($response) {
24: * print $response->body;
25: * });
26: * </code>
27: *
28: * Synchronous example:
29: * <code>
30: * $response = $this->http->get("http://www.google.com/")->waitAndReturnResponse();
31: * print $response->body;
32: * </code>
33: *
34: * @param string $uri the requested URI
35: * @return AsyncHttp
36: */
37: public function get($uri) {
38: $asyncHttp = new AsyncHttp('get', $uri);
39: Registry::injectDependencies($asyncHttp);
40: $this->timer->callLater(0, array($asyncHttp, 'execute'));
41: return $asyncHttp;
42: }
43:
44: /**
45: * Requests contents of given $uri using POST method and returns AsyncHttp
46: * object which has additional methods for controlling how the query is done.
47: *
48: * See get() for code example.
49: *
50: * @param string $uri the requested URI
51: * @return AsyncHttp
52: */
53: public function post($uri) {
54: $asyncHttp = new AsyncHttp('post', $uri);
55: Registry::injectDependencies($asyncHttp);
56: $this->timer->callLater(0, array($asyncHttp, 'execute'));
57: return $asyncHttp;
58: }
59: }
60: