Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows swoole-cli-v4.8.13-cygwin-x64.zip 在windows上 run(function{}) 内的curl 问题 #5328

Open
Dadiaojin opened this issue May 17, 2024 · 2 comments

Comments

@Dadiaojin
Copy link

Dadiaojin commented May 17, 2024

该问题出现在本人公司电脑上(家里的电脑正常)
环境:swoole下载网站 : https://www.swoole.com/download 版本swoole-cli-v4.8.13-cygwin-x64.zip 电脑操作系统:windows10
windows cmd窗口运行命令:swoole-cli.exe .\t7.php
问题是: 运行下面代码时

<?php
Co\run(function () {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.2");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    var_dump(curl_error($ch));
    curl_close($ch);
    var_dump($result);
});

返回:
string(26) "Couldn't connect to server"
bool(false)

但是以下代码却是正常的

<?php
Co\run(function () {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    var_dump(curl_error($ch));
    curl_close($ch);
    var_dump($result);
});

返回:
string(0) ""
string(15) "127.0.0.1Hi!"

他们上面2个的区别是一个是请求127.0.0.1 ,一个是 127.0.0.2 。 那么有当时我觉得可能是 127.0.0.2的问题? 但是我把Co\run(function () {}) 去掉后,结果却正常返回。代码如下:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.2");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    var_dump(curl_error($ch));
    curl_close($ch);
    var_dump($result);

返回值:
string(0) ""
string(15) "127.0.0.1Hi,Yg!"

所以由此得出是 该电脑 swoole-cli.exe 单纯使用curl正常 但是协程使用curl 无法访问 除了 127.0.0.1 或 localhost 的网站。我应该怎么排查和解决这个问题?

@NathanFreeman
Copy link
Member

php -S 127.0.0.2:8080

我用php监听了本地的127.0.0.2:8080是可以访问的

你的127.0.0.2是怎么监听的

@Dadiaojin
Copy link
Author

php -S 127.0.0.2:8080

我用php监听了本地的127.0.0.2:8080是可以访问的

你的127.0.0.2是怎么监听的

我使用的是phpstudy的阿帕奇 监听的,在浏览器上可以正常访问。但是在 Co\run(function () {})外使用curl可以正常访问,在 Co\run(function () {})内curl就是不能访问。 是不是我的公司电脑上装了很多VPN(连接其他公司内网的那种),难道是那些vpn导致的swoole问题?因为我家里的电脑没有安装那些vpn,是可以正常连接的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants