This is a translated page. The original can be found here: http://iwebdevel.com/2009/10/03/php-how-to-download-a-webpage-aka-web-scrapping-with-php-fsockopen-file_get_contents-curl-function-download-web-page/
UPDATES VIA RSS | Email Ενημέρωση μέσω RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / PHP / PHP: How to download a webpa… Αρχική Σελίδα / Κωδικοποίηση / PHP / PHP: Πώς να κατεβάσετε ένα webpa ...

PHP: How to download a webpage (aka web scrapping) with PHP PHP: Πώς να κατεβάσετε μια ιστοσελίδα (web διάλυση γνωστός) με την 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. Υπάρχουν πολλοί τρόποι για κατέβασμα ιστοσελίδων, ή περιεχομένου web. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Προσωπικά μου αρέσει να χρησιμοποιούν cURL για τις ανάγκες web διάλυση μου, αλλά μερικές φορές χρησιμοποιώ επίσης fsockopen και file_get_contents.

Here are 3 different functions that will allow you to download web content. Εδώ είναι 3 διαφορετικές λειτουργίες που θα σας επιτρέψει να κάνετε λήψη περιεχομένου web.

cURL : cURL:

function getData($url) { if($url!='localhost' && $url!='http://localhost') { $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); 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_FOLLOWLOCATION,3); $result['data']=curl_exec($ch); $result['error']=curl_error($ch); curl_close($ch); return $result; } else return $result['error']='err'; } getData λειτουργία ($ url) (if ($ url! = 'localhost' & & $ url! http://localhost = '') ($ ch = curl_init ()? curl_setopt ($ ch, CURLOPT_URL, $ url)? 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) Firefox/3.0.3 Gecko/2008070208")? curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 3)? $ result [ 'data'] = curl_exec ($ ch)? $ result [ 'error'] = curl_error ($ ch)? curl_close ($ ch)? return $ αποτέλεσμα?) 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); $ fp = fsockopen ($ arr [ 'υποδοχής'], 80, $ errno, $ errstr, 30)? 
     if(!$fp) { if (! $ fp) ( 
         return false; return false? 
     }else { ) else ( 
     // send headers / / Αποστολή 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"; $ out .= "Host:". str_replace ( "http://". $ arr [ 'url'],'',$ υποδοχής). "\ r \ n"? 
         $out .= "User-Agent: FSOCKOPEN\r\n"; $ out .= "User-Agent: fsockopen \ r \ n"? 
         $out .= "Connection: Close\r\n\r\n"; $ out .= "Connection: Close \ r \ n \ r \ n"? 
         fwrite($fp, $out); fwrite ($ fp, $ out)? 
         while(!feof($fp)) { while (! feof ($ fp)) ( 
             $contents .= fgets($fp, 4096); $ περιεχόμενο .= fgets ($ fp, 4096)? 
         }; )? 
         fclose($fp); fclose ($ fp)? 
         return $contents; return $ contents? 
     } ) 
 } ) 

file_get_contents file_get_contents

 function getData($url) { getData λειτουργία ($ url) ( 
 return file_get_contents($url); file_get_contents return ($ 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 είναι ο καλύτερος τρόπος να πάει για εσάς.

Translate this post Μεταφράστε αυτήν την θέση





No related posts. Δεν υπάρχουν σχετικές θέσεις.

    blog comments powered by Disqus blog παρατηρήσεις που κινούνται με Disqus