Page 1 of 1

image grabber

Posted: Fri Jun 08, 2007 4:03 pm
by evilthawts
Ok, I need to be able to grab an image from a protected server (i have access) and save it to directory on my server.

I can get it to connect and it will show the data for the image but when i try to write it to a file it saves the header return as well.

I used to code alot but have been out of the game for awhile and I'm forgetting alot.
heres an example on my code:

Code: Select all

<?php
error_reporting(E_ALL);
$credentials = base64_encode("user:pass");
$service_port = getservbyname('www', 'tcp');
$address = gethostbyname('www.bla.com');
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
    echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
}

$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
}

$in = "GET /images/image.jpg HTTP/1.1\r\n";
$in .= "Authorization: Basic $credentials\r\n";
$in .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
$in .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4\r\n";
$in .= "Host: http://www.bla.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';

socket_write($socket, $in, strlen($in));

while ($out = socket_read($socket, 2048)) {
   $total .= $out;
}
socket_close($socket);


echo $total;


$fn = "pic.jpg";
$fh = fopen($fn, 'w') or die("can't open file");
fwrite($fh, $total);
fclose($fh);
?>
any help would be appreciate it

Posted: Sat Jun 09, 2007 4:01 pm
by feyd
You will have to process out the header. Better yet, if you can, use cURL, Snoopy or some other browser emulation library.

The header and content is separated the first two consecutive carriage returns.

If you want an example of this, check out the UrlInput class provided in my newest SHA256 library.

Posted: Sat Jun 09, 2007 6:57 pm
by Ollie Saunders
Yeah cURL would definitely be much easier here.

Sidenote: Is "\r\n" OK to separate headers?

Posted: Sat Jun 09, 2007 8:24 pm
by feyd
ole wrote:Yeah cURL would definitely be much easier here.

Sidenote: Is "\r\n" OK to separate headers?
It's standard, but some use \n\n...

helP?

Posted: Sun Jun 10, 2007 9:59 pm
by evilthawts
[Message deleted] - sorry I didn't see feyd's response.