8000 returns contents now, not object · shuchkin/react-http-client@a18b961 · GitHub
[go: up one dir, main page]

Skip to content

Commit a18b961

Browse files
committed
returns contents now, not object
1 parent 2084963 commit a18b961

File tree

5 files changed

+85
-33
lines changed

5 files changed

+85
-33
lines changed

README.md

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ $loop = \React\EventLoop\Factory::create();
88

99
$http = new \Shuchkin\ReactHTTP\Client( $loop );
1010

11-
$http->get( 'https://tools.ietf.org/rfc/rfc2068.txt' )->then( function ( \Shuchkin\ReactHTTP\Client $client ) {
11+
$http->get( 'https://tools.ietf.org/rfc/rfc2068.txt' )->then( function( $content ) {
1212

13-
echo $client->content;
13+
echo $content;
1414

1515
}, function ( \Exception $ex ) {
1616

1717
echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
1818

1919
} );
20+
21+
$loop->run();
2022
```
2123

2224
## Post
@@ -25,39 +27,80 @@ $loop = \React\EventLoop\Factory::create();
2527

2628
$http = new \Shuchkin\ReactHTTP\Client( $loop );
2729

28-
$http->post( 'https://reqres.in/api/users', '{"name": "morpheus","job": "leader"}' )->then( function ( \Shuchkin\ReactHTTP\Client $client ) {
30+
$http->post( 'https://reqres.in/api/users', '{"name": "morpheus","job": "leader"}' )->then( function ( $content ) {
2931

30-
echo $client->content;
32+
echo $content;
3133

3234
}, function ( \Exception $ex ) {
3335

3436
echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
3537

3638
} );
37-
// {"name":"morpheus","job":"leader","id":"554","createdAt":"2018-12-17T10:31:29.469Z"}
39+
40+
$loop->run();
41+
42+
// {"name":"morpheus","job":"leader","id":"554","createdAt":"2018-12-17T10:31:29.469Z"}
3843
```
3944

40-
## Chunked encoding
45+
## Send headers
4146
```php
4247
$loop = \React\EventLoop\Factory::create();
43-
4448
$http = new \Shuchkin\ReactHTTP\Client( $loop );
45-
$http->get( 'https://jigsaw.w3.org/HTTP/ChunkedScript' )->then( function () {
4649

47-
echo PHP_EOL . 'Mission complete';
48-
49-
}, function ( \Exception $ex ) {
50+
$http->get('GET', 'https://jigsaw.w3.org/HTTP/TE/foo.txt',['User-Agent' => 'ReactPHP Awesome'] )->then(
51+
function ( $content ) {
52+
echo $content;
53+
},
54+
function ( \Exception $ex ) {
55+
echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
56+
}
57+
);
58+
$loop->run();
59+
```
5060

51-
echo 'ERROR '.$ex->getCode().' '.$ex->getMessage();
61+
## Read chunks
62+
```php
63+
$loop = \React\EventLoop\Factory::create();
5264

53-
} );
65+
$http = new \Shuchkin\ReactHTTP\Client( $loop );
66+
$http->get( 'https://jigsaw.w3.org/HTTP/ChunkedScript' )->then(
67+
function () {
68+
echo PHP_EOL . 'Mission complete';
69+
},
70+
function ( \Exception $ex ) {
71+
echo 'ERROR '.$ex->getCode().' '.$ex->getMessage();
72+
}
73+
);
5474

5575
$http->on('chunk', function( $chunk ) {
5676
echo PHP_EOL.'-- CHUNK='.$chunk;
5777
});
5878

5979
$loop->run();
6080
```
81+
82+
## Get headers & debug
83+
```
84+
$loop = \React\EventLoop\Factory::create();
85+
86+
$http = new \Shuchkin\ReactHTTP\Client( $loop );
87+
88+
$http->request('GET','https://reqres.in/api/users')->then(
89+
function( \Shuchkin\ReactHTTP\Client $client ) {
90+
// dump response headers
91+
print_r( $client->headers );
92+
// dump content
93+
echo PHP_EOL . $client->content;
94+
},
95+
function ( \Exception $ex ) {
96+
echo 'ERROR '.$ex->getCode().' '.$ex->getMessage();
97+
}
98+
);
99+
// enable debug mode
100+
$http->on('debug', function( $s ) { echo trim($s).PHP_EOL; } );
101+
$loop->run();
102+
```
103+
61104
## Install
62105

63106
The recommended way to install this library is [through Composer](https://getcomposer.org).

examples/01-basic-usage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
$http = new \Shuchkin\ReactHTTP\Client( $loop );
88

9-
$http->get( 'https://tools.ietf.org/html/rfc2616' )->then( function ( \Shuchkin\ReactHTTP\Client $client ) {
9+
$http->get( 'https://tools.ietf.org/html/rfc2616' )->then( function ( $content ) {
1010

11-
echo $client->content;
11+
echo $content;
1212

1313
}, function ( \Exception $ex ) {
1414

examples/02-headers.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@
55
$loop = \React\EventLoop\Factory::create();
66

77
$http = new \Shuchkin\ReactHTTP\Client( $loop );
8-
$http->get( 'https://jigsaw.w3.org/HTTP/TE/foo.txt' )->then( function ( \Shuchkin\ReactHTTP\Client $client ) {
8+
$http->request('GET', 'https://jigsaw.w3.org/HTTP/TE/foo.txt', null, ['User-Agent' => 'ReactPHP Awesome'] )->then(
9+
function ( \Shuchkin\ReactHTTP\Client $client ) {
910

10-
echo 'HEADERS------------------------'.PHP_EOL;
11+
echo 'HEADERS------------------------'.PHP_EOL;
1112

12-
/** @noinspection ForgottenDebugOutputInspection */
13-
print_r( $client->headers );
13+
/** @noinspection ForgottenDebugOutputInspection */
14+
print_r( $client->headers );
1415

15-
echo PHP_EOL.'BODY-------------------------'.PHP_EOL.$client->content;
16+
echo PHP_EOL.'BODY-------------------------'
17+
.PHP_EOL.$client->content;
1618

17-
echo PHP_EOL.'Content real length='.strlen( $client->content );
19+
echo PHP_EOL.'Content real length='.strlen( $client->content );
1820

19-
}, function ( \Exception $ex ) {
21+
},
22+
function ( \Exception $ex ) {
2023

21-
echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
24+
echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
2225

23-
} );
26+
}
27+
);
2428

2529
$loop->run();

examples/04-post.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
$http = new \Shuchkin\ReactHTTP\Client( $loop );
88

9-
$http->post( 'https://reqres.in/api/users', '{"name": "morpheus","job": "leader"}' )->then( function ( \Shuchkin\ReactHTTP\Client $client ) {
9+
$http->post( 'https://reqres.in/api/users', '{"name": "morpheus","job": "leader"}' )->then( function ( $content ) {
1010

11-
echo $client->content;
11+
echo $content;
1212

1313
}, function ( \Exception $ex ) {
1414

src/Client.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Client extends \Evenement\EventEmitter {
2121
private $chunked;
2222
private $curChunkLen;
2323
private $curChunk;
24+
private $resultAsContents;
2425

2526
public $keepAlive;
2627
public function __construct( \React\EventLoop\LoopInterface $loop, array $options = [] ) {
@@ -33,29 +34,33 @@ public function __construct( \React\EventLoop\LoopInterface $loop, array $option
3334
$this->connector = new \React\Socket\Connector( $this->loop, $options );
3435
$this->maxRedirects = 10;
3536
$this->numRedirects = 0;
37+
3638
}
3739

3840
public function get( $url, array $headers = [] ) {
39-
return $this->request( 'GET', $url, null, $headers );
41+
return $this->request( 'GET', $url, null, $headers, true );
4042
}
4143
public function post( $url, $data, array $headers = [] ) {
42-
return $this->request( 'POST', $url, $data, $headers );
44+
return $this->request( 'POST', $url, $data, $headers, true );
4345
}
4446
public function put( $url, $data, array $headers = [] ) {
45-
return $this->request( 'PUT', $url, $data, $headers );
47+
return $this->request( 'PUT', $url, $data, $headers, true );
4648
}
4749
public function delete( $url, array $headers = [] ) {
48-
return $this->request( 'DELETE', $url, null, $headers );
50+
return $this->request( 'DELETE', $url, null, $headers, true );
4951
}
5052

51-
public function request( $method, $url, $data = null, array $headers = [] ) {
53+
public function request( $method, $url, $data = null, array $headers = [], $result_as_contents = false ) {
5254
if ( $this->busy ) {
5355
$new_client = new self( $this->loop );
5456
$new_client->cookie = $this->cookie;
5557
$new_client->url = $this->url;
5658

57-
return $new_client->request( $method, $url, $data, $headers );
59+
return $new_client->request( $method, $url, $data, $headers, $result_as_contents );
5860
}
61+
62+
$this->resultAsContents = $result_as_contents;
63+
5964
if ( isset($this->listeners['debug'])) {
6065
$this->emit( 'debug', [$method.' '.$url.' data='.gettype($data).' headers='.count($headers)] );
6166
}
@@ -268,7 +273,7 @@ public function handleEnd() {
268273
$this->deffered->reject( new \Exception( $this->headers['STATUS'], $this->headers['STATUS-CODE'] ));
269274
return;
270275
}
271-
$this->deffered->resolve( $this );
276+
$this->deffered->resolve( $this->resultAsContents ? $this->content : $this );
272277
}
273278

274279
public function handleClose() {

0 commit comments

Comments
 (0)
0