CURL Error #:Failed to connect to URL port 443: Network is unreachable

Author - Komal Dhiman
14.01.2019
|
0 Comments
|

CURL Error #:Failed to connect to URL port 443: Network is unreachable

Calling an API through CURL sometimes leads to this error –

CURL Error #:Failed to connect to URL port 443: Network is unreachable

Where, URL is the url with which we try to connect.
This error can occur on connecting to any API through the CURL request.

Example:

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
 CURLOPT_URL => "https://reportsapi.zoho.com/api/$email/$DB_name/$table_name?ZOHO_ACTION=EXPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_VERSION=1.0&authtoken=$auth_token",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_HTTPHEADER => array(
   "cache-control: no-cache"
 ),
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
 	echo "cURL Error #:" . $err;
} else {
 	echo $response;
}

This is a CURL request to access the Zoho reports table data through its API. In this URL –

"https://reportsapi.zoho.com/api/$email/$DB_name/$table_name?ZOHO_ACTION=EXPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_VERSION=1.0&authtoken=$auth_token"

$email is the variable that has value for the email of Zoho reports account.
$DB_name contains the name of the database with which we are connecting.
$table_name is the name of the table to get the data from.
$auth_token contains the auth token we get from the zoho reports account to access its API.

Output:
CURL Error #:Failed to connect to reportsapi.zoho.com port 443: Network is unreachable

Solution

This issue usually occurs when using host names that resolve addresses using more than one version of IP. This curl request tries to use IPv6 even though it is not configured properly (the IPv6 network is unreachable) and does not retry with IPv4 after a failure. In order to resolve it, we just need to specify which IP protocol version to use. We can specify the IP protocol using curl_setopt function. It can have one of these three values –

CURL_IPRESOLVE_WHATEVER resolves addresses to all IP versions that your system allows. It is default option.
CURL_IPRESOLVE_V4 specifies the IPv4 version.
CURL_IPRESOLVE_V6 specifies the IPv6 version.

Example:

To resolve this issue, add this line to the CURL request

curl_setopt( $curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

just before its execution through

$response = curl_exec($curl);

Now, execute it again.

Output:
API through CURL

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *