Axis camera and PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

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

where 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="" ;
$target="/axis-cgi/admin/restart.cgi" ;
$port=80 ;
$br="\r\n" ;
$sk=fsockopen($host,$port,$errnum,$errstr,$timeout) ;
exit("Connection failed: ".$errnum." ".$errstr) ;
$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.


Re: Axis camera and PHP

Stefano wrote:
Quoted text here. Click to load it

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');

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

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:


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.


Quoted text here. Click to load it

Re: Axis camera and PHP

Stefano wrote:

Quoted text here. Click to load it

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

Quoted text here. Click to load it

The fast and bad way is URL-injection like this:

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

$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. hosts it.

  _(_p>   Ulf [Kado] Kadner

Re: Axis camera and PHP

Quoted text here. Click to load it

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.


Site Timeline