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 от Драгош в кодиране, 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 . Лично аз да използвате навийте за моите нужди в Мрежата бракуване, но понякога също така да използвате и fsockopen file_get_contents.
Here are 3 different functions that will allow you to download web content. Тук са 3 различни функции, които ще ви позволи да изтеглите уеб съдържание.
cURL : навийте:
function getData($url) { функция getData ($ URL) ( if($url!='localhost' && $url!='http://localhost') { ако ($ URL! = 'Localhost "& & $ URL! =' http://localhost ') ( $ch=curl_init(); $ CH = curl_init (); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ CH, CURLOPT_URL, $ URL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt ($ CH, 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 ($ 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($ch, CURLOPT_FOLLOWLOCATION,3); curl_setopt ($ CH, CURLOPT_FOLLOWLOCATION, 3); $result['data']=curl_exec($ch); $ резултат [ "данни"] = curl_exec ($ ч); $result['error']=curl_error($ch); $ резултат [ 'грешка'] = curl_error ($ ч); curl_close($ch); curl_close ($ ч); return $result; върне $ резултат; } ) else return $result['error']='err'; иначе върне $ резултат [ 'грешка'] = 'греша "; } )
fsockopen fsockopen
function getData($url) { функция getData ($ URL) ( $arr=parse_url($url); $ ARR = parse_url ($ URL); $fp = fsockopen($arr['host'], 80, $errno, $errstr, 30); $ РП = fsockopen ($ ARR [ 'домакин'], 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 = "GET". fsockopen ($ ARR [ 'домакин'], 80, $ errno, $ errstr, 30). "HTTP/1.1 \ R \ N"; $out .= "Host: ".str_replace('http://'.$arr['host'],'',$url)."\r\n"; $ посочени .= "Host:". str_replace ( "http://". $ ARR [ 'URL домакин'],'',$). "\ R \ N"; $out .= "User-Agent: FSOCKOPEN\r\n"; $ посочени .= "User-Agent: FSOCKOPEN \ R \ N"; $out .= "Connection: Close\r\n\r\n"; $ посочени .= "Connection: Близо \ R \ N \ R \ N"; fwrite($fp, $out); fwrite ($ РП, $ изход); while(!feof($fp)) { докато (! feof ($ РП)) ( $contents .= fgets($fp, 4096); $ съдържанието .= fgets ($ РП, 4096); }; ); fclose($fp); fclose ($ РП); 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, но ако имате нужда от повече възможности, особено ако работите със заглавията, след това навийте е най-добрият начин да се отиде за вас.
Related posts: Свързани пунктове:
- ferry ardhana ферибот ardhana












































