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) { Функция GetData ($ URL) ( $arr=parse_url($url); $ приб = parse_url ($ URL); $fp = fsockopen($arr['host'], 80, $errno, $errstr, 30); $ FP = fsockopen ($ Arr [ 'Host'], 80, $ ошибка, $ errstr, 30); if(!$fp) { IF (! $ FP) ( return false; Return FALSE; }else { () другое // send headers / / Отправить заголовки $out = "GET ".fsockopen($arr['host'], 80, $errno, $errstr, 30)." HTTP/1.1\r\n"; $ OUT = "GET". fsockopen ($ Arr [ 'Host'], 80, $ ошибка, $ errstr, 30). "HTTP/1.1 \ п"; $out .= "Host: ".str_replace('http://'.$arr['host'],'',$url)."\r\n"; $ из .= "Host:". str_replace ( 'http://'. $ Arr [ 'URL принимающей'],'',$). "\ R \ п"; $out .= "User-Agent: FSOCKOPEN\r\n"; $ из .= "User-Agent: FSOCKOPEN \ R \ п"; $out .= "Connection: Close\r\n\r\n"; $ из .= "Connection: Close \ R \ N \ R \ п"; fwrite($fp, $out); FWRITE ($ FP, $ OUT); while(!feof($fp)) { WHILE (! feof ($ FP)) ( $contents .= fgets($fp, 4096); Содержание .= $ ЕдеЬз ($ FP, 4096); }; ); fclose($fp); FCLOSE ($ FP); return $contents; вернуть $ содержимого; } ) } )
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












































