Jump to content

How to send simple GET request to IP Camera


bcamp1973

Recommended Posts

I'm trying to control a few functions of my home IP camera via PHP. Commands for the camera are in the form of http://cameraIPaddress/cameraCGI?direction=panLeft ...I don't have to worry about capturing any kind of response etc. I simply want to send the command. I'd also like to authenticate the request, but no idea how to handle that from within the PHP? I've tried fsockopen(), but maybe i'm not doing it right...i worked from this example...it seems like it's overly complex for what i'm trying to do?

 

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}

 

Link to comment
Share on other sites

That was my initial thought, however, i'd like to just open up my web server and handle the requests within my network. Otherwise I have to open up the camera to the outside world as well. After i have this figured out i have 3 separate cameras that i'll be attempting to control and each of them would have to bet set up on their own port and accessible to anyone looking to mess with them :P

Link to comment
Share on other sites

That was my initial thought, however, i'd like to just open up my web server and handle the requests within my network. Otherwise I have to open up the camera to the outside world as well. After i have this figured out i have 3 separate cameras that i'll be attempting to control and each of them would have to bet set up on their own port and accessible to anyone looking to mess with them :P

 

No, you do not need to open up the cameras to the internet. I'm assuming you want these actions to be user initiated, so a form and/or links would make the most sense. Personally, I would create a form with a drop-down to select the camera and have different buttons for each action I want to perform. Then, instead of actually submitting the form, I would use Javascript on the button clicks to submit the request to the appropriate camera.

Link to comment
Share on other sites

No, you do not need to open up the cameras to the internet. I'm assuming you want these actions to be user initiated, so a form and/or links would make the most sense. Personally, I would create a form with a drop-down to select the camera and have different buttons for each action I want to perform. Then, instead of actually submitting the form, I would use Javascript on the button clicks to submit the request to the appropriate camera.

 

@mjdamato, maybe you can dumb this down for me a little further? :) My impression is that if I'm at work hitting a website on home server (http://mydomain.com), any GET requests made from the website will have to be accessible from wherever I'm accessing the site. If my form's ACTION hits an internal IP (192.168.1.xxx) won't that submission error out outside the network?

Link to comment
Share on other sites

If you dont want to open it up to outside access you will have to setup a

VPN(Virtual Private Network). Google VPN and you camera make/model.

 

As long as the web server is running on the same network as the cameras, there is no need to expose them to the internet. He only needs to forward port 80 to the machine running the web server. That machine then can send requests to the cameras on the same network.

Link to comment
Share on other sites

According to #6 I think he's not on the same net.

 

Well, HE is not on the same network, but that doesn't mean the SERVER isn't. Of course, that does rule out a JS solution.

 

@bcamp1973: Where are you hosting this page? If you are hosting on another machine at home, then you won't have to open the cameras to the internet. Here is a quick script on what I would do. note, the form uses POST but that is because the FORM is not sending the data to the camera. The form is POSTing to the same script which then sends the GET data to the camera. The script simply uses file_get_contents() to send the URL to the camera. But, you can use another method if that doesn't work

 

<?php

$cameras = array(
    'Camera 1' => '192.168.1.121',
    'Camera 2' => '192.168.1.122',
    'Camera 3' => '192.168.1.123'
);

$actions = array(
    'Turn On' => 'power=on',
    'Turn Off' => 'power=off',
    'Pan Up' => 'direction=panUp',
    'Pan Left' => 'direction=panLeft',
    'Pan Right' => 'direction=panRight',
    'Pan Down' => 'direction=panDown'
);

if (in_array($_POST['action'], array_keys($actions)) && in_array($_POST['camera'], $cameras))
{
    $urlRequest = "http://{$_POST['camera']}/cameraCGI?{$actions[$_POST['action']]}";
    file_get_contents($urlRequest);
    $cameraName = array_search($_POST['camera'], $cameras);
    $response = "Request sent to {$cameraName} at IP {$_POST['camera']} to {$_POST['action']}\n";
}

//Create camera options
$cameraOptions = '';
foreach($cameras as $name => $ip)
{
    $cameraOptions .= "<option value=\"{$ip}\">{$name}</option>\n";
}

?>
<html> 
<head></head> 
<body> 
<pre>
<?php echo $response; ?>
</pre>
<form method="POST">

<div style="text-align:center;">
Camera:
<select name="camera" id="camera">
  <?php echo $cameraOptions; ?>
</select>
<br /><br />
<input type="submit" name="action" value="Turn On" />
<input type="submit" name="action" value="Turn Off" />
<br /><br />
<input type="submit" name="action" value="Pan Up" />
<br />
<input type="submit" name="action" value="Pan Left" />
<input type="submit" name="action" value="Pan Right" />
<br />
<input type="submit" name="action" value="Pan Down" />
</div>

</form>
</body>
</html>

Link to comment
Share on other sites

@bcamp1973: Where are you hosting this page? If you are hosting on another machine at home, then you won't have to open the cameras to the internet. Here is a quick script on what I would do. note, the form uses POST but that is because the FORM is not sending the data to the camera. The form is POSTing to the same script which then sends the GET data to the camera. The script simply uses file_get_contents() to send the URL to the camera. But, you can use another method if that doesn't work

 

@mjdamato, you are correct in your assumption...and thank you for doing a better job of describing it than I did ;) I think your solution makes sense and will do what I'm looking for...I'll keep you posted

Link to comment
Share on other sites

@bcamp1973: Where are you hosting this page? If you are hosting on another machine at home, then you won't have to open the cameras to the internet. Here is a quick script on what I would do. note, the form uses POST but that is because the FORM is not sending the data to the camera. The form is POSTing to the same script which then sends the GET data to the camera. The script simply uses file_get_contents() to send the URL to the camera. But, you can use another method if that doesn't work

 

Ok, using @mjdamato's approach, i'm to the point where i can control the camera...hooray! ...now, i want to grab the motion JPEG that the camera returns using a similar method.  Here's what i've stared with...which isn't working of course :P Hopefully this is possible??

 

header("Content-Type: image/jpeg\n");
fopen("http://192.168.1.112/nphMotionJpeg?Resolution=320x240&Quality=Standard","rb");

Link to comment
Share on other sites

It all depends on what, exactly the camera is returning and what you are wanting to do with it. I don't know what the mode of "rb" is supposed to be doing. "r" is for read, but there's nothing about "b" in the manual. If that "link" is the correct format for the camera to display an image, you probably don't need to add all the additional ligic. Have you tried just a simple IMG tag:

 

<img src="http://192.168.1.112/nphMotionJpeg?Resolution=320x240&Quality=Standard" />

 

By the way, what camera are you using?

Link to comment
Share on other sites

It all depends on what, exactly the camera is returning and what you are wanting to do with it. I don't know what the mode of "rb" is supposed to be doing. "r" is for read, but there's nothing about "b" in the manual. If that "link" is the correct format for the camera to display an image, you probably don't need to add all the additional ligic. Have you tried just a simple IMG tag:

 

<img src="http://192.168.1.112/nphMotionJpeg?Resolution=320x240&Quality=Standard" />

 

By the way, what camera are you using?

 

I'm using a few of the Panasonic "Pet cams". You can read more about them here... http://bit.ly/11VPeO ...just for a little background, this is a silly pet project of mine. I like the cameras and their features, but i HATE their web interface. I'm trying to bring the cameras together into a more integrated interface that works better on my wife and my phones. It's partly because I want them easier to use, and partly because I'm just having fun tinkering around. In the end none of this is truly "necessary" ;)

 

The cameras provide either motion JPEG images or MPEG-4. Regardless of what it's set to it's marked up in the cameras web server HTML like so...

<input type="image" src="nphMotionJpeg?Resolution=320x240&Quality=Standard">

 

So, again, I need to get access to that stream from within my network. If it was a static image it probably wouldn't be a big issue, but not sure since it's a "stream"?

 

...oh, and the "b" in "rb" is supposedly to represent "binary"...or so my googling tells me, but you're right, i didn't see mention of it in the PHP manual either

Link to comment
Share on other sites

The cameras provide either motion JPEG images or MPEG-4. Regardless of what it's set to it's marked up in the cameras web server HTML like so...

<input type="image" src="nphMotionJpeg?Resolution=320x240&Quality=Standard">

 

OK, I got you. The SERVER needs to read the image and output it to the browser since the user (you) are not on the same network as the camera. I tried reading the image directly from another server and outputting to the browser, but couldn't get it to work (not to say it is n't possible). One solution that did work was to copy the image to the server.

 

<?php
//Create URL to image
$cameraIP_1 = '192.168.1.201';
$imageURL = "http://{$cameraIP_1}/nphMotionJpeg?Resolution=320x240&Quality=Standard";
//Read the image file
$content = file_get_contents($imageURL);
//Write image file to server
$fp = fopen("{$cameraIP_1}.jpg", 'w');
fwrite($fp, $content);
fclose($fp);
?>

 

Then in the page you can diaplay the image like so

<img src="<?php echo $cameraIP_1; ?>.jpg" />

 

Link to comment
Share on other sites

<?php
//Create URL to image
$cameraIP_1 = '192.168.1.201';
$imageURL = "http://{$cameraIP_1}/nphMotionJpeg?Resolution=320x240&Quality=Standard";
//Read the image file
$content = file_get_contents($imageURL);
//Write image file to server
$fp = fopen("{$cameraIP_1}.jpg", 'w');
fwrite($fp, $content);
fclose($fp);
?>

 

Then in the page you can diaplay the image like so

<img src="<?php echo $cameraIP_1; ?>.jpg" />

 

 

I tried your solution and it just hangs on me. I don't think it likes the $content = file_get_contents($imageURL); line. Maybe the MJPEG screws it up? I did some more googling and came across the following link....

 

http://www.lavrsen.dk/twiki/bin/view/Motion/MjpegFrameGrabPHP

 

As far as i can tell this is exactly what i'm trying to do (first example) but even their example isn't working for me. I know this has to be possible...unfortunately my knowledge of HTTP headers is nil!

Link to comment
Share on other sites

Got it working! I guess it just took a good night's sleep and fresh eyes ;) I just put the following in a file called getStream.php...

 

set_time_limit(0);
$fp = fsockopen("{$_GET['camera']}", 80, $errno, $errstr, 30);
if(!$fp) {
echo "/public/images/error.jpg";
} else {
fputs($fp, "GET //nphMotionJpeg?Resolution=320x240&Quality=Standard HTTP/1.0\r\n\r\n");
while($str = trim(fgets($fp, 4096)))
header($str);
fpassthru($fp);
fclose($fp);
}

 

and it's used like so...

 

<img src="getStream.php?camera=office"/>

 

and in my webserver's hosts file I set "office" to match with my cameras internal network IP address...I'm sure i'll bollocks something else up and be back with more questions ;)

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.