Subscribe to How-To Geek Mag-subscribe sa Paano-Upang 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: Kunin ang mga nilalaman ng isang web page, RSS feed, o XML file sa isang variable ng string

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. Ikaw ay madalas magkaroon ng pangangailangan na ma-access ang data na tirahan sa ibang server, kung ikaw ay pagsulat ng isang online na aggregator RSS o paggawa ng ad-screen para sa isang paghahanap na mekanismo. PHP makes pulling this data into a string variable an extremely simple process. PHP gumagawa ng paghila ang data na ito sa isang string variable na isang lubhang payak na proseso.

You can go with the really short method: Maaari kang pumunta sa tunay maikling paraan:

$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. Ang tanging problema sa pamamaraan na iyon ay na ang ilang mga nagho-host ng web ay may url na ma-access ang blocked sa mga pamamaraan na file, para sa seguridad dahilan. You may be able to use this workaround method instead: Maaari mong gamitin ito workaround sa halip na paraan:

 function get_url_contents($url){ function 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, $ timeout);
        $ret = curl_exec($crl); $ magpabasa = curl_exec ($ crl);
        curl_close($crl); curl_close ($ crl);
        return $ret; bumalik $ magpabasa;
} ) 

You should now have the contents of the website in a string variable. Ikaw ay dapat na ngayon ang mga nilalaman ng website sa isang variable ng string. Note that this doesn't pull down the supporting files such as javascript or CSS. Tandaan na ito ay hindi pull down ang sumusuporta sa file gaya ng javascript o CSS. You will have to further parse the page and retrieve those seperately if you need the whole thing. Ikaw ay magkakaroon ng karagdagang upang mai-parse ang mga pahina at kunin ang mga hiwalay kung kailangan mo ang buong bagay.

This article was originally written on 09/25/06 Tagged with: Ang artikulong ito ay orihinal na isinulat sa 09/25/06 Tagged with: PHP PHP , , Programming Programming

Daily Email Updates Araw-araw na Updates Email

You can get our how-to articles in your inbox each day for free. Maaari kang makakuha ng aming kung-paano na mga artikulo sa iyong inbox sa bawat araw para sa libre. Just enter your name and email below: Ilagay lamang ang inyong pangalan at email sa ibaba:


Name: Pangalan:
Email: Email:

Comments (4) Comments (4)

  1. Demetrius Demetrius

    Problem with RSS Feed in WordPress. Problema sa mga RSS Feed sa 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. Mayroon akong isang subdomain na naka-install ko ang wordpress para sa isa pang blog site, pero rss ang subdomain site feed points sa aking mga magulang site.
    Can anyone come up with any suggestions? Kahit sino ay maaaring magkaroon ng anumang mga mungkahi?

  2. Davide Davide

    Hi, Hi,

    I've tried your hack but I always get the same result: Ako tried ang iyong hack pero lagi ko makuha ang parehong resulta:

    “Destination host forbidden” "Destination host ipinagbabawal"

    How can I solve this issue? Paano ko malutas ang isyu na ito?

    Thanks. Salamat.

  3. ypi prem ypi prem

    Thanks a lot for your simple function. Thanks a lot para sa iyong mga simpleng function. This really gives a lot of power to user to reuse the internet! Ito ay tunay na nagbibigay ng isang pulutong ng kapangyarihan sa user na muli sa internet!
    Ofcourse the content should be pulled with prior approval! Ofcourse nilalaman ang dapat hinila na may approval bago!

  4. anonymous di-kilala

    i love u! i love u!


Our Friends Ang aming mga Friends
Getting Started Pagsisimula


About How-To Geek Tungkol sa Paano-Upang Geek
What Is That Process? Ano ba ang Proseso Iyon?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
mDNSResponder.exe mDNSResponder.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. All Rights Reserved.