webcam server - use PHP to grab mjpeg or single frame

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
devusr
Forum Newbie
Posts: 1
Joined: Tue Sep 17, 2013 8:04 pm

webcam server - use PHP to grab mjpeg or single frame

Post by devusr »

Hello,
I have a webcam on my home LAN that can be accessed on the net. The router port forwards the port XXX to the port number for the webcam's mjpeg stream. The webcam doesn't have any password protection features, so I just pick a really difficult to guess port number :).

I also have a shared host account. I'd like to use PHP to display the webcam (either as a single image or mjpeg stream). This way, I'm hiding the IP address and port number of the my home webcam, and I can wrap the whole thing around a simple password prompt, and all the sensitive info would be on the server side.

I played with PHP on a home PC with nginx installed, and found this code works. But when I use the same PHP code on my shared host, it doesn't. The share host seems to be going to my home network and try to download the mjpeg stream (which would never complete) instead of stream it back to me real time. That is my guess on what's happening anyways. On the browser, all I see is the "waiting for host..." message.

So, I might switch strategies and instead of streaming the mjpeg stream, I would like a way to just grab a single frame of the mjpeg stream. But I don't have a clue on how to do this.

Here's the code I've been using to grab the entire stream. It works locally, but not when the code is on the shared host. Locally, I just change the dynamic dns address to the IP of the webcam, and the port number to the internal port number of the webcam stream. Also, I copied this code from another site, and have very little understanding of what the code is actually doing.

Code: Select all

set_time_limit(0);
$fp = fsockopen ("xxxx.dyndns.org", 20804, $errno, $errstr, 30);
if (!$fp) {
	echo "$errstr ($errno)<br>\n";
} else {
	fputs ($fp, "GET / HTTP/1.0\r\n\r\n");
	while ($str = trim(fgets($fp, 4096)))
	   header($str);
	fpassthru($fp);
	fclose($fp);
	}
Thanks.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: webcam server - use PHP to grab mjpeg or single frame

Post by Eric! »

Try testing it on your local host but through the dyndns connection and not "localhost". Most likely there is a problem with the routing or your computer's firewall is blocking the outside request.
Post Reply