Page 1 of 1

downloading a webpages with php

Posted: Mon Jan 20, 2003 9:34 am
by elipollak
I'm messing around with trying to write a simple ICQ client
starting with a contact list

I found this code somewhere that should tell me whether I am online or not, but this doesn't work.

I imagine if i downloaded the webste of an image icon that tells if i am online or not, I can tell by the length of the image in bytes whether I am online or not. I assume that this is what this is trying to do.

I dun really want spend time socket programming for this. Is this possible any other way ?

Code: Select all

<?php
<? 
$uin = "*********"; 

function ICQ_Status($uin) { 
 $arr = @file("http://wwp.icq.com/scripts/online.dll?icq=$uin&img=1"); 
 $len = strlen($arr[1]); 
 if ($len == "") return "ICQ: <span style="color:#707070">Local Server</span>"; 
 if ($len == 96) return "ICQ: <span style="color:#009000">Online</span>"; 
 if ($len == 83) return "ICQ: <span style="color:#b00000">Offline</span>"; 
 if ($len == 1) return "ICQ: <span style="color:#000080">Disabled</span>"; 
} 
echo ICQ_Status($uin); 

?>
?>
Thanx for any help
Eli

Posted: Mon Jan 20, 2003 12:23 pm
by puckeye
In your code you have:

Code: Select all

$arr = @file("http://wwp.icq.com/scripts/online.dll?icq=$uin&img=1");
Is a semi colon ";" supposed to be between amp and ing as in &ing=1 ???

It's the first time I see something like this...

Posted: Mon Jan 20, 2003 7:16 pm
by elipollak
... when i write "&" in php tags in a message here it comes out as "&"
ignore the amp and the semi colon in that

Code: Select all

<?php
$uin = "*********";  

function ICQ_Status($uin) {  
 $arr = @file("http://wwp.icq.com/scripts/online.dll?icq=$uin&img=1");  
 $len = strlen($arr[1]);  
 if ($len == "") return "ICQ: <span style="color:#707070">Local Server</span>";  
 if ($len == 96) return "ICQ: <span style="color:#009000">Online</span>";  
 if ($len == 83) return "ICQ: <span style="color:#b00000">Offline</span>";  
 if ($len == 1) return "ICQ: <span style="color:#000080">Disabled</span>";  
}  
echo ICQ_Status($uin);  
?>

Posted: Tue Jan 21, 2003 1:54 am
by volka
when I'm online the request
http://wwp.icq.com/scripts/online.dll?icq=151017995
redirects me to Location: /lib/image/0,,4362,00.gif.
When I'm offline to Location: /lib/image/0,,4345,00.gif
if this is valid for all users (haven't tested that) and you have access to the socket-extension the script may check that redirect.
simple and without error-handling:

Code: Select all

<?php

function icqIsOnline($uin)
{
	$sd = socket_create (AF_INET, SOCK_STREAM, getprotobyname("TCP"));
	if( socket_connect( $sd, 'wwp.icq.com', 80) )
	{
		$headers = "GET /scripts/online.dll?icq=$uin HTTP/1.0\n\n";
		socket_write($sd, $headers, strlen($headers));
		$headers = socket_read($sd, 1024);
		socket_close($sd);
		preg_match('!Location:.*,,(\d+),.*!', $headers, $matches);
		if ($matches[1] == '4362')
			return TRUE;
	}

	return FALSE;
}

echo (icqIsOnline('151017995')) ? 'online' : 'offline';

?>
since the server replies with http 1.1 on a 1.0 request I don't care about giving it a completely valid request :)

yea .. that's exactly what I wanted ... but I get an error

Posted: Tue Jan 21, 2003 6:11 am
by elipollak
yea .. that's exactly what I wanted ... but I get an error :

Fatal error: Call to undefined function: socket_create() in c:\apache\htdocs\icq.php on line 8

I have php 4.1.1 ... and at php.net, it says :
socket_create : (PHP 4 >= 4.1.0)
so I have no idea why I'm getting it.

any ideas ?

And by the way, thanx very much for the help
Eli

Posted: Tue Jan 21, 2003 7:39 am
by volka
and you have access to the socket-extension
;)
you have to enable php_sockets.dll either by appending it to your extensions in php.ini or via dl() if possible

( 'c:\apache' -> assuming win32 )

Posted: Tue Jan 21, 2003 6:28 pm
by elipollak
ahhh .. dll must be a library (I'm relatively new to php)
yea .. I put this in and it works.

if (!dl('php_sockets.dll ')) {
exit;
}

but when I load it up from my windows OS computer to a linux server, i get :

Warning: Unable to load dynamic library '/usr/lib/php4/20010901/php_sockets.dll ' - /usr/lib/php4/20010901/php_sockets.dll : cannot open shared object file: No such file or directory in /tmp_amd/elfman/export/elfman/1/elip/public_html/icq.php on line 6

Any ideas ?

THANX FOR YOUR HELP
Eli

Posted: Tue Jan 21, 2003 9:08 pm
by volka
the unix/linux version of php does not use .dlls. They are called .so on thos systems. Would be best if you add the extension to php.ini (if possible).

Posted: Wed Jan 22, 2003 7:37 am
by elipollak
"Would be best if you add the extension to php.ini (if possible)"

umm ... how do u do that
The unix server belongs to uni not me. I don't think I have access.

And I don't actually know what u mean by "add the extension to php.ini "

Eli

Posted: Wed Jan 22, 2003 8:57 am
by volka
http://www.php.net/manual/en/function.dl.php
the example is for the socket-extension :)

But maybe it's not available. Then the script is useless.

Posted: Wed Jan 22, 2003 9:34 am
by elipollak
so if i do this

if (!dl('sockets.so')) {
exit;
}

and it doesn't work then I can't do any socket programming in php on that unix server ?

Posted: Wed Jan 22, 2003 10:07 am
by volka

Code: Select all

if (!extension_loaded('sockets')) {
    if (!dl('sockets.so')) {
        exit;
    }
}
but yes, you can't use the socket-functions without that

Posted: Wed Jan 22, 2003 6:08 pm
by elipollak
oh ok.

Thanx very much for all your help
Eli