简体   繁体   English

如何解决 CURL 请求错误“无法解析主机:...” PHP?

[英]How to solve CURL request error "Could not resolve host: ..." PHP?

I'm facing this very odd error.我正面临这个非常奇怪的错误。 I can't send a cURL request anymore after changing nothing.什么都没有改变后,我无法再发送 cURL 请求。 Out of the sudden I'm getting:突然我得到:

'Could not resolve host: hostname' '无法解析主机:主机名'

I know there are like a 1000 instances of this question on stackoverflow but I have tried all the suggested answers/comments in them for the past 5 hours and nothing seems to work.我知道在 stackoverflow 上有 1000 个这个问题的实例,但在过去的 5 个小时里我已经尝试了所有建议的答案/评论,但似乎没有任何效果。 This is my code for the request:这是我的请求代码:

<?php

class APIService
{
    private $host;
    private $key;

    function __construct($host, $key)
    {
        $this->host = $host;
        $this->key = $key;
    }

    function curlRequest($config)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('api:'.$this->key));
        curl_setopt($ch, CURLOPT_URL, $this->host.$config);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $curlSession = curl_exec($ch);

        return $this->decodeJSON($curlSession);
    }

    function getSomeList()
    {
        return $this->curlRequest('/folder/list');
    }

    private function decodeJSON($someList)
    {
        return json_decode($someList, true);
    }
}

The host is local and using docker ps I can see that is running.主机是本地的,使用docker ps我可以看到它正在运行。 Everything worked fine a few days ago and I made absloutly no changes to it or to any component that may have an effect on it but it doesn't work out of the sudden.几天前一切正常,我绝对没有对它或任何可能对其产生影响的组件进行任何更改,但它不会突然起作用。 Has anyone has any ideas why this is happening and how I could fix it?有没有人知道为什么会发生这种情况以及我该如何解决?

I've had this problem before.我以前遇到过这个问题。 I see in the chat that you said you had a backend and a frontend that you're putting on the same network and are using Docker.我在聊天中看到你说你有一个后端和一个前端,你放在同一个网络上并使用 Docker。 I think the problem is occuring because something went wrong with the virtual netwrok between your front and backend.我认为问题的发生是因为您的前端和后端之间的虚拟网络出现问题。 Try the following:请尝试以下操作:

1- Make sure both the front and backend's containers are up. 1- 确保前端和后端的容器都已启动。

2- Ensure that they're in the same netwrok using the following commands: 2- 使用以下命令确保它们在同一个网络中:

  • Make a network docker network create some-network-name-or-the-name-you-already-had docker network create some-network-name-or-the-name-you-already-had一个网络docker network create some-network-name-or-the-name-you-already-had
  • Add the backend to the network docker network connect some-network-name-or-the-name-you-already-had your-backend-webserver-name (if you don't know the name of your backend webserver use docker ps to view it.将后端添加到网络docker network connect some-network-name-or-the-name-you-already-had your-backend-webserver-name (如果您不知道后端网络服务器的名称,请使用docker ps查看它。
  • Add the frontend to the network docker network connect some-network-name-or-the-name-you-already-had your-frontend-webserver-name .将前端添加到网络docker network connect some-network-name-or-the-name-you-already-had your-frontend-webserver-name

Now I'm unsure of this but, whenever the container of the frontend or the backend stops, you have (after starting the container using docker-compose up -d ) each time to add it to the network.现在我不确定这一点,但是,每当前端或后端的容器停止时,您(在使用docker-compose up -d启动容器后)每次都将其添加到网络中。 There might be a way around this, but I'm not aware of it.可能有办法解决这个问题,但我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM