swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.48k stars 3.16k forks source link

Co\Http\Client cannot resolve "localhost": DNS Lookup resolve failed #5269

Closed skyshore2001 closed 7 months ago

skyshore2001 commented 8 months ago

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error. I run swoole-cli 4.8.13 on windows 10 OS. I use Co\Http\Client to get http result of "http://localhost" but failed with error message: "DNS Lookup resolve failed".

example code:

<?php
use Swoole\Coroutine\Http\Client;
use function Swoole\Coroutine\run;

run(function () {
    $client = new Client('localhost', 80);
    $client->get('/');
    if ($client->errCode) {
        echo "error: " . $client->errMsg . "\n";
    }
    else {
        echo $client->body;
    }
    $client->close();
});
  1. What did you expect to see? the http result.

  2. What did you see instead? "error: DNS Lookup resolve failed"

  3. What version of Swoole are you using (show your php --ri swoole)? swoole-cli 4.8.13 on Windows 10 OS. It cannot handle DNS resolving if the name is "localhost" or someone defined in C:\Windows\System32\drivers\etc\hosts (similar to /etc/hosts on Linux). But the code works on Linux and WSL env.

  4. What is your machine environment used (show your uname -a & php -v & gcc -v) ? uname -a: MSYS_NT-10.0-19045 lj-laptop 3.3.3-341.x86_64 2022-01-17 11:45 UTC x86_64 Msys swoole-cli --version Swoole 4.8.13 (cli) (built: Feb 23 2023 12:05:55) (NTS)

matyhtf commented 7 months ago

Try using the IP address instead. Additionally, use ping or gethostbyname to resolve the domain name and observe if it can be successfully resolved.