-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
18 deletions.
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
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
71 changes: 71 additions & 0 deletions
71
output/swoole_library/src/core/Coroutine/Http/functions.php
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 = swoole_array_default_value($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