Ajax Talk - Asynchronous javascript and xml discussion board
 FAQ   Search    Usergroups      Register   Profile   Check your messages   Log in 
Signup now to enjoy a range of free membership advantages !!
Ajax Talk - Moderators Wanted
fetch <title> from external webpage from script

 
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> PHP
View previous topic :: View next topic  
Author Message
deejay
Forum Assistant
Forum Assistant


Joined: 17 May 2006
Posts: 78
Location: Netherlands

PostPosted: Thu Jul 06, 2006 3:14 pm    Post subject: fetch <title> from external webpage from script Reply with quote

hi check this script,

it fetches the title (from between the title tags) from a given url.

Code:
$url = "http://www.ajaxtalk.com";

$html = implode('', file($url));

// get title
   $fcontents = stristr($html, '<title>');
   $rest = substr($fcontents, 7);
   $extra = stristr($fcontents, '</title>');
   $titlelen = strlen($rest) - strlen($extra);
   $title= trim(substr($rest, 0, $titlelen));
print $title;

_________________
please correct me if i am wrong
seo-expertglobalwarming
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Sun Aug 06, 2006 12:25 pm    Post subject: Reply with quote

For this to work safe_mode has to be turned off, and I don't think that buffering the whole file (that's what implode('', file($url)); does) is a good idea. a combination of fopen and fread is much better.
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
admin
Assistant Forum Supervisor
Assistant Forum Supervisor


Joined: 29 Oct 2003
Posts: 154

PostPosted: Mon Aug 07, 2006 7:11 am    Post subject: found this on php.net Reply with quote

You're right, implode puts the html into one string.

found this on php.net

Code:
<?php
#usage:
$r = new HTTPRequest('http://www.php.net');
echo $r->DownloadToString();

class HTTPRequest
{
   var $_fp;        // HTTP socket
   var $_url;        // full URL
   var $_host;        // HTTP host
   var $_protocol;    // protocol (HTTP/HTTPS)
   var $_uri;        // request URI
   var $_port;        // port
   
   // scan url
   function _scan_url()
   {
       $req = $this->_url;
       
       $pos = strpos($req, '://');
       $this->_protocol = strtolower(substr($req, 0, $pos));
       
       $req = substr($req, $pos+3);
       $pos = strpos($req, '/');
       if($pos === false)
           $pos = strlen($req);
       $host = substr($req, 0, $pos);
       
       if(strpos($host, ':') !== false)
       {
           list($this->_host, $this->_port) = explode(':', $host);
       }
       else
       {
           $this->_host = $host;
           $this->_port = ($this->_protocol == 'https') ? 443 : 80;
       }
       
       $this->_uri = substr($req, $pos);
       if($this->_uri == '')
           $this->_uri = '/';
   }
   
   // constructor
   function HTTPRequest($url)
   {
       $this->_url = $url;
       $this->_scan_url();
   }
   
   // download URL to string
   function DownloadToString()
   {
       $crlf = "\r\n";
       
       // generate request
       $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf
           .    'Host: ' . $this->_host . $crlf
           .    $crlf;
       
       // fetch
       $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
       fwrite($this->_fp, $req);
       while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
           $response .= fread($this->_fp, 1024);
       fclose($this->_fp);
       
       // split header and body
       $pos = strpos($response, $crlf . $crlf);
       if($pos === false)
           return($response);
       $header = substr($response, 0, $pos);
       $body = substr($response, $pos + 2 * strlen($crlf));
       
       // parse headers
       $headers = array();
       $lines = explode($crlf, $header);
       foreach($lines as $line)
           if(($pos = strpos($line, ':')) !== false)
               $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
       
       // redirection?
       if(isset($headers['location']))
       {
           $http = new HTTPRequest($headers['location']);
           return($http->DownloadToString($http));
       }
       else
       {
           return($body);
       }
   }
}
?>
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Mon Aug 07, 2006 10:55 am    Post subject: Reply with quote

Coming from the Java world I'm always a bit uncomfortable seeing people buffer lots and lots of stuff. I must admit the Input-/Output-Stream Paradigm in Java takes some time to get used to, but its actually the nicest thing one can have ^^
That's why I was suggesting fread instead of file because it allows you to read small chunks of data at a time.
Consider this: you allow users to enter a URL and your code will then grab the title, nice idea, but what happens if the user enters the URL of some 100MB+ file? Your application will try to buffer it all in the memory and, PHP having a memory limit, will eventually crash because you used up all the memory.
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
deejay
Forum Assistant
Forum Assistant


Joined: 17 May 2006
Posts: 78
Location: Netherlands

PostPosted: Tue Aug 08, 2006 3:10 pm    Post subject: you mean something like this Reply with quote

indeed, the fgets or fread function allows us to buffer Smile

$fp = fopen("http://www.ajaxtalk.com", "r");

if($fp){
while(!feof($fp)) {
$line .= htmlentities(fgets($fp, 1024));
}
}
fclose($fp);
var_dump($line);
_________________
please correct me if i am wrong
seo-expertglobalwarming
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
deejay
Forum Assistant
Forum Assistant


Joined: 17 May 2006
Posts: 78
Location: Netherlands

PostPosted: Tue Aug 08, 2006 3:12 pm    Post subject: same same Reply with quote

Like these two are the same;

if(($fp = fopen($filename, r)) === false) {
$file = false;
} else {
$file = fread($fp, filesize($filename));
}
fclose($fp);

or the file_get_contents() solution;

This function will replace the entire previous loop with
$file = file_get_contents($filename);

source: zend study guide
_________________
please correct me if i am wrong
seo-expertglobalwarming
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Tue Aug 08, 2006 6:53 pm    Post subject: Reply with quote

Once again you are buffering the stuff...
You append each string you read to $line. By making it streamable I mean something like:
Code:
$fp = fopen("http://www.ajaxtalk.com", "r");

if($fp){
  while(!feof($fp)) {
    $line = fgets($fp, 1024);
    // Do something with the line.
    // Extract the data for example
  }
}
fclose($fp);

This way you have a fast application that does not require much memory because we always work on single lines instead of the whole file.
By the way, it is much more effective to use strstr or other functions on many smaller strings than one large one ^^
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
deejay
Forum Assistant
Forum Assistant


Joined: 17 May 2006
Posts: 78
Location: Netherlands

PostPosted: Thu Aug 10, 2006 10:06 am    Post subject: Reply with quote

that's the same as:


Code:
$fp = fopen("http://www.ajaxtalk.com", "r");

if($fp){
while(!feof($fp)) {
$line .= htmlentities(fgets($fp, 1024));
}
}
fclose($fp);
var_dump($line);


which i mentioned earlier Razz Cool
_________________
please correct me if i am wrong
seo-expertglobalwarming
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
php_penguin
New one
New one


Joined: 25 Jun 2007
Posts: 4
Location: Wales, UK

PostPosted: Tue Jun 26, 2007 10:14 pm    Post subject: Reply with quote

wow, real helpful stuff here.

at any rate, you could always parse the page as an XML string, and just go to head->title.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> PHP All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 Advertisements
 XML & Copyright Notice 
    


Powered by phpBB © 2001, 2005 phpBB Group

What is according to Wikipedia: Ajax Web2.0 XML E4X


Link to our site:

  


del.icio.us digg spurl wists simpy newsvine blinklist furl blogmarks yahoo! myweb smarking ma.gnolia segnalo reddit fark technorati cosmos


Normal Bookmark


Partner Button Links:
Codecrunch.com: Webmaster Tutorials    One Ajax


Partner Text Links:
quomon | web20log | ajaxmatters | tableless | 456bereastreet | music charts | gift ideas | competitions | free xbox 360 | link trade ?


Credits:
it consultancy & project management lead generation zoekmachine optimalisatie steal these buttons seo expert