【问题标题】:file_get_contents failed to open stream: Cannot assign requested addressfile_get_contents 无法打开流:无法分配请求的地址
【发布时间】:2021-06-06 10:14:01
【问题描述】:

当我尝试将 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);
}

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

【问题讨论】:

  • 如果这是使用任何类型的代理,您可能需要通过传递给 file_get_contents 的流上下文指定,请参阅stackoverflow.com/a/53185479/1427878
  • 8001 是内部还是外部暴露的 Docker 端口?此外,您的代码是否有理由对其自身进行 API 调用?您是否可以只使用实际逻辑而无需额外的 HTTP 请求?
  • 8001 是外部端口。我真的不知道这是否有原因。我是这个项目的新手。

标签: php nginx php-7


【解决方案1】:

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

【讨论】:

  • 谢谢..它帮助了我!我可以使用以下方法找到我的私有 IP 地址:ipconfig 值:IPv4 地址。
猜你喜欢
  • 1970-01-01
  • 2021-11-03
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多