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.

5
  • 1
    which OS you are using? Commented Nov 30, 2015 at 7:44
  • 2
    The error suggests that curl itself is working. This looks like a firewall issue on the host that is running the script. Commented Nov 30, 2015 at 7:45
  • @WisdmLabs server os is linux Commented Nov 30, 2015 at 7:49
  • 3
    My 80 & 443 port are blocked from server side. Thanks to both of you guys for helping me! Commented Nov 30, 2015 at 7:58
  • Is DNS issue... check stackoverflow.com/questions/24967855/… Commented Jan 4, 2018 at 14:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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 example.com, in your code and see if it works. Good luck!
My 80 & 443 port are blocked from server side. Thanks for your Answer @Aviram
0

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

nano /etc/selinux/config

Change

SELINUX=enforcing

to

SELINUX=disabled

1 Comment

On CentOS 8 use "sestatus" instead of "nano /etc/selinux/config" which gives "unknown".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.