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 التحديثات عن طريق آر إس إس | البريد الالكتروني Get updates via feedburner Get updates via twitter
Home / Coding / PHP / PHP: How to download a webpa… الوطن / الترميز / [فب] / [فب] : كيفية تنزيل webpa...

PHP: How to download a webpage (aka web scrapping) with PHP [فب] : كيفية تحميل صفحة ويب (ويعرف أيضا باسم الشبكة تخريد) مع بي

Posted on 03. نشرت بتاريخ 03. Oct, 2009 by Dragos in Coding , PHP أكتوبر ، 2009 من قبل دراكوش في الترميز ، [فب]

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) { جتدتا ظيفة (عنوان دولار) (
    if($url!='localhost' && $url!='http://localhost') { إذا دولار (رابط! = 'مضيف محلي' & & عنوان دولار! = 'http://localhost') (
        $ch=curl_init(); $ الفصل = curl_init () ؛
        curl_setopt($ch, CURLOPT_URL, $url); curl_setopt دولار (الفصل ، CURLOPT_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 (ويندوز ؛ يو ؛ الإقليم الشمالي ويندوز 5.1 ؛ اون الولايات المتحدة ؛ رف : 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); $ نتيجة [ 'بيانات'] = 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) { جتدتا ظيفة (عنوان دولار) (
    $arr=parse_url($url); $ كبريآء = parse_url (عنوان دولار) ؛
    $fp = fsockopen($arr['host'], 80, $errno, $errstr, 30); $ ع ع = fsockopen ($ كبريآء [ 'مضيفة'] ، 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"; دولار من أصل = "الحصول على". fsockopen ($ كبريآء [ 'مضيفة'] ، 80 ، $ errno ، $ errstr ، 30). "HTTP/1.1 \ ص \ ن" ؛
        $out .= "Host: ".str_replace('http://'.$arr['host'],'',$url)."\r\n"; دولار من أصل.= "المضيف". str_replace ( 'http://'. $ كبريآء [ 'المضيف']،''،$ رابط). "\ ص \ ن" ؛
        $out .= "User-Agent: FSOCKOPEN\r\n"; دولار من أصل.= "عامل المستخدم : FSOCKOPEN \ ص \ ن" ؛
        $out .= "Connection: Close\r\n\r\n"; .= دولار من أصل "اتصال : إغلاق \ ص \ ن \ ص \ ن" ؛
        fwrite($fp, $out); fwrite ($ تنظيم الأسرة ، إلى $) ؛
        while(!feof($fp)) { في حين (! feof ($ اف ب)) (
            $contents .= fgets($fp, 4096); $ محتويات.= fgets ($ تنظيم الأسرة ، 4096) ؛
        }; ) ؛
        fclose($fp); فكلوس ($ ع ع) ؛
        return $contents; العودة دولار محتويات ؛
    } )
} ) 

file_get_contents file_get_contents

 function getData($url) { جتدتا ظيفة (عنوان دولار) (
return file_get_contents($url); file_get_contents عودة (عنوان دولار) ؛
} ) 

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 ، ولكن اذا كنت بحاجة الى المزيد من الخيارات ، وخاصة إذا كنت تعمل مع الرؤوس ، ثم الضفيرة هو أفضل وسيلة للذهاب للكم.

Translate this post ترجمة هذه الوظيفة


Related posts: الوظائف ذات الصلة :

  1. JavaScript: Send function as a parameter to another function (callbacks) جافا سكريبت : إرسال الدالة كمعلمة إلى وظيفة أخرى (رد)

  • saya kemarin juga ngerjain web yang grabb data dari website lain... سايا kemarin juga ngerjain الويب يانغ البيانات غراب الضاري راقدا في موقع...

    sayang ga sempat baca artikel ini, jadi nya pake file_get_contents().. sayang الجا sempat باكا مقالات النطاق ، جادي نيا pake file_get_contents ().. di potong2 pake preg_replace(), buat ngambil data yang di butuh kan... دي potong2 pake preg_replace () ، وبوا البيانات ngambil يانغ دي butuh اساسه...


    btw salam kenal... راجع للشغل سلام kenal...
blog comments powered by Disqus بلوق التعليقات مدعوم من ديسقوس]