Saturday, August 11, 2012

Use php curl to get web page

If you want PHP to obtain the source code of a page, this function can help you. This function uses the PHP CURL technology, you can modify the OPTION parameter, the function returns an array, which contains the page source code .below is an example, hope that can help you, thank you!

Exported from Notepad++
<?php function get_page( $url ){ $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // not reutrn header CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1", // set UserAgent CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 60, // timeout on connect CURLOPT_TIMEOUT => 60, // timeout on response CURLOPT_MAXREDIRS => 10, // stop after 10 redirects ); $ch = curl_init( $url ); curl_setopt_array( $ch, $options ); $content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); $header['erron'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header; } $arr=get_page("http://www.google.com"); print_r($arr); ?>

1 comment: