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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同