PHP code example of easyswoole / http-client
1. Go to this page and download the library: Download easyswoole/http-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
easyswoole / http-client example snippets
asySwoole\HttpClient\HttpClient;
use Swoole\Coroutine;
Coroutine\run(function () {
$client = new HttpClient('https://example.com/api/users?page=1');
$response = $client->get(['Accept' => 'application/json']);
if ($response->getStatusCode() !== 200) {
printf(
"request failed: status=%s error=%s (%s)\n",
$response->getStatusCode(),
$response->getErrMsg(),
$response->getErrCode()
);
return;
}
var_dump($response->json(true));
});
$client = new HttpClient('https://example.com/search?lang=zh');
$client->setQuery([
'lang' => 'en',
'keyword' => 'swoole',
]);
$response = $client->get();
// 请求路径:/search?lang=en&keyword=swoole
$client->setPath('/v2/search');
// 表单数据
$response = (new HttpClient('https://example.com/form'))->post([
'name' => 'EasySwoole',
'enabled' => 1,
]);
// 原始请求体
$response = (new HttpClient('https://example.com/raw'))->put('raw body');
$response = (new HttpClient('https://example.com/raw'))->patch('patch body');
// OPTIONS
$response = (new HttpClient('https://example.com/resource'))->options(
null,
['Access-Control-Request-Method' => 'POST']
);
$jsonResponse = (new HttpClient('https://example.com/api'))->postJson(
json_encode(['name' => 'EasySwoole'], JSON_THROW_ON_ERROR)
);
$xmlResponse = (new HttpClient('https://example.com/xml'))->postXml(
'<request><name>EasySwoole</name></request>'
);
$client = new HttpClient('https://example.com/profile');
$client->setHeader('X-Request-ID', 'CaseSensitive-ID');
$client->setHeaders([
'Accept' => 'application/json',
'X-Client' => 'demo',
]);
$client->addCookie('session', 'token');
$client->addCookies(['locale' => 'zh-CN']);
$client->setBasicAuth('username', 'password');
$response = $client->get();
use EasySwoole\HttpClient\Bean\CURLFile;
use EasySwoole\HttpClient\HttpClient;
$client = new HttpClient('https://example.com/upload');
$response = $client->post([
'description' => 'avatar',
'native_file' => new \CURLFile('/path/to/avatar.png'),
'client_file' => new CURLFile(
'/path/to/document.pdf',
'document',
'application/pdf'
),
]);
$client = new HttpClient('https://example.com/archive.zip');
$response = $client->download('/tmp/archive.zip');
if ($response === false) {
echo "download failed\n";
} else {
echo $response->getStatusCode();
}
// 从指定偏移量继续下载
$offset = file_exists('/tmp/archive.zip') ? filesize('/tmp/archive.zip') : 0;
$response = $client->download('/tmp/archive.zip', $offset);
$client = new HttpClient('https://example.com/old-path');
$client->setFollowLocation(5);
$response = $client->get();
// 禁止自动重定向
$client->setFollowLocation(0);
$client = new HttpClient('https://example.com/api');
$client
->setTimeout(10.0)
->setConnectTimeout(3.0)
->setReadTimeout(5.0)
->setWriteTimeout(5.0)
->setKeepAlive(true);
$client->setClientSetting('defer', false);
$client->setClientSettings([
'timeout' => 10,
'keep_alive' => true,
]);
$client = new HttpClient('https://example.com');
$client->setSslVerifyPeer(true);
$client->setSslHostName('example.com');
$client->setSslCafile('/path/to/ca.pem');
// 双向 TLS
$client->setSslCertFile('/path/to/client.crt');
$client->setSslKeyFile('/path/to/client.key');
$client->setSslPassphrase('secret');
$client = new HttpClient('https://example.com');
$client->setProxyHttp('127.0.0.1', 8080, 'user', 'password');
$client->setProxySocks5('127.0.0.1', 1080, 'user', 'password');
$client->setSocketBind('192.168.1.10', 0);
use EasySwoole\HttpClient\HttpClient;
use Swoole\WebSocket\Frame;
$client = new HttpClient('ws://127.0.0.1:9501/chat');
if ($client->upgrade()) {
$frame = new Frame();
$frame->data = json_encode(['action' => 'ping']);
$client->push($frame);
$reply = $client->recv(3.0);
var_dump($reply?->data);
}
$response->getStatusCode();
$response->getHeaders();
$response->getBody();
$response->getCookies();
$response->getErrCode();
$response->getErrMsg();
$array = $response->json(true);
$object = $response->json();
$array = $response->jsonp(true);
$xml = $response->xml();