It’s very common task for web developers who want to download remote content of a particular URL, XML, RSS or even call to a API or web service. PHP’s cURL library which often comes with default configuration on popular shared hostings allows us to do that.
Get Web Page Content Function
<?php function get_webpage_content($webpage_url) { $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,$webpage_url); curl_setopt($curl_handle,CURLOPT_TIMEOUT,10); curl_setopt($curl_handle,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,10); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $html = curl_exec($curl_handle); curl_close($curl_handle); return $html; } ?> |
The Usage:
<?php echo get_webpage_content("http://4rapiddev.com/php/php-get-webpage-content-using-curl/"); ?> |
Or you can use the file_get_contents:
<?php echo file_get_contents("http://4rapiddev.com/php/php-get-webpage-content-using-curl/"); ?> |