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 ต.ค., 2,009 โดย 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 . ตัวฉันต้องการจะใช้ ม้วน สำหรับความต้องการส่งเรื่องที่สนใจเว็บของฉันแต่บางครั้งฉันยังใช้ 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(); ฃ $ = curl_init (); curl_setopt($ch, CURLOPT_URL, $url); บริษัท curl_setopt ($, CURLOPT_URL, $ url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); บริษัท curl_setopt ($, CURLOPT_RETURNTRANSFER ทรู); 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 (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); ผล $ [ 'ข้อมูล'] = curl_exec ($ CH); $result['error']=curl_error($ch); ผล $ [ 'ผิดพลาด'] curl_error = ($ CH); curl_close($ch); curl_close ($ CH); 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); $ fp = fsockopen ($ arr [ 'เจ้าบ้าน'], 80, $ errno, $ errstr, 30); if(!$fp) { หาก (! $ 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"; $ 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)) { ในขณะที่ (feof! ($ fp)) ( $contents .= fgets($fp, 4096); เนื้อหา $ 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 แต่หากคุณต้องการตัวเลือกมากขึ้นโดยเฉพาะหากคุณทำงานกับส่วนหัวที่ ม้วน แล้ววิธีที่ดีที่สุดสำหรับคุณคือ.
No related posts. ไม่มีบทความที่เกี่ยวข้อง.
- ferry ardhana ardhana เรือข้ามฟาก












































