Jump to content

[SOLVED] Get page title of external site using fsockopen()


jibster

Recommended Posts

Hi, I'm trying to get the page title for a given URL by connecting to the site directly. So if I supply the URL

 

http://www.google.com/search?source=ig&hl=en&rlz=&q=i+love+php

 

it should find that the title is "i love php - Google Search".

 

I'm just trying to do this with a static URL at the moment then I can change it to accept ANY web address later.

 

 

$con = fsockopen('www.google.com', 80, $errno, $errstr, 30);
if (!$con) { echo "Couldn't connect"; exit; }


$header = "GET /search?num=20&hl=en&safe=off&q=i+love+php HTTP/1.0\r\n\r\n";
$header .= "Host: www.google.com\r\n";

fwrite($con, $header);

// Read and store the server response
$response = '';

while(!feof($con)) {
	$response .= fgets($con,128); 

	//title start
	$title_start = stripos($response, '<title>');

 

And that's basically where I'm up to. $title_start does hold the position (460) where the <title> tag begins, but I can't seem to find a string function that will get me whatever's after that tag and before </title> tag.

 

Hope that made sense. Any pointers appreciated. Cheers people.

Here's what I'd use

 

<?php

$site = 'http://www.google.ca/search?q=omg+lol';

echo getTitle( $site );

function getTitle ( $url ) {

if (  ( $contents = file_get_contents( $url ) ) === FALSE  )
	return FALSE;

# Use %<title>.+?</title>% for a 'looser' title search.. can be much less efficient though
if (  preg_match( '/<title>([^<]++)/', $contents, $matches ) == FALSE )
	return FALSE;

return $matches[1];

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.