SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    file_get_contents()

    How do I create a variable that contains the html of a site using file_get_contents()?

    Based on http://php.net/manual/en/function.file-get-contents.php, I've tried many variations of this short script with no joy.
    PHP Code:
    <?php
    $homepage 
    file_get_contents('http://www.example.com');
    $txt fopen($homepage,'r');
    ?>
    How do I avoid the warning and create a variable that I can fopen using file_get_contents()?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    $txt = file_get_contents('http://www.example.com');
    That is all. You do not use fopen or other f* functions with this.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks logic_earth.

    So, when I run this script:
    PHP Code:
    <?php
    $file 
    file_get_contents('http://comlinc.com/file_get_contents3.php');
    $var =  substr($file,strripos($file,"ViewPropertyOrOwners"));
    var_dump($var);
    ?>
    It targets the string in the javascript. I want to target the "ViewPropertyOrOwners" string in the html.

    Why doesn't my script target the string in the html first?

    Here's the link for the script: http://comlinc.com/test_121209.php

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP has no concept of HTML or Javascript. If that is what you need, use the DOM functions. http://us2.php.net/DOM
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'm following you. Thanks.

    I found this tutorial at http://www.php4every1.com/tutorials/...ment-tutorial/

    It produces this little script:
    PHP Code:
    <?php
    $dom 
    = new DOMDocument();
    $dom->load('http://www.w3schools.com/XML/simple.xml');
    $food $dom->getElementsByTagName('food');
    var_dump($food);
    ?>
    It produces this result: object(DOMNodeList)#2 (0) { }

    How do I see what's in it? var_dump will display the contents of a "normal" array. How do I display the contents of a "object" array. I'm new to objects and don't know which keywords to google.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Got it!
    PHP Code:
    <?php
    $DOM 
    = new DOMDocument();
    $DOM->loadHTMLFile('http://www.w3.org');

    $items $DOM->getElementsByTagName('a');

    for (
    $i 0$i $items->length$i++) {
        echo 
    $items->item($i)->nodeValue '<br/>';
    }

    ?>
    Thanks again for your help logic_earth, but then I'm sure you hear that all the time.

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    $txt = file_get_contents('http://www.example.com');
    That is all. You do not use fopen or other f* functions with this.
    Note Bene: fopen wrappers must be enabled for this to work. Some server admins may disable this for security reasons, in which event cURL should be used. So if anyone tries this and it doesn't work, that's likely why.

  8. #8
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Also, even if URL fopen is enabled, file_get_contents() can fail if the remote server checks the user agent of the request. Sometimes the default one is blocked. Twitter RSS feeds, for example, don't like the default User Agent of my web server. You can set the user agent in file_get_contents() but it's just as easy to use cURL in these cases.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •