skip to Main Content

I can use lynx to access a website from the server fine. When I try to access the same website with a php curl instruction I get http_code=>0 from curl_getinfo($ch);
The same php code on another redhat server in the same data centre works fine. I have admin access to the server.

I have tried php 7.4 and php 8.0
curl is enabled when I call phpinfo();
I am unsure how to debug this error!

It is RHEL 8.8

This is the output from curl_getinfo($ch);

Array ( [url] =>
https://…..
[content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.055895 [namelookup_time] => 0.105978
[connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0
[size_download] => 0 [speed_download] => 0 [speed_upload] => 0
[download_content_length] => -1 [upload_content_length] => -1
[starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] =>
[primary_ip] => [certinfo] => Array ( ) [primary_port] => 0
[local_ip] => [local_port] => 0 [http_version] => 0 [protocol] => 0
[ssl_verifyresult] => 0 [scheme] => [appconnect_time_us] => 0
[connect_time_us] => 0 [namelookup_time_us] => 105978
[pretransfer_time_us] => 0 [redirect_time_us] => 0
[starttransfer_time_us] => 0 [total_time_us] => 55895 )

2

Answers


  1. Chosen as BEST ANSWER
    setsebool -P httpd_can_network_connect on
    

    This worked. The full answer is here: CURL not working from web browser on Red Hat, Apache, PHP, AWS


  2. I can use lynx to access a website

    Probably you can’t access it via curl cli because of the cookies.

    # err
    $ curl https:// --verbose
    # suc
    $ curl https:// 
          --verbose 
          --cookie="/tmp/cookie.txt" 
          --cookie-jar="/tmp/cookie.txt"
    

    PHP representation

    $cookieFile = '/tmp/cookie.txt';
    $ch = curl_init($url);
    curl_setopt_array($ch, [
       CURLOPT_COOKIEJAR => $cookieFile,
       CURLOPT_COOKIEFILE => $cookieFile,
       // other options
    ]);
    curl_exec($ch);
    curl_close($ch);
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search