create file (image) based on header



Hi guys, I'd like to use Alexia thumbnail service. It's an interesting
service, you pass an url to its cgi and you get a thumbnail of that
url. Of course they don't have the screenshots of all the websites so I
have to check if the image returned is fine or not. This is not that
problematic, I can read the headers and understand if it is a jpg or a
gif (the error one). The problem that I have is that sometimes although
they have the screenshot in the database they return the error one. For
different reasons I thought to backup once on a while the thumbs that I
need.

So the procedure should be something like this:

- query the cgi
- check if it is a correct image
- if it is, backup it

I'm having problem on backup it basically. To read and check the header
I do:

[code]
$a_url = parse_url($url);
if (!isset($a_url['port'])) $a_url['port'] = 80;
$errno = 0;
$errstr = '';
$timeout = 30;
if(isset($a_url['host']) &&
$a_url['host']!=gethostbyname($a_url['host'])){
$fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr,
$timeout);
if (!$fid) return false;
$page = isset($a_url['path']) ?$a_url['path']:'';
$page .= isset($a_url['query'])?'?'.$a_url['query']:'';
fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host:
'.$a_url['host']."\r\n\r\n");
$head = fread($fid, 4096);
if(!preg_match('[Content-Type: image/gif]', $head) &&
preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head)){
$foundThumb = true;
// here I should to create the file using the $fid resource
}
fclose($fid);
}
[/code]

Since sometimes the service fails I have to use the same stream I check
when creating the new file and I tried in many way to read the bites
and "fput" them in the new file but without success. How could I reuse
$fid to create the new file? I don't know so much fsockopen and I
didn't understand in which way I'm handling the file, it's quite weird
to me to open a socket to a server and then get the headers setting the
header in the socket. If it is like that how to make me pass the stream
and create the image without doing another call to the cgi with the
risk of having the error one?

Any help would be welcome and let me know if something is not clear.
Thanks, chr

.



Relevant Pages

  • Re: Simple script execution problems (newbie)
    ... my $query= new CGI; ... Internal Server Error 500 and the error log reports "Premature end of script ... Permission denied", "Premature end of script headers: ...
    (comp.lang.perl.misc)
  • Re: Misbehaved CGI application headers
    ... The CGI developer said it was by design. ... response -- so the error message is gone. ... Individual request headers are not terminated by CRLF (according to ... > My application developers cannot fix their Perl code because IIS 6 does ...
    (microsoft.public.inetserver.iis)
  • Re: Misbehaved CGI application headers
    ... CGI applications can be run locally to generate the output, ... Individual request headers are not terminated by CRLF (according to HTTP ... My application developers cannot fix their Perl code because IIS 6 does not ... not returning a complete set of HTTP headers. ...
    (microsoft.public.inetserver.iis)
  • Re: mysterious line in my script - what does it all mean?
    ... maxwells> use CGI; ... maxwells> $query = new CGI; ... not mean the data will not contain additional headers. ...
    (comp.lang.perl.misc)
  • Re: POST from a CGI
    ... > I'm receiving POST data to a CGI, which I'd like to forward to another ... > CGI using urllib2. ... > I've checked the headers and the body data and they seem normal and I ... > we're going to rebuild from the environment variables ...
    (comp.lang.python)