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: Get nội dung của một trang web, tin RSS, hoặc tệp tin XML vào một biến 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. Bạn sẽ thường có nhu cầu truy cập dữ liệu mà nằm trên máy chủ khác, cho dù bạn đang viết một aggregator RSS trực tuyến hoặc làm màn hình cào cho một cơ chế tìm kiếm. PHP makes pulling this data into a string variable an extremely simple process. PHP làm kéo dữ liệu này vào một chuỗi biến một quá trình rất đơn giản.

You can go with the really short method: Bạn có thể đi với các phương pháp thực sự ngắn hạn:

$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. Vấn đề duy nhất với phương pháp đó là một số máy chủ web có url bị chặn truy cập vào các phương pháp tập tin, vì lý do an ninh. You may be able to use this workaround method instead: Bạn có thể sử dụng phương pháp này workaround thay vì:

 function get_url_contents($url){ chức năng 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); $ ret = curl_exec ($ CRL);
        curl_close($crl); curl_close ($ CRL);
        return $ret; return $ ret;
} ) 

You should now have the contents of the website in a string variable. Bây giờ bạn có các nội dung của các trang web trong một biến chuỗi. Note that this doesn't pull down the supporting files such as javascript or CSS. Lưu ý rằng điều này không kéo xuống các tập tin hỗ trợ như javascript hay CSS. You will have to further parse the page and retrieve those seperately if you need the whole thing. Bạn sẽ phải tiếp tục phân tích các trang và lấy lại những cách riêng biệt nếu bạn cần toàn bộ điều.

This article was originally written on 09/25/06 Tagged with: Bài viết này ban đầu được viết ngày 09/25/06 Tagged với: PHP PHP , , Programming Lập trình

Daily Email Updates Email cập nhật hàng ngày

You can get our how-to articles in your inbox each day for free. Bạn có thể làm thế nào chúng tôi vào các bài viết trong hộp thư của bạn mỗi ngày miễn phí. Just enter your name and email below: Chỉ cần nhập tên và email của bạn dưới đây:


Name: Tên:
Email: Email:

Comments (4) Comment (4)

  1. Demetrius Demetrius

    Problem with RSS Feed in WordPress. Vấn đề với RSS trong 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. Tôi có một tên miền phụ mà tôi đã cài đặt wordpress cho một trang blog, nhưng rss trang web tên miền phụ của thức ăn điểm đến trang web của mẹ tôi.
    Can anyone come up with any suggestions? Bất cứ ai có thể đến với bất kỳ lời đề nghị?

  2. Davide Davide

    Hi, Chào,

    I've tried your hack but I always get the same result: Tôi đã thử hack của bạn nhưng tôi luôn nhận được cùng một kết quả:

    “Destination host forbidden” "Điểm đến máy chủ lưu trữ cấm"

    How can I solve this issue? Làm thế nào tôi có thể giải quyết vấn đề này?

    Thanks. Cảm ơn.

  3. ypi prem tuÁi Prem

    Thanks a lot for your simple function. Cảm ơn rất nhiều cho chức năng đơn giản của bạn. This really gives a lot of power to user to reuse the internet! Điều này thực sự mang đến cho rất nhiều sức mạnh để người sử dụng để tái sử dụng internet!
    Ofcourse the content should be pulled with prior approval! Ofcourse nội dung cần được kéo với chấp thuận trước!

  4. anonymous vô danh

    i love u! i love u!


Leave a Comment Để lại một Thảo luận




Leave your Để lại của bạn friendly thân thiện comment here. bình luận ở đây.

If you have a computer help question, Nếu bạn có một câu hỏi giúp máy tính, click here to leave it on the forums bấm vào đây để lại nó trên các diễn đàn instead. thay vì.

Note: Your comment may not show up immediately on the site. Lưu ý: Ý kiến của bạn có thể không hiển thị ngay lập tức trên trang web.

Our Friends Bạn bè của chúng tôi
Getting Started Bắt đầu


About How-To Geek Giới How-To Geek
What Is That Process? Quá trình đó là gì?
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. Bản quyền © 2006-2009 HowToGeek.com. All Rights Reserved. Tất cả các quyền.