Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

curl

dnsmasq

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.

like image 316
alexey-novikov Avatar asked Feb 07 '19 10:02

alexey-novikov


1 Answers

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.

like image 143
jpcaparas Avatar answered Sep 26 '22 23:09

jpcaparas