Windows – Linux – Mac OSX Guides

How to get HTML source code with PHP

Sometimes you may want to get the source code of an external HTML webpage. Well, here is the solution that might work in almost every cases

<?php
    $ch = curl_init();
    // the external url
    $url = 'http://google.com';
    curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt($ch,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 ($ch, CURLOPT_HTTPHEADER, array (
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language: en-us,en;q=0.5", "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
    ));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    // get the source code
    $html = curl_exec($ch);
    // print it out
    echo $html;
    curl_close($ch);
?>
Tagged with:     , , ,

Related Articles

Loading Facebook Comments ...

Post your comments

Your email address will not be published. Required fields are marked *