dflow Posted July 16, 2009 Share Posted July 16, 2009 what is best way to get curl url source code? i want to get all the content between <html> </html> from a current url and save it as a variable then send it by email Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/ Share on other sites More sharing options...
MatthewJ Posted July 16, 2009 Share Posted July 16, 2009 <?php /** * Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an * array containing the HTTP server response header fields and content. */ function get_web_page( $url ) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_USERAGENT => "spider", // who am i CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // 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['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header; } $data = get_web_page('http://www.google.com'); echo $data['content']; ?> Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/#findComment-876577 Share on other sites More sharing options...
MatthewJ Posted July 16, 2009 Share Posted July 16, 2009 You probably need less options... I borrowed the script from http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/#findComment-876578 Share on other sites More sharing options...
dflow Posted July 16, 2009 Author Share Posted July 16, 2009 You probably need less options... I borrowed the script from http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl thanks ill check it out Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/#findComment-876583 Share on other sites More sharing options...
dflow Posted July 16, 2009 Author Share Posted July 16, 2009 i get a <?php /** * Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an * array containing the HTTP server response header fields and content. */ function get_web_page( $url ) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_USERAGENT => "spider", // who am i CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // 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['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header; } $data = get_web_page('http://www.google.com'); echo $data['content']; ?> i get an error on this line $ch = curl_init( $url ); Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/#findComment-876596 Share on other sites More sharing options...
MatthewJ Posted July 16, 2009 Share Posted July 16, 2009 You probably need to uncomment the curl dll in you php ini file ;extension=php_curl.dll Just remove the semicolon and make sure you restart the server Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/#findComment-876600 Share on other sites More sharing options...
phporcaffeine Posted July 16, 2009 Share Posted July 16, 2009 You can side step curl altogether if your server allows the use of wrappers ... <?php $content = file_get_contents('http://somewhere.com'); function get_string_between($string, $start, $end) { $string = " " . $string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string, $end, $ini) – $ini; return substr($string, $ini, $len); } echo get_string_between($content, '<html>', '</html>'); ?> Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/#findComment-876775 Share on other sites More sharing options...
MatthewJ Posted July 17, 2009 Share Posted July 17, 2009 I could be wrong, but I thought I read somewhere that curl was faster. My theory... whatever works Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/#findComment-877257 Share on other sites More sharing options...
dflow Posted July 21, 2009 Author Share Posted July 21, 2009 You can side step curl altogether if your server allows the use of wrappers ... hi im getting an error with $len = strpos($string, $end, $ini) – $ini; Parse error: syntax error, unexpected T_STRING Link to comment https://forums.phpfreaks.com/topic/166226-what-is-best-way-to-get-curl-url-source-code/#findComment-879438 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.