note 108881 modified in function.curl-setopt by danbrown
| From: | danbrown@php.net | Date: | Thu, 31 May 2012 17:54:56 +0000 |
| Subject: | note 108881 modified in function.curl-setopt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188350@lists.php.net to get a copy of this message | ||
Sometimes we want to extract the HTML content of the remote website page, this technique is called
as HTML scrapper. This article will discuss on how we can extract the HTML content of the remote
webpage. We can achieve HTML scrapper operation in 2 step
operation: Call to Remote Web Page and extract the HTML
content. Match the HTML tags using Regular Expression. Call to
Remote Web Page using PHP: In PHP there are various ways we can call the remote
webpage. But here we will be using CURL to achieve our
operation. <?php $ch =
curl_init(); $timeout = 5; // set to zero for no
timeout curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch,
CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,
$timeout); $file_contents =
curl_exec($ch); curl_close($ch);
preg_match_all('/<span>[\\/\\(\\)-:<>\\w\\s]+<
\\/span>/',$file_contents,$htmlContent); ?>
--was--
Sometimes we want to extract the HTML content of the remote website page, this technique is called
as HTML scrapper. This article will discuss on how we can extract the HTML content of the remote
webpage.
We can achieve HTML scrapper operation in 2 step operation:
Call to Remote Web Page and extract the HTML content.
Match the HTML tags using Regular Expression.
Call to Remote Web Page using PHP:
In PHP there are various ways we can call the remote webpage, for more information on connecting to
remote web page we can refer to . But here we will be using CURL to achieve our operation.
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
preg_match_all('/<span>[\/\(\)-:<>\w\s]+<
\/span>/',$file_contents,$htmlContent);
For more details readt the full article
http://www.codingquery.com/html-scrapper-in-php-source-code
http://php.net/manual/en/function.curl-setopt.php