Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uccdev's avatar

Laravel cURL project works fine on local machine - FAILS on server?

I have a laravel project that makes a cURL request to a Canvas API endpoint. This project runs perfectly on my virtual machine. But on the centOS 7 server I've ported it to, its cURL requests don't work at all.

I get the following errors:

curl_getinfo($curl, CURLINFO_HTTP_CODE) returns curl error number 0. From what research I've done, this implies the server is refusing the connection

curl_exec($curl) returns false.

a dd of $curl or $resp dd($curl) returns a very empty string.

Below are some sample curl functions I use:

  //NOTE: $this->headers = ["Authorization: Bearer " . myServerAccessToken];
 //sets URL and $curl
 public function getAdmins() {
     $adminsURL = "https://my.test.instructure.com/api/v1/accounts/1/admins?per_page=20";
     $curl = curl_init();

     self::requestCurl($curl, $adminsURL, $this->headers);
     $resp = curl_exec($curl);
     dd($resp); //shows false
     return self::setHeaders($curl);
   }

  //something missing here?
 public function requestCurl($curl, $url, $headers) {
     return curl_setopt_array($curl, [
       CURLOPT_RETURNTRANSFER => TRUE,
       CURLINFO_HEADER_OUT => TRUE,
       CURLOPT_URL => $url,
       CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
       CURLOPT_SSL_VERIFYPEER => TRUE,
       CURLOPT_HTTPHEADER => $headers,
       CURLOPT_CUSTOMREQUEST => 'GET',
       CURLOPT_HEADER => TRUE
     ]);
   }

 //returns "curl errno: 0"
 public function setHeaders($curl) {
     $resp = curl_exec($curl);
     $header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE );
     $header = substr( $resp, 0, $header_size );
     $body = substr( $resp, $header_size); //$header_size
     $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
       echo "<h3>Curl errno: " . curl_getinfo($curl, CURLINFO_HTTP_CODE) . "</h3>";
       dd($body); //EMPTY.
       return null;  //TODO error handling (I think returning null will be enough?)
     }
     $data = json_decode($body, true);
     return $data;
   }
    

Can anyone suggest why this might be happening, and what I could try to fix it? Any advice would be appreciated

0 likes
6 replies
Braunson's avatar

Typically CURL error 0 means (host no found). Are you behind a firewall? Can you run this code outside the server. Things to note, is your url redirecting? You'll need to make it follow via CURLOPT_FOLLOWLOCATION = true. Don't forget to set the CURLOPT_MAXREDIRS to say 10 or whatever your comfortable with.

You could try bumping up the timeout limit?

Additionally use curl_error after the exec call to find the hidden errors;

print curl_error($ch);
1 like
sauravs012's avatar

check FIRST curl is installed on Centos 7 or NOT?

please run command curl -V .

1 like
uccdev's avatar

cURL is definitely installed, curl -V works fine

@braunson curl_error($ch) gave me the following:

 Failed connect to my.test.instructure.com:443; Operation now in progress

From what I've read, it seems this is a timeout error?

I think it might be the program being behind a firewall. How can I check if it is?

uccdev's avatar

Thank you for that. I tried both commands. To the curl --ipv4 -v "https://my.test.instructure.com/api/v1/accounts/1/admins?per_page=20" command, I got a full cURL log trace. Of the lines there, these two stood out:

HTTP/1.1 401 Unauthorized
...
"status":"unauthenticated","errors":[{"message":"user authorisation required"}

A check of my /etc/hosts file read like this:

 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 143.239.2.37 buzzi buzzi.website.com

What exactly should be there instead?

uccdev's avatar

As a followup to this, I've replaced buzzi with a different, matching server:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.65. myServer myServer.website.com

However, I still have a curl error number zero. This method here:

 //gets the data from a successful curl request, and returns said data
public function setHeaders($curl) {
  echo "<h1>SetHeaders()</h1>";
  $resp = curl_exec($curl);
  $header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE );
  $header = substr( $resp, 0, $header_size );
  $body = substr( $resp, $header_size); //$header_size
  $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
    echo "<h3>Curl errno: " . curl_getinfo($curl, CURLINFO_HTTP_CODE) . "</h3>";
    echo "<h3>Curl error(): " . curl_error($curl) . "</h3>";
    dd($body);
    return null; 
  }
  $data = json_decode($body, true);
  return $data;
}

This gives me the following output on the screen:

 "SetHeaders()"
 "Curl errno: 0"
 "Curl error(): Failed connect to my.test.instructure.com:443; Operation now in progress"

  "" [dd($body) returns an empty string]

I am trying to understand the curl error message: what does 443 mean? And why would a cURL request that succeeds on my local machine fail when coming from my server?

Any help would be very valuable.

Please or to participate in this conversation.