CURL and HTTPS, “Cannot resolve host”

前端 未结 11 1933
时光取名叫无心
时光取名叫无心 2020-11-28 08:25

I\'m trying to fetch the contents of a page using CURL. The page that is doing the fetching is https and the page it is trying to fetch is also https. I\'m getting an erro

相关标签:
11条回答
  • 2020-11-28 08:58

    We need to add host security certificate to php.ini file. For local developement enviroment we can add cacert.pem in your local php.ini.

    do phpinfo(); and file your php.ini path open and add uncomment ;curl.capath

    curl.capath=path_of_your_cacert.pem

    0 讨论(0)
  • 2020-11-28 09:02

    After tried all above, still can't resolved my issue yet. But got new solution for my problem.

    At server where you are going to make a request, there should be a entry of your virtual host.

    sudo vim /etc/hosts
    

    and insert

    192.xxx.x.xx www.domain.com

    The reason if you are making request from server to itself then, to resolve your virtual host or to identify it, server would need above stuff, otherwise server won't understand your requesting(origin) host.

    0 讨论(0)
  • 2020-11-28 09:03

    I found that CURL can decide to use IPv6, in which case it tries to resolve but doesn't get an IPv6 answer (or something to that effect) and times out.

    You can try the command line switch -4 to test this out.

    In PHP, you can configure this line by setting this:

    curl_setopt($_h, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    
    0 讨论(0)
  • 2020-11-28 09:04

    Maybe a DNS issue?

    Try your URL against this code:

    $_h = curl_init();
    curl_setopt($_h, CURLOPT_HEADER, 1);
    curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($_h, CURLOPT_HTTPGET, 1);
    curl_setopt($_h, CURLOPT_URL, 'YOUR_URL' );
    curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
    curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
    
    var_dump(curl_exec($_h));
    var_dump(curl_getinfo($_h));
    var_dump(curl_error($_h)); 
    
    0 讨论(0)
  • 2020-11-28 09:04

    add yourlocalhost
    ex. 127.0.0.1 cards.localhost in the /etc/hosts directory. Now restart apache server

    0 讨论(0)
  • 2020-11-28 09:09

    Your getting the error because you're probably doing it on your Local server environment. You need to skip the certificates check when the cURL call is made. For that just add the following options

    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_SSL_VERIFYHOST,  0);
    
    0 讨论(0)
提交回复
热议问题