note 108881 modified in function.curl-setopt by danbrown

From: 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.&#13;&#10;We can achieve HTML scrapper operation in 2 step operation:&#13;&#10;Call to Remote Web Page and extract the HTML content.&#13;&#10;Match the HTML tags using Regular Expression.&#13;&#10;Call to Remote Web Page using PHP:&#13;&#10;In PHP there are various ways we can call the remote webpage. But here we will be using CURL to achieve our operation.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$ch = curl_init();&#13;&#10;$timeout = 5; // set to zero for no timeout&#13;&#10;curl_setopt ($ch, CURLOPT_URL, $url);&#13;&#10;curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);&#13;&#10;curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);&#13;&#10;$file_contents = curl_exec($ch);&#13;&#10;curl_close($ch);&#13;&#10;&#13;&#10; preg_match_all(&#39;/&#60;span&#62;[\\/\\(\\)-:&#60;&#62;\\w\\s]+&#60; \\/span&#62;/&#39;,$file_contents,$htmlContent);&#13;&#10;?&#62; --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

« previous php.notes (#188350) next »