jibster Posted June 10, 2008 Share Posted June 10, 2008 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. Link to comment https://forums.phpfreaks.com/topic/109574-solved-get-page-title-of-external-site-using-fsockopen/ Share on other sites More sharing options...
discomatt Posted June 10, 2008 Share Posted June 10, 2008 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]; } ?> Link to comment https://forums.phpfreaks.com/topic/109574-solved-get-page-title-of-external-site-using-fsockopen/#findComment-562074 Share on other sites More sharing options...
jibster Posted June 10, 2008 Author Share Posted June 10, 2008 Thank you! That worked great. Had to modify slightly though because file_get_contents() is disabled on the server. Thanks again. Link to comment https://forums.phpfreaks.com/topic/109574-solved-get-page-title-of-external-site-using-fsockopen/#findComment-562239 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.