Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 25.x
Latest version:
pecl install openswoole-25.2.0
<?php Swoole\Coroutine\Http\Client->execute(string $path): int|bool
The URL path to send a HTTP request to. For example /user/account or /index.php. Only set the path here not the protocol or domain name like http://domain.
Returns the HTTP status code as an int if the request was made without any client errors.
Returns false if something went wrong, use $client->errCode to see what went wrong.
Execute a HTTP request based on using setMethod and setData.
This method is very similar to post() or get() but it allows you to have more control over how the request is built before being sent.
You must call
setMethodand oraddDatabefore making a request withexecute
<?php
use Swoole\Coroutine\HTTP\Client;
Co\run(function()
{
$client = new Client('127.0.0.1', 80);
$client->setHeaders([
'Host' => "localhost",
"User-Agent" => 'Chrome/49.0.2587.3',
'Accept' => 'text/html,application/xhtml+xml,application/xml',
'Accept-Encoding' => 'gzip',
]);
$client->set(['timeout' => 1]);
// Defer HTTP requests and allow other coroutines to execute when waiting
$client->setDefer(true);
// Set the request type and add a file using a string
$client->setMethod('POST');
$client->setData('swoole');
// Execute client request based on how it was setup...
$status = $client->execute('/index.php');
echo "Request status: " . $status . "\n";
var_dump($client->getBody());
$client->close();
});