walkor / http-client Goto Github PK
View Code? Open in Web Editor NEWAsynchronous http client for PHP based on workerman.
Asynchronous http client for PHP based on workerman.
Hi @walkor
How to make the HTTP response of Workerman HTTP server waits from the HTTP client to finish first? 🤔
<?php
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
$http_worker = new Worker('http://127.0.0.1:2345');
$http_client = new Workerman\Http\Client();
$http_worker->count = 1;
$http_worker->onMessage = function ($connection, $request) use ($http_client) {
$http_client->request('https://ifconfig.me/ip', [
'method' => 'GET',
'version' => '1.1',
'success' => function ($response) use ($connection) {
echo $response->getBody() . "\n";
},
'error' => function ($exception) {
echo $exception;
}
]);
$connection->send(""); // need to use $response->getBody() here
};
Worker::runAll();
<?php
namespace app\common;
use Monolog\Logger;
use Workerman\Http\Client;
class httpClient
{
private static Client $client;
public static function init(): void
{
if (!isset(self::$client)) {
$options = [
// 'max_conn_per_addr' => 128,
// 'keepalive_timeout' => 10,
// 'connect_timeout' => 10,
// 'timeout' => 10,
];
self::$client = new Client($options);
}
}
/**
* @param string $backUrl
* @param array $data
* @return false
* @throws \Throwable
*/
public static function RequestPost(string $backUrl, array $data): bool
{
self::init();
self::$client->post($backUrl, $data,
function ($response) use ($data, $backUrl, $log) {
$body = $response->getBody()->getContents();
if ($response->getStatusCode() === 200 && $body === 'success') {
//等待异步通知
}
},
function ($exception) use ($data, $backUrl) { //异常处理
}
);
return true;
}
}
很奇怪我有2个地方使用RequestPost 我有2个应用 一个APP,一个admin APP请求正常,admin请求会在控制台输出
POST / HTTP/1.1
Host: xxxxxxx
User-Agent: workerman/http-client
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 127
I decided use http client in my worker.
Server part works on workerman Worker with one thread ( $worker->count = 1; ) Business logic requires it.
Part of http client here :
$http = new Workerman\Http\Client(
['max_conn_per_addr' => 128,
'keepalive_timeout' => 15,
'connect_timeout' => 30,
'timeout' => 30,
]);
$t=microtime(1);
$http->post('http://127.0.0.1:8080', ['command'=>'ping'],
function ($response) { echo "ok\n"; },
function ($exception) use($t){ echo ' exception ['.substr($exception,0,57).']</b> diff:'.(microtime(1)-$t).s"\n"; }
);
So, when i post queries sequentially all work fine and quick. But if i send multiple quries - http client very quickly (0.1sec) recieve exception "Connection closed". I was expect that it will wait answer from server up to 30 seconds, but not.
If I use another asynchronous method of client operation via curl_multi I don’t observe such server behavior.
May be i need some tune of psr7 ? Or do you have any smart thoughts on this topic?
Thank you for your product and attention to it.
Hello!
When I make a request, I don't receive a response until a timeout is reached. In Request.php, function checkComplete waits the connection to be closed if empty Content-Length. Why? For responses with a code of 200 and not empty response body everything works correctly, but if the answer is 401 or 201 with empty body, we are waiting for the closing.
workerman/http-client/src/Request.php:321
$this->_response = new Response(
$parts[1],
$response_data['headers'],
'',
explode('/', $parts[0])[1],
isset($parts[2]) ? $parts[2] : null
);
环境:Workerman version:5.0.0-beta.7 PHP version:8.2.12 Event-loop:Workerman\Events\Revolt
目前当遇到网络波动或dns解析错误而连接失败时,无法处理错误,导致worker重启.
例子代码:
<?php
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
date_default_timezone_set( 'Asia/Shanghai' );
$worker = new Worker();
$worker->count = 1;
$worker->onWorkerStart = function($worker){
$http = new \Workerman\Http\Client();
$http->request('http://httpbin9.org/cookies', [
'success' => function ($response) {
echo $response->getBody();
},
'error' => function ($exception) {
echo $exception;
}
]);
//or 协程用法
try {
$response = $http->request('http://httpbin9.org/cookies');
echo $response->getBody();
} catch (\Throwable $th) {
var_dump('errrrr');
}
};
Worker::runAll();
是不是还不支持https,报错的内容如下:
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
Exception: Connection closed in /home/bdata/local/BData-service/vendor/workerman/http-client/src/Request.php:487
Stack trace:
#0 /home/bdata/local/BData-service/vendor/workerman/workerman/Connection/TcpConnection.php(949): Workerman\Http\Request->onUnexpectClose(Object(Workerman\Connection\AsyncTcpConnection))
#1 /home/bdata/local/BData-service/vendor/workerman/workerman/Connection/TcpConnection.php(776): Workerman\Connection\TcpConnection->destroy()
#2 /home/bdata/local/BData-service/vendor/workerman/workerman/Connection/TcpConnection.php(581): Workerman\Connection\TcpConnection->doSslHandshake(Resource id #31)
#3 /home/bdata/local/BData-service/vendor/workerman/workerman/Events/Select.php(294): Workerman\Connection\TcpConnection->baseRead(Resource id #31)
#4 /home/bdata/local/BData-service/vendor/workerman/workerman/Worker.php(2382): Workerman\Events\Select->loop()
#5 /home/bdata/local/BData-service/vendor/workerman/workerman/Worker.php(1490): Workerman\Worker->run()
#6 /home/bdata/local/BData-service/vendor/workerman/workerman/Worker.php(1320): Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))
#7 /home/bdata/local/BData-service/vendor/workerman/workerman/Worker.php(1294): Workerman\Worker::forkWorkersForLinux()
#8 /home/bdata/local/BData-service/vendor/workerman/workerman/Worker.php(509): Workerman\Worker::forkWorkers()
#9 /home/bdata/local/BData-service/httpclient.php(40): Workerman\Worker::runAll()
#10 {main}SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
服务器A:
$response = [
"hello": "123456"
];
$http->request($host,$response)->getBody()->getContents();
接收端B:
$request->only(['hello']);
"data": {
"hello": "123456"
}
这是发送端 还是接收端 没处理
When i send request to any web url with installed only TLS 1.3 i got error and connection close:
SSL handshake error. SSL routines::tlsv1 alert protocol version.
How fix and make support all the TLS versions (1.1, 1.2, 1.3)
Server installed
Ubuntu 24.04
PHP 8.3
OpenSSL 3.0
I need to connect through http proxy, how can I use it.
Is there a curl-like implementation?
I tried setting up the environment proxy on the system. But the connection still doesn't go through the proxy
Is that any ways to add the basic auth for the request?
浏览代码时,发现下面代码
$connection = $this->_connectionPool->fetch($address, strpos($url, 'https') === 0);
$connection->errorHandler = function(Throwable $exception) use ($task) {
$this->deferError($task['options'], $exception);
};
// No connection is in idle state then wait.
if (!$connection) {
return;
}
$connection->errorHandler = ...;
是不是要放在if (!$connection) {...}
后面?
how to wait/await http-request ?
请问请求如何使用代理服务器?
windows下循环100万次请求时,响应“Connection closed”
i'm using curl with workerman, it works fine, but i want to switch to async
I converted to this library, it works fine with html and css content, but when it encounters .woff and .ttf font files it gives an error
web_1 | Error: Call to a member function destroy() on null in /var/www/html/vendor/workerman/http-client/src/Request.php:453
web_1 | Stack trace:
web_1 | #0 /var/www/html/vendor/workerman/http-client/src/Request.php(379): Workerman\Http\Request->emitError()
web_1 | #1 /var/www/html/vendor/workerman/workerman/Connection/TcpConnection.php(660): Workerman\Http\Request->handleData()
web_1 | #2 [internal function]: Workerman\Connection\TcpConnection->baseRead()
web_1 | #3 /var/www/html/vendor/workerman/workerman/Events/Event.php(195): EventBase->loop()
web_1 | #4 /var/www/html/vendor/workerman/workerman/Worker.php(2430): Workerman\Events\Event->loop()
web_1 | #5 /var/www/html/vendor/workerman/workerman/Worker.php(1554): Workerman\Worker->run()
web_1 | #6 /var/www/html/vendor/workerman/workerman/Worker.php(1384): Workerman\Worker::forkOneWorkerForLinux()
web_1 | #7 /var/www/html/vendor/workerman/workerman/Worker.php(1358): Workerman\Worker::forkWorkersForLinux()
web_1 | #8 /var/www/html/vendor/workerman/workerman/Worker.php(1691): Workerman\Worker::forkWorkers()
web_1 | #9 /var/www/html/vendor/workerman/workerman/Worker.php(1640): Workerman\Worker::monitorWorkersForLinux()
web_1 | #10 /var/www/html/vendor/workerman/workerman/Worker.php(544): Workerman\Worker::monitorWorkers()
web_1 | #11 /var/www/html/index.php(174): Workerman\Worker::runAll()
web_1 | #12 {main}
跟有的框架不兼容了
guzzle/psr 2.x的 "psr/http-message": "^1.1 || ^2.0"
workerman/psr 的 "psr/http-message": "~1.0"
$http->request($url,$options);
你请求一个错误网址 会影响你代码后续操作
看了下源码,MS没有方法设置,忽略 SSL错误
When access to https I got error message :
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
How to skip SSL verification ?
thx
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.