www.webdeveloper.com
Results 1 to 2 of 2

Thread: Pull data from external site

  1. #1
    Join Date
    Apr 2008
    Posts
    85

    Pull data from external site

    I tried look this up but I couldn't seem to find what I need.

    I want to pull the movie synopsis from Apple.com/trailers pages.

    This seems like it should be fairly easy considering that they are stored in a META tag near the top of each page no matter how it is displayed to the user (some pages use an old layout, some new).

    HTML Code:
    <meta name="Description" content="synopsis_text" />
    If I can get ahold of that line of code I just posted, I know how to split it up with PHP to get just the synopsis text, but I can't seem to figure out how to pull just that meta tag. I'd like to avoid pulling the whole page source if possible.

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,407
    PHP Code:
    <?php
    $dom 
    = new DOMDocument();
    @
    $dom->loadHTMLFile('http://www.apple.com/trailers/universal/thewolfman/');
    $metas $dom->getElementsByTagName('meta');
    foreach(
    $metas as $meta)
    {
       if(
    strtolower($meta->getAttribute('name')) == 'description')
       {
          
    $desc $meta->getAttribute('content');
          break;
       }
    }
    if(!empty(
    $desc))
    {
       echo 
    "<p>".htmlentities($desc)."</p>\n";
    }
    else
    {
       echo 
    "Oops!";
    }
    If your host does not have allow_url_fopen enabled, you'll probably have to use the cURL functions to grab the HTML content, then use DOMDocument->loadHTML() instead of DOMDocument->loadHTMLFile().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles