Page 1 of 1

Hot to get content of a webpage without getting redirected

Posted: Tue Jun 28, 2011 12:06 pm
by kunal333
Hi,

I am new to PHP and need some help to get past below huddle.

What I am trying to do is read the contents of a webpage. Below program works good for most of the website but fails when the page is redirected to another page.

PHP Code:
function get_url_contents($url){
$crl = curl_init();
$timeout = 10;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($crl, CURLOPT_AUTOREFERER, FALSE);
curl_setopt ($crl, CURLOPT_FOLLOWLOCATION, TRUE);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}

$content= get_url_contents("http://www.foo.com");
echo $content;

I tried creating a html link and if I click on that link it does not get redirect but lands on the right page.
<a href ="http://foo.com">Foo</a>

My question is - how can I do this in PHP? I understand that Server is forcing it to re-direct but why is it not doing for HTML code?

Thanks in advance!

Re: Hot to get content of a webpage without getting redirect

Posted: Tue Jun 28, 2011 12:31 pm
by Jade
If scripts on foo.com is looking for a REFERRER before it redirects that may be the problem. You have CURLOPT_AUTOREFERER set to false.