Forum Moderators: coopster

Message Too Old, No Replies

how to fetch remote page using fopen and change user agent

         

lindajames

10:45 pm on Oct 13, 2005 (gmt 0)

10+ Year Member



hi everyone,

i have the following code that fetches a remote webpage:


$open = fopen("http://www.example.com/page.htm", "r");
$read = fread($open, 9024);
fclose($open);
echo $read;

while fetching the remote webpage i want my code to tell the remote webpage that my user agent is NSPlayer/9.0.0.2980 WMFSDK/9.0 otherwise the webpage does not display the relevant information i need.

can anyone please tell me how i can do this?

thanx

R e b r a n d t

12:13 pm on Oct 14, 2005 (gmt 0)



You will need cURL support. Then the code will look smth like this:


$user_agent = 'NSPlayer/9.0.0.2980 WMFSDK/9.0';
$url='http://www.example.com/page.htm';
$cookies = 'cookies';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$html = curl_exec($ch);
$error = curl_error($ch);
echo (($error!= "")? ('Error: ' . $error) : $html);
curl_close($ch);

More info about cURL [php.net].

P.S.: Example has cookie support.