Subscribe to How-To Geek Підписатись на How-To Geek

Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

PHP: Get the contents of a web page, RSS feed, or XML file into a string variable PHP: отримати вміст веб-сторінки, RSS-канал, або XML-файлу у послідовну змінну

You will often have the need to access data that resides on another server, whether you are writing an online RSS aggregator or doing screen scraping for a searching mechanism. Ви будете часто необхідний доступ до даними, який знаходиться на іншому сервері, будь ви пишете онлайн агрегатор RSS або робити чищення екрану для пошуку механізму. PHP makes pulling this data into a string variable an extremely simple process. PHP робить потягнувши ці дані в рядок мінлива дуже простий процес.

You can go with the really short method: Ви можете перейти зі справді короткий спосіб:

$url = “http://www.howtogeek.com”; $ URL = "http://www.howtogeek.com";

$str = file_get_contents($url); $ STR = file_get_contents ($ URL);

The only problem with that method is that some web hosts have url access blocked in the file methods, for security reasons. Єдина проблема до цього методу є те, що деякі веб-хостингу мають доступ до заблокованих URL у файлі методами, з міркувань безпеки. You may be able to use this workaround method instead: Ви можете використовувати цей метод замість обходу:

 function get_url_contents($url){ Функція get_url_contents ($ URL) ( 
         $crl = curl_init(); $ CRL = curl_init (); 
         $timeout = 5; $ Timeout = 5; 
         curl_setopt ($crl, CURLOPT_URL,$url); curl_setopt ($ CRL, CURLOPT_URL, $ URL); 
         curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ CRL, CURLOPT_RETURNTRANSFER, 1); 
         curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt ($ CRL, CURLOPT_CONNECTTIMEOUT, $ таймаут); 
         $ret = curl_exec($crl); $ Ret = curl_exec ($ CRL); 
         curl_close($crl); curl_close ($ CRL); 
         return $ret; повернути $ RET; 
 } ) 

You should now have the contents of the website in a string variable. Тепер ви повинні мати зміст веб-сайту в рядку змінних. Note that this doesn't pull down the supporting files such as javascript or CSS. Зауважимо, що це не знести допоміжні файли, такі як JavaScript і CSS. You will have to further parse the page and retrieve those seperately if you need the whole thing. Вам доведеться подальшого розбору сторінки і отримати їх окремо, якщо вам потрібна цілком.

This article was originally written on 09/25/06 Tagged with: Ця стаття була спочатку написана на 09/25/06 тегами: PHP PHP , , Programming Програмування

Daily Email Updates Email Щоденні оновлення

You can get our how-to articles in your inbox each day for free. Ви можете отримати наші довідкові статті у Вашу поштову скриньку щодня безкоштовно. Just enter your name and email below: Просто введіть ваші ім'я та адресу електронної пошти нижче:


Name: Ім'я:
Email: Електронна пошта:

Comments (4) Коментарі (4)

  1. Demetrius Димитрій

    Problem with RSS Feed in WordPress. Проблема з RSS-канал в WordPress.
    I have a subdomain that I installed wordpress for another blog site, but the subdomain site's rss feed points to my parent site. У мене є субдомен, що я встановив ще на WordPress блог на сайті, а RSS Feed субдомени сайту вказує на мій батько сайті.
    Can anyone come up with any suggestions? Чи можна придумати будь-які пропозиції?

  2. Davide Davide

    Hi, Привіт,

    I've tried your hack but I always get the same result: Я намагався зламати ваш, але я завжди отримаєте той самий результат:

    “Destination host forbidden” "Напрямок приймають заборонені"

    How can I solve this issue? Як можна вирішити цю проблему?

    Thanks. Спасибо.

  3. ypi prem YPI Prem

    Thanks a lot for your simple function. Велике спасибі за вашу просту функцію. This really gives a lot of power to user to reuse the internet! Це дійсно дає багато енергії, щоб користувач для повторного використання в Інтернет!
    Ofcourse the content should be pulled with prior approval! Звичайно зміст треба тягнути за попередньою згодою!

  4. anonymous анонімно

    i love u! I Love U!


Leave a Comment Залишити коментар




Leave your Залиш свій friendly дружній comment here. коментарі.

If you have a computer help question, Якщо у вас є комп'ютер допоможе питання, click here to leave it on the forums Натисніть тут, щоб залишити його на форумах instead. замість.

Note: Your comment may not show up immediately on the site. Примітка: Ваш коментар може не з'явитися відразу на місці.

Our Friends Наші друзі
Getting Started Приступаючи до роботи


About How-To Geek Про How-To Geek
What Is That Process? Що це за процес?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe Ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
wmpnscfg.exe wmpnscfg.exe
rundll32.exe rundll32.exe
wfcrun32.exe wfcrun32.exe
Ipoint.exe Ipoint.exe
Itype.exe Itype.exe
Wfica32.exe Wfica32.exe
Mobsync.exe Mobsync.exe
conhost.exe conhost.exe
Dpupdchk.exe Dpupdchk.exe Adobe_Updater.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. Copyright © 2006-2009 HowToGeek.com. All Rights Reserved. Всі права захищені.