7

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.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Kasim Rangwala
  • 1,765
  • 2
  • 23
  • 44

2 Answers2

10

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.

Aviram
  • 3,017
  • 4
  • 29
  • 43
  • Let me checkout for `Firewall` issue. but, I can't understand about `IP is banned`. can you tell me in brief. How can I check if my IP is banned. – Kasim Rangwala Nov 30 '15 at 07:48
  • 1
    I can't tell you for sure if it is banned or not, because it can also be a firewall configuration issue. Another thing you could try is to run `telnet www.google.com 80` in the terminal and see if you can connect. Try fetching a different URL, like http://www.example.com, in your code and see if it works. Good luck! – Aviram Nov 30 '15 at 07:52
  • 3
    My `80` & `443` port are blocked from server side. Thanks for your Answer @Aviram – Kasim Rangwala Nov 30 '15 at 07:57
0

disable SELinux if you are on Centos or Fedora or any Redhat Distro

nano /etc/selinux/config

Change

SELINUX=enforcing

to

SELINUX=disabled