get info from remote web page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

get info from remote web page

Post by kingconnections »

Ok - so say that I want to have my script open a url and get text from a remote web page. Is this possible? If so how would I go about doing something like this?

ie http://support.microsoft.com/kb/232039/en-us


and I wanted my script to return

Windows 2000 Upgrade Paths


Thanks in advance
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

So you want to get page title...

I like using regular functions for doing this (and not regex):

Code: Select all

$url = "http://support.microsoft.com/kb/232039/en-us";
$content = file_get_contents($url);

$find_open_tag = "<title>";
$position_start = strpos($content, $find_open_tag);
if($position_start === false)
{
   echo "Page doesn't have a title!";
}
else
{
   // Move the pointer to the end of the opening title tag...
   $position_start = $position_start + strlen($find_open_tag);
   // Find the closing title tag: </title>
   $find_close_tag = "</title>";
   $position_end = strpos($content, $find_close_tag);
   if($position_end === false)
   {
      echo "Page doesn't have a closing title tag!";
   }
   else
   {
      // Read the title...
      $title = substr($content, $position_start, $position_end-$position_start);
   }
}
echo $title;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$content = file_get_contents('http://support.microsoft.com/kb/232039/en-us');

preg_match('#<title>(.*?)</title>#s', $content, $matches);

echo '<pre>';
print_r($matches);
I'd prefer regular expression in this case, or likely http://www.php.net/manual/en/function.p ... .php#46790
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Dude, you guys rock!!!! Thanks for the info.
Post Reply