how to get a snapshot using php for dahua SD49225T-HN

0blar

Getting the hang of it
Joined
May 17, 2017
Messages
281
Reaction score
34
Yes i can see the image file created in "/var/www/html/images" correctly
My php file is located in "/var/www/html/" folder
here the complete code i use:
PHP:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$login = 'admin';
$password = 'password';
$url = 'http://Cam_IP/cgi-bin/snapshot.cgi';
header("Content-type: image/jpg");
header ("Content-type: image/jpeg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$im = imagecreatefromstring($result);
$now = date("d-m-Y-H:i");
$newnew = imagecreatetruecolor(960,540);
imagecopyresized($newnew, $im, 0, 0, 0, 0, 960, 540, 1920, 1080);
$newnewfile = "images/$now-r.jpg";
imagejpeg($newnew, $newnewfile);
imagedestroy($im);
imagedestroy($newnew);
?>
<!DOCTYPE html>
<HTML>
<HEAD>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
<TITLE>Test Cam SnapShot</TITLE>
</HEAD>
<BODY>
<img src="<?php echo "<img src=\"images/$now-r.jpg\">"; ?>" alt=""/>
</BODY>
</HTML>
I have the same problem
 

Go3Team

Pulling my weight
Joined
Mar 13, 2017
Messages
147
Reaction score
110
Location
RVA
Code:
$newnewfile = "images/$now-r.jpg";
should be

Code:
$newnewfile = "/var/www/html/images/$now-r.jpg";
Did you say you were using nginx?

Do you have a file /var/log/nginx/error.log?

Can you give me the output of:

Code:
tail -n 25 /var/log/nginx/error.log
Edit #2:

You also don't need this either:

Code:
header("Content-type: image/jpg");
header ("Content-type: image/jpeg");
With that. you're telling the browser to expect an image, but you're giving it html.
 
Last edited:

0blar

Getting the hang of it
Joined
May 17, 2017
Messages
281
Reaction score
34
I just make it works
here the code:
Code:
<?php
$login = 'admin';
$password = 'password';
$url = 'http://Cam_IP/cgi-bin/snapshot.cgi';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$im = imagecreatefromstring($result);
$now = date("d-m-Y-H:i");
$newnew = imagecreatetruecolor(960,540);
imagecopyresized($newnew, $im, 0, 0, 0, 0, 960, 540, 1920, 1080);
$newnewfile = "images/$now-r.jpg";
imagejpeg($newnew, $newnewfile);
imagedestroy($im);
imagedestroy($newnew);
echo "<img src=\"images/$now-r.jpg\">";
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="3">
<title>Cam SnapShot</title>
</head>
<body>
</body>
</html>
Thanks again

My next target will be to hook a motion sensor to the rpi then get a snapshot and send it via email
 

danbutter

Getting the hang of it
Joined
May 28, 2017
Messages
139
Reaction score
47
Have you guys had the issue of this cam not stopping sending snapshots once you start?
I have tried to config this cam to save a snapshot on an event like line crossing and it seems to work fine sending the snapshots to my ftp server.
Issue comes where I used the URL you are using here in a browser. It never stops sending the snapshots after that. I have to reboot the cam to stop them.
Took me a sec to figure out why my ftp server was being filled up.
 

0blar

Getting the hang of it
Joined
May 17, 2017
Messages
281
Reaction score
34
I'm only using the script with a HFW5231e-z12e for the moment

Do you have
PHP:
<meta http-equiv="refresh" content="5">
somewhere on your code ?
Maybe this is why you get non stop pictures ?
 

danbutter

Getting the hang of it
Joined
May 28, 2017
Messages
139
Reaction score
47
No. I setup my ftp for snapshots which I then chose as what to do after a line crossing event.
That worked fine.
A few weeks later I was messing around and went to the snapshot url via a browser. From that moment on the snapshots didn't stop until I rebooted the camera.
I just happened to have the console for my ftp open under the browser...that was how I noticed this.
 

wozzzzza

Getting the hang of it
Joined
Jan 5, 2015
Messages
385
Reaction score
82
now i got it all working, refreshing how i wanted to on my local computer, PTZ and zoom buttons are working well, but when i upload to my webserver the PTZ buttons stop working but the image still refreshes.
anyone know what this could be?? ive got port 80 forwarded on router to the camera from external and DDNS name setup to my home network.
 

0blar

Getting the hang of it
Joined
May 17, 2017
Messages
281
Reaction score
34
Hi
Can you share your code here ?
 

Go3Team

Pulling my weight
Joined
Mar 13, 2017
Messages
147
Reaction score
110
Location
RVA
now i got it all working, refreshing how i wanted to on my local computer, PTZ and zoom buttons are working well, but when i upload to my webserver the PTZ buttons stop working but the image still refreshes.
anyone know what this could be?? ive got port 80 forwarded on router to the camera from external and DDNS name setup to my home network.
Probably not a good idea to port forward direct to the camera.

I've taken snapshots and the PTZ still works using SPSS, Joystick and in IE. It works fine each time.
 

wozzzzza

Getting the hang of it
Joined
Jan 5, 2015
Messages
385
Reaction score
82
Hi
Can you share your code here ?
when i get it working properly.
Probably not a good idea to port forward direct to the camera.

I've taken snapshots and the PTZ still works using SPSS, Joystick and in IE. It works fine each time.
how to talk to camera from webserver if port isnt open?
 

Go3Team

Pulling my weight
Joined
Mar 13, 2017
Messages
147
Reaction score
110
Location
RVA
when i get it working properly.

how to talk to camera from webserver if port isnt open?
The camera is remote from the webserver? I would use a different port other than 80 and redirect that to the camera. Like say Port 5000 -> Router -> Camera Port 80.

Or use a VPN.
 

wozzzzza

Getting the hang of it
Joined
Jan 5, 2015
Messages
385
Reaction score
82
yeah thats what im doing, a 4 figure high port number then router NAT to port 80 of camera. i may change this as well once i get script working.

problem im getting is this script works on my local test server but when i upload it to the real webserver it gives me a 500 internal server error, but when i remove both of these below functions the script function as well as it can without them. what can you see wrong with these 2 functions?? both need to be removed for script to work

running PHP Version 5.3.29

PHP:
function build_options($url,$user,$pass,$debug,$basic_auth){
  $options = array(
          CURLOPT_URL            => $url,
          CURLOPT_USERPWD        => $user . ":" . $pass,
          CURLOPT_RETURNTRANSFER => true,
  );
  # auth method
  if($basic_auth){
    $auth_header='Basic '.base64_encode("$user:$pass");
    $options[CURLOPT_HTTPHEADER]=[$auth_header];
  }else{
    $options[CURLOPT_HTTPAUTH]=CURLAUTH_DIGEST;
  }
  # debug headers
  if($debug){
    $options[CURLOPT_HEADER]=true;
    $options[CURLOPT_VERBOSE]=true;
  }
  return $options;
}



function curl_call($options){
  $ch = curl_init();
  curl_setopt_array( $ch, $options);

  try {
    $raw_response  = curl_exec( $ch );
    // validate CURL status
    if(curl_errno($ch)){
        throw new Exception(curl_error($ch), 500);
    }
    // validate HTTP status code (user/password credential issues)
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($status_code != 200)
        throw new Exception("Response with Status Code [" . $status_code . "].", 500);
  } catch(Exception $ex) {
      throw new Exception($ex);
  } finally {
      curl_close($ch);
      return $raw_response;
  }
}
 

Go3Team

Pulling my weight
Joined
Mar 13, 2017
Messages
147
Reaction score
110
Location
RVA
What webserver are you running?

If you're running Apache, what is the output of:

Code:
tail -n 25 /var/log/apache2/error.log
 

wozzzzza

Getting the hang of it
Joined
Jan 5, 2015
Messages
385
Reaction score
82
i was looking at error log on the FTP, i was being lazy, it didnt have anything in it. so i went into the cpanel and looked aat the error logs there and came with
PHP Parse error: syntax error, unexpected '{' in /home/public_html/webcam/test/SecureImageDisplayControl.php on line 232

this equates to

} finally {

in the above code. but whats wrong with that??
 

Go3Team

Pulling my weight
Joined
Mar 13, 2017
Messages
147
Reaction score
110
Location
RVA
i was looking at error log on the FTP, i was being lazy, it didnt have anything in it. so i went into the cpanel and looked aat the error logs there and came with
PHP Parse error: syntax error, unexpected '{' in /home/public_html/webcam/test/SecureImageDisplayControl.php on line 232

this equates to

} finally {

in the above code. but whats wrong with that??
Should this be:

Code:
if ($status_code != 200) {
        throw new Exception("Response with Status Code [" . $status_code . "].", 500);
}
 

wozzzzza

Getting the hang of it
Joined
Jan 5, 2015
Messages
385
Reaction score
82
i will have a look, but yeah that script is part of it, i have modified it heavily. basically just using those functions on the tail end for the PTZ commands.
 

Go3Team

Pulling my weight
Joined
Mar 13, 2017
Messages
147
Reaction score
110
Location
RVA
Using the script as it is on that site works fine for me, so I doubt that part is the issue. If you feel up to it, post the full file so I can test it on my end.
 

wozzzzza

Getting the hang of it
Joined
Jan 5, 2015
Messages
385
Reaction score
82
i just tried that script in its original form and gives me the same thing, internal server error 500.
error log says
PHP Parse error: syntax error, unexpected '[' in /home/public_html/webcam/test/original.php on line 10

which is the "$cam_list=[" line. what gives here??
 
Top