-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add http functions * fix tests * fix tests [2] * enhancements on type declarations * coding style fix Co-authored-by: Demin Yin <[email protected]>
- Loading branch information
Showing
3 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* This file is part of Swoole. | ||
* | ||
* @link https://www.swoole.com | ||
* @contact [email protected] | ||
* @license https://github.com/swoole/library/blob/master/LICENSE | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Swoole\Coroutine\Http; | ||
|
||
use Swoole\Coroutine\Http\Client\Exception; | ||
|
||
/** | ||
* @param mixed $data | ||
* @throws Exception | ||
* @return mixed | ||
*/ | ||
function request(string $url, string $method, $data = null, array $options = null, array $headers = null, array $cookies = null) | ||
{ | ||
$info = parse_url($url); | ||
if ($info['scheme'] == 'http') { | ||
$client = new Client($info['host'], swoole_array_default_value($info, 'port', 80), false); | ||
} elseif ($info['scheme'] == 'https') { | ||
$client = new Client($info['host'], swoole_array_default_value($info, 'port', 443), true); | ||
} else { | ||
throw new Exception('unknown scheme "' . $info['scheme'] . '"'); | ||
} | ||
$client->setMethod($method); | ||
if ($data) { | ||
$client->setData($data); | ||
} | ||
if (is_array($options)) { | ||
$client->set($options); | ||
} | ||
if (is_array($headers)) { | ||
$client->setHeaders($options); | ||
} | ||
if (is_array($cookies)) { | ||
$client->setCookies($options); | ||
} | ||
$request_url = $info['path']; | ||
if (!empty($info['query'])) { | ||
$request_url .= '?' . $info['query']; | ||
} | ||
if ($client->execute($request_url)) { | ||
return $client; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
* @throws Exception | ||
* @return Client|false|mixed | ||
*/ | ||
function post(string $url, $data, array $options = null, array $headers = null, array $cookies = null) | ||
{ | ||
return request($url, 'POST', $data, $options, $headers, $cookies); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
* @return Client|false|mixed | ||
*/ | ||
function get(string $url, array $options = null, array $headers = null, array $cookies = null) | ||
{ | ||
return request($url, 'GET', null, $options, $headers, $cookies); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* This file is part of Swoole. | ||
* | ||
* @link https://www.swoole.com | ||
* @contact [email protected] | ||
* @license https://github.com/swoole/library/blob/master/LICENSE | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Swoole\Coroutine; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use function Swoole\Coroutine\Http\get; | ||
use function Swoole\Coroutine\Http\post; | ||
|
||
/** | ||
* @internal | ||
* @coversNothing | ||
*/ | ||
class HttpFunctionTest extends TestCase | ||
{ | ||
public function testGet() | ||
{ | ||
run(function () { | ||
$data = get('http://httpbin.org/get?hello=world'); | ||
$body = json_decode($data->getBody()); | ||
self::assertSame($body->headers->Host, 'httpbin.org'); | ||
self::assertSame($body->args->hello, 'world'); | ||
}); | ||
} | ||
|
||
public function testPost() | ||
{ | ||
run(function () { | ||
$random_data = base64_encode(random_bytes(128)); | ||
$data = post('http://httpbin.org/post?hello=world', ['random_data' => $random_data]); | ||
$body = json_decode($data->getBody()); | ||
self::assertSame($body->headers->Host, 'httpbin.org'); | ||
self::assertSame($body->args->hello, 'world'); | ||
self::assertSame($body->form->random_data, $random_data); | ||
}); | ||
} | ||
} |