image grabber

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
evilthawts
Forum Newbie
Posts: 2
Joined: Fri Jun 08, 2007 3:50 pm

image grabber

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah cURL would definitely be much easier here.

Sidenote: Is "\r\n" OK to separate headers?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
evilthawts
Forum Newbie
Posts: 2
Joined: Fri Jun 08, 2007 3:50 pm

helP?

Post by evilthawts »

[Message deleted] - sorry I didn't see feyd's response.
Post Reply