file_get_contents 无法打开流:无法分配请求的地址

Dex*_*aru 2 php nginx php-7

当我尝试将 url 传递给 file_get_contents 时,我得到了这个

警告:file_get_contents(http://localhost:8001/els/get_data_from_radius.php?id=1&u=GERONIMO-ANGELA-GLADIS-17010-16407&p=5T7U5756876O93FPG7F2B56B89359488&s=1&action=2&ud=524288/5242880):失败打开流:无法分配请求地址位于 /var/local/entropia/model/MonitoreoRadiusWrapper.php第108

我的功能是

public static function activar_servicio($ws_host, $id_rad, $user, $pass, $simultaneo, $ud_mikrotik) {
    $url = $ws_host . ":8001/els/get_data_from_radius.php?id=$id_rad&u=$user&p=$pass&s=$simultaneo&action=2&ud=$ud_mikrotik";

    $result = file_get_contents($url);

    return json_decode($result);
}
Run Code Online (Sandbox Code Playgroud)

我在带有 nginx 的 docker 容器中使用 php 7。

Chr*_*ory 6

编辑:正如 Sven Tore 在评论中提到的,使用host.docker.internal应该是引用其他正在运行的容器的首选方式。 https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host

如果您的$ws_host设置为http://localhost,那么 docker 映像将尝试查询它自己的容器localhost,而不是其他容器。尝试更改http://localhost为您的主机的私有 IP。例子http://10.0.0.8。该 IP 必须是Mac 上en0.inet返回的值中您计算机的私有 IP 地址ifconfig。对于其他操作系统,您需要了解如何查找计算机的私有 IP 地址。

  • 考虑使用 http://host.docker.internal,在具有不同 IP 的计算机/网络之间切换时更容易 https://docs.docker.com/desktop/networking/#use-cases-and-workarounds-for-all-platforms (2认同)