首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
再一个http请求中如何 异步发送一个http请求
### 问题描述 再一个http请求中如何 异步发送一个http请求 ### Swoole版本,PHP版本,以及操作系统版本信息 php PHP 7.4.2 (cli) (built: Feb 17 2020 12:56:02) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.5.2 Built => Feb 24 2021 19:21:10 coroutine => enabled debug => enabled trace_log => enabled kqueue => enabled rwlock => enabled pcre => enabled zlib => 1.2.11 brotli => E16777223/D16777223 async_redis => enabled Directive => Local Value => Master Value swoole.enable_coroutine => Off => Off swoole.enable_library => On => On swoole.enable_preemptive_scheduler => On => On swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 262144 => 262144 ### 相关代码 ```php <?php use function Swoole\Coroutine\run; use function Swoole\Coroutine\go; $http = new Swoole\Http\Server('127.0.0.1', 9501); $http->on('start', function ($server) { echo "Swoole http server is started at http://127.0.0.1:9501\n"; }); $http->on('request', function ($request, $response) { run(function() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://localhost:5200/'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); var_dump($result); }); $response->header('Content-Type', 'text/plain'); $response->end('Hello World'); }); $http->start(); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 请求'http://localhost:5200/ 会阻塞
发布于4年前 · 18 次浏览 · 来自
提问
哪吒
### 问题描述 再一个http请求中如何 异步发送一个http请求 ### Swoole版本,PHP版本,以及操作系统版本信息 php PHP 7.4.2 (cli) (built: Feb 17 2020 12:56:02) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.5.2 Built => Feb 24 2021 19:21:10 coroutine => enabled debug => enabled trace_log => enabled kqueue => enabled rwlock => enabled pcre => enabled zlib => 1.2.11 brotli => E16777223/D16777223 async_redis => enabled Directive => Local Value => Master Value swoole.enable_coroutine => Off => Off swoole.enable_library => On => On swoole.enable_preemptive_scheduler => On => On swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 262144 => 262144 ### 相关代码 ```php <?php use function Swoole\Coroutine\run; use function Swoole\Coroutine\go; $http = new Swoole\Http\Server('127.0.0.1', 9501); $http->on('start', function ($server) { echo "Swoole http server is started at http://127.0.0.1:9501\n"; }); $http->on('request', function ($request, $response) { run(function() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://localhost:5200/'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); var_dump($result); }); $response->header('Content-Type', 'text/plain'); $response->end('Hello World'); }); $http->start(); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 请求'http://localhost:5200/ 会阻塞
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2022-09-23
Rango
不要使用`Coroutine\run`这是协程容器的入口,改成使用`Coroutine\go`
赞
0
回复
2022-09-26
哪吒
`<?php $http = new Swoole\Http\Server('127.0.0.1', 9501); $http->on('start', function ($server) { echo "Swoole http server is started at http://127.0.0.1:9501\n"; }); $http->on('request', function ($request, $response) { go(function() { var_dump("send http start ==================================="); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://localhost:5200/'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); var_dump("send http end ==================================="); var_dump($result); }); go(function() { var_dump("test==================================="); }); $response->header('Content-Type', 'text/plain'); $response->end('Hello World'); }); $http->start();` http://localhost:5200/ 阻塞100秒 需要等请求http的协程结束后才能执行下面的协程、可以并行执行吗
赞
0
回复
2022-09-28
d
dadou
回复
哪吒
两个方案 1.使用swoole提供的http协程客户端. 2.开启curl协程化
赞
0
回复