Hot to get content of a webpage without getting redirected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kunal333
Forum Newbie
Posts: 1
Joined: Tue Jun 28, 2011 11:58 am

Hot to get content of a webpage without getting redirected

Post 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!
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post 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.
Post Reply