PHP: How to download a webpage (aka web scrapping) with PHP PHP: Як для завантаження веб-сторінок (т.зв. веб металобрухт) з PHP
Posted on 03. Опублікований 03. Oct, 2009 by Dragos in Coding , PHP Жовтень, 2009 Dragos в кодування, PHP
There are many ways of downloading web pages, or web content. Є багато способів завантаження веб-сторінки або веб-контенту. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Особисто я хотів би використовувати CURL для моїх потреб веб злам, але іноді я також використовую fsockopen і file_get_contents.
Here are 3 different functions that will allow you to download web content. Тут знаходяться 3 різних функцій, які дозволять вам завантажувати веб-контенту.
cURL : CURL:
function getData($url) { Функція GetData ($ URL) ( if($url!='localhost' && $url!='http://localhost') { If ($ URL! = 'Localhost' & & $ URL! = 'Http://localhost') ( $ch=curl_init(); $ CH = curl_init (); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ гл, CURLOPT_URL, $ URL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt ($ гл, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.3"); curl_setopt ($ гл, CURLOPT_USERAGENT, "Mozilla/6.0 (compatible; Windows NT 5.1; EN-US; RV: 1.9.0.1) Gecko/2008070208 Firefox/3.0.3"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,3); curl_setopt ($ гл, CURLOPT_FOLLOWLOCATION, 3); $result['data']=curl_exec($ch); $ Result [ 'дані'] = curl_exec ($ CH); $result['error']=curl_error($ch); $ Result [ 'Error'] = curl_error ($ CH); curl_close($ch); curl_close ($ CH); return $result; повернути $ результат; } ) else return $result['error']='err'; ELSE RETURN $ результат [ 'Error'] = 'ERR'; } )
fsockopen fsockopen
function getData($url) { $arr=parse_url($url); $fp = fsockopen($arr['host'], 80, $errno, $errstr, 30); if(!$fp) { return false; }else { // send headers $out = "GET ".fsockopen($arr['host'], 80, $errno, $errstr, 30)." HTTP/1.1\r\n"; $out .= "Host: ".str_replace('http://'.$arr['host'],'',$url)."\r\n"; $out .= "User-Agent: FSOCKOPEN\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while(!feof($fp)) { $contents .= fgets($fp, 4096); }; fclose($fp); return $contents; } } Функція GetData ($ URL) ($ приб = parse_url ($ URL); $ FP = fsockopen ($ Arr [ 'Host'], 80, $ помилка, $ errstr, 30), якщо (! $ FP) (Return FALSE; інше) (/ / відправити заголовки $ OUT = "GET". fsockopen ($ Arr [ 'Host'], 80, $ помилка, $ errstr, 30). "HTTP/1.1 \ N"; $ з .= " Host: ". str_replace ( 'http://'. $ Arr [ 'URL приймаючої'],'',$)." \ R \ N"; $ з .= "User-Agent: FSOCKOPEN \ R \ п" ; $ з .= "Connection: Close \ R \ N \ R \ п"; FWRITE ($ FP, $ Out), у той час (! feof ($ FP)) ($ Contents .= ЕдеЬз ($ FP, 4096); ); FCLOSE ($ FP); повернути $ вмісту;)) file_get_contents file_get_contents
function getData($url) { Функція GetData ($ URL) ( return file_get_contents($url); повернення file_get_contents ($ URL); } )
As you see the easiest way of downloading web content is by using the file_get_contents function, but if you need more options, especially if you are working with the headers, then cURL is the best way to go for you. Як бачите, найпростіший спосіб завантаження веб-контенту полягає у використанні функції file_get_contents, але якщо вам потрібно більше можливостей, особливо якщо ви працюєте із заголовками, то CURL це найкращий шлях для вас.
Related posts: Схожі повідомлення:
- ferry ardhana Пором ardhana












































