Tuesday, October 20, 2009

Read webpage or external file contents in php

The following are the different ways of reading a web page or external file contents in php.

$pageurl = "http://google.com";

//First Method
$r = new HTTPRequest($pageurl);
$html = $r->DownloadToString();
echo $html;


//Second Method
$html =file_get_contents($pageurl);
echo $html;


//Third Method (Using Curl) For this curl support should be enabled.
echo phpinfo();
//to check whether curl is enabled or not.

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_HEADER, 0); 
 // RETURN THE CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res=curl_exec($ch);
curl_close($ch);
print_r($res[2]);

No comments:

Post a Comment