Reputation: 1835
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
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";
?>
I'm not using WAMP or XAMPP server. The above code runs directly on the server. I've no idea what's going wrong.
Upvotes: 7
Views: 50243
Reputation: 101
disable SELinux if you are on Centos or Fedora or any Redhat Distro
nano /etc/selinux/config
Change
SELINUX=enforcing
to
SELINUX=disabled
Upvotes: 0
Reputation: 3067
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.
Upvotes: 10