doubei5310 2019-06-19 21:16
浏览 227
已采纳

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

I'm facing this very odd error. I can't send a cURL request anymore after changing nothing. 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. 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. 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?

  • 写回答

1条回答 默认 最新

  • douxian4376 2019-06-22 08:33
    关注

    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. 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.

    2- Ensure that they're in the same netwrok using the following commands:

    • Make a network 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.
    • Add the frontend to the network 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. There might be a way around this, but I'm not aware of it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现