Curl: Showing "Can't resolve domain name" error of the all localhost sites from PHP
Asked Answered
T

1

11

Today my cURL is starting to show me this error Could not resolve: {local-domain.test} (domain name not found) All working fine if I'm trying to get some info from an external link, but local sites aren't working properly.

So, here is my code

public function send($type, $url, $data)
    {
        $type = strtolower($type);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        if ($type == 'post') {
            $urlWithData = http_build_query($data);
            curl_setopt($ch, CURLOPT_POST, count($data));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $urlWithData);
        }

        $result = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        $out = new \stdClass();
        $out->status = $status;
        $out->body = json_decode($result, true);

        return $out;
    }

All start to work if I will add that host into my /etc/hosts/ file but I have a huge list of the domain and before updating PHP all worked fine on my side. I'm using dnsmasq as a dynamic host resolving tool.

  • OS: MacOs High Siera
  • Apache: 2.4.37
  • PHP: Multiple PHP versions, from 5.6-7.3

Some debug info:

  • Ping to that domain from the terminal is working fine (showing localhost)
  • Everything is working fine even If I run curl -v local-domain.test from a terminal
  • I've tried to use CURLOPT_DNS_USE_GLOBAL_CACHEoption.
  • Cleared all DNS cache of my machine from a terminal

Maybe I forgot to add something to the php.ini of each PHP? Also, all worked fine before I've made updating process of them all.

Tunisia answered 7/2, 2019 at 10:37 Comment(2)
Experiencing this same issue. I did not have this issue on php 7.3.0. This issue has existed on 7.3.1 and 7.3.2. I'm using Laravel Valet and there is something with the dns resolution with curl via php not using the rules given by dnsmasq. Editing /etc/hosts works just fine - not sure why dnsmasq is being ignoredFetal
Can confirm I also had to add my local site to the host file (E.G: 127.0.0.1 <your.domain>). PHP 7.2 using Valet+.Portemonnaie
N
2

This answer on this thread worked for me.

Running:

brew uninstall curl-openssl --ignore-dependencies

(removes the last version of curl-openssl, ensuring parity between curl versions used by your system and PHP)

... then followed by a valet restart

... allowed local curl to resolve Valet hostnames properly.

Needlepoint answered 4/4, 2020 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.