Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: CURL is enabled but has no effect

Tags:

php

curl

I'm trying to get data from external website using cURL in PHP but, somehow it's not working.

I've checked out that CURL enable in phpinfo(). It shows cURL is enabled cURL is enabled

But, my code is not working.

<?php
if (! function_exists ( 'curl_version' )) {
    exit ( "Enable cURL in PHP" );
}

$ch = curl_init ();
$timeout = 0; // 100; // set to zero for no timeout
$myHITurl = "http://www.google.com";
curl_setopt ( $ch, CURLOPT_URL, $myHITurl );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
$file_contents = curl_exec ( $ch );
if (curl_errno ( $ch )) {
    echo curl_error ( $ch );
    curl_close ( $ch );
    exit ();
}
curl_close ( $ch );

// dump output of api if you want during test
echo "$file_contents";
?>


It goes timeout.
Connection Timeout

I'm not using WAMP or XAMPP server. The above code runs directly on the server. I've no idea what's going wrong.

like image 421
Kasim Rangwala Avatar asked Nov 30 '15 07:11

Kasim Rangwala


1 Answers

Your code is perfect, I have tested it on my own server (data center in Texas) and it worked fine.

My guess is that your server IP is banned. Try to fetch a different URL, and see if it works for you. If it does then you are banned, if it doesn't then it might be a firewall configuration issue in your server.

like image 50
Aviram Avatar answered Oct 15 '22 17:10

Aviram