downloading a webpages with php

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
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

downloading a webpages with php

Post 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
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post 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...
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

Post 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);  
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 :)
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

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

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 )
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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).
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

Post 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 ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

Post by elipollak »

oh ok.

Thanx very much for all your help
Eli
Post Reply