Axis camera and PHP

Axis camera and PHP

am 12.08.2007 19:26:31 von Stefano

I have to send a restart command to an IP camera from a PHP script. The
command is as follow:

http://192.168.1.5/axis-cgi/admin/restart.cgi

where 192.168.1.5 is the address of the camera.

In PHP, my idea was to open a socket connection and send the command, with
the following code:

$host="192.168.1.5" ;
$target="/axis-cgi/admin/restart.cgi" ;
$port=80 ;
$timeout=60;
$br="\r\n" ;
$usarname="root";
$password="password_root";
$sk=fsockopen($host,$port,$errnum,$errstr,$timeout) ;
if(!is_resource($sk)){
exit("Connection failed: ".$errnum." ".$errstr) ;
}
else{
$headers = "GET ".$target." HTTP/1.1".$br ;
$headers.="Accept: */*".$br ;
$headers.="Accept-Language: it".$br ;
$headers.="Host: ".$host.$br ;
$headers.="Authorization: Basic root:password_root".$br.$br;
fputs($sk,$headers) ;

When printing $sk I see the error message

401 Unauthorized
You client does not have permission to get URL /axis-cgi/admin/restart.cgi
from this server

Seems there is something wrong with authentication.

Any idea ?
Is there somebody who uses PHP with Axis camera ? How do you send API
commands to the camera ?


Thank you for your help.

Stefano

Re: Axis camera and PHP

am 12.08.2007 20:29:20 von Ulf Kadner

Stefano wrote:
> I have to send a restart command to an IP camera from a PHP script. The
> command is as follow:
>
> http://192.168.1.5/axis-cgi/admin/restart.cgi
>
> $headers.="Authorization: Basic root:password_root".$br.$br;

Dont know if this is the right header.
I use allways PHPs CURL extension. It works fine.

e.g.: curl_setopt($curl, CURLOPT_HTTPAUTH, 'username:password');
http://php.net/curl

It requires also an min. PHP version 5.*
PHP 4.4.2+ supports it to but official documentation says "avialable for
version 5 and above". May it helps you.

So long, Ulf

--
_,
_(_p> Ulf [Kado] Kadner
\<_)
^^

Re: Axis camera and PHP

am 12.08.2007 23:50:27 von Stefano

Ulf,
thank you a lot for your answer. I've never used Curl before and I am trying
to do right now what you suggested.

So, after curl_init(), using the function :

curl_setopt($curl, CURLOPT_HTTPAUTH, 'username:password');

I get the same error message as before: 401 Unauthorized.

But looking at curl_setopt() specifications, the option CURLOPT_HTTPAUTH can
be set to values CURLAUTH_BASIC, CURLAUTH_DIGEST and so on depending by the
authentication methods to use.
So I should write for example:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

Seems that username:password is not accepted as CURLOPT_HTTPAUTH set value.
In that case where should I specify login and password ?

Thank you again.

Stefano


"Ulf Kadner" ha scritto nel messaggio
news:f9nji0$fdc$02$1@news.t-online.com...
> Stefano wrote:
>> I have to send a restart command to an IP camera from a PHP script. The
>> command is as follow:
>>
>> http://192.168.1.5/axis-cgi/admin/restart.cgi
>>
>> $headers.="Authorization: Basic root:password_root".$br.$br;
>
> Dont know if this is the right header.
> I use allways PHPs CURL extension. It works fine.
>
> e.g.: curl_setopt($curl, CURLOPT_HTTPAUTH, 'username:password');
> http://php.net/curl
>
> It requires also an min. PHP version 5.*
> PHP 4.4.2+ supports it to but official documentation says "avialable for
> version 5 and above". May it helps you.
>
> So long, Ulf
>
> --
> _,
> _(_p> Ulf [Kado] Kadner
> \<_)
> ^^

Re: Axis camera and PHP

am 13.08.2007 00:25:22 von Ulf Kadner

Stefano wrote:

> So, after curl_init(), using the function :
>
> curl_setopt($curl, CURLOPT_HTTPAUTH, 'username:password');
>
> I get the same error message as before: 401 Unauthorized.

Right. It was my failure. Better to read my posting before send.
Yesterday i used it like my example but with PROXY* Paramater. worry? :-)

> curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
>
> Seems that username:password is not accepted as CURLOPT_HTTPAUTH set value.
> In that case where should I specify login and password ?

The fast and bad way is URL-injection like this:
http://username:password@example.com/...

The other and better way is to change youre code at line:
$headers.="Authorization: Basic root:password_root".$br.$br;

to:
$headers .= 'Authorization: Basic '
. base64_encode('root:password_root') . $br . $br);

The RFC says username:password must be Base64 encoded.

By the way: A good PHP-Library for doing something more with
http/ftp/... (by a easy way) is Snoopy.
http://sourceforge.net hosts it.

--
_,
_(_p> Ulf [Kado] Kadner
\<_)
^^

Re: Axis camera and PHP

am 13.08.2007 08:42:12 von Stefano

> The other and better way is to change youre code at line:
> $headers.="Authorization: Basic root:password_root".$br.$br;
>
> to:
> $headers .= 'Authorization: Basic '
> . base64_encode('root:password_root') . $br . $br);
>

YYYEEEESSSSS !!!!
It works now !

To be honest, I tryed the function base64_encode() before but colon was
outside base64_encode.

Thank you very much Ulf. I was working around that problem since last week.

Regards.
Stefano