uccdev
4 months ago
723
6
Laravel

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

Posted 4 months ago by uccdev

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

Please sign in or create an account to participate in this conversation.