Skip to content

Commit

Permalink
More complex example and small fix for Http
Browse files Browse the repository at this point in the history
  • Loading branch information
mbonneau committed Apr 19, 2018
1 parent b1ae10e commit d09f327
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"ext-swoole": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
"phpunit/phpunit": "^6.0",
"rx/operator-extras": "^2.1"
}
}
19 changes: 19 additions & 0 deletions examples/read_and_http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

$file = \Rx\Swoole\Async::read(__DIR__ . '/url_list.txt');

$urlInfo = $file
->cut("\n")
->flatMap(function ($url) {
return \Rx\Swoole\Http::get($url)
->map(function ($content) use ($url) {
return $url . " is " . strlen($content) . " bytes.\n";
});
});

$urlInfo->subscribe(function ($info) {
echo $info;
});

3 changes: 3 additions & 0 deletions examples/url_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://www.google.com:80/
https://www.facebook.com:80/
https://www.microsoft.com:80/
4 changes: 2 additions & 2 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public static function head(string $url, array $headers = []): Observable
private static function createClient($url)
{
$urlParts = parse_url($url);
$secure = 'wss' === substr($url, 0, 3);
$port = $urlParts['port'] ?: ($secure ? 443 : 80);
$secure = 'https' === substr($url, 0, 3);
$port = $urlParts['port'] ?? ($secure ? 443 : 80);

$cli = new \Swoole\Http\Client($urlParts['host'], $port);

Expand Down

0 comments on commit d09f327

Please sign in to comment.