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

如何解决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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题