PHP: How to download a webpage (aka web scrapping) with PHP PHP: כיצד להוריד דף אינטרנט (aka מבטל אינטרנט) עם 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') { אם ($ 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) Firefox/3.0.3 Gecko/2008070208"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,3); curl_setopt ($ ch, 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 $ result [ 'error'] = 'לטעות'; } )
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) { אם (! $ fp) ( return false; return false; }else { אחר) ( // send headers / / לשלוח כותרות $out = "GET ".fsockopen($arr['host'], 80, $errno, $errstr, 30)." HTTP/1.1\r\n"; $ החוצה = "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: Close \ n R \ \ r \ n"; fwrite($fp, $out); fwrite ($ fp, $ out); while(!feof($fp)) { בזמן (! feof ($ fp)) ( $contents .= fgets($fp, 4096); $ contents .= fgets ($ 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 היא הדרך הטובה ביותר ללכת בשבילך.
No related posts. הודעות לא קשורות.
- ferry ardhana ardhana המעבורת












































