function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
i find this function from the internet. when i test it in a php file using this code $returned_content = get_data('http://google.com');
but it can't work.and get a "301 Moved Permanently" The document has moved here. error. why?
cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.
PHP cURL GET requestphp $ch = curl_init('http://webcode.me'); curl_exec($ch); curl_close($ch); In the example, we send a GET request to a small website. The output is directly shown in the standard output.
cURL is enabled by default but in case you have disabled it, follow the steps to enable it. Open php. ini (it's usually in /etc/ or in php folder on the server). Search for extension=php_curl.
According to your comments, you are getting a 302 status code. Try
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
to follow 30x redirects.
Manual on curl_setopt()
add one more option to your get_data function :
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
Google is redirecting you to the local google servers and your curl call currently isn't chasing redirects.
oh yeah,
and do a var_dump($returned_content);
to see the results :P
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With