Search code examples
phpwebcam

PHP Proxy to display axis camera in website


I have an axiscam with a limit of 20 concurrent connections. I want to embed the MJPG stream in my website. So far I got the following php script:

header('content-type: multipart/x-mixed-replace; boundary=--myboundary');
while (@ob_end_clean());
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://WEBCAM/axis-cgi/mjpg/video.cgi?resolution=320x240');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE );
    $im = curl_exec($ch);
echo $im;
curl_close($ch);
?>

and the corresponding HTML:

<script type="text/javascript">
$(function() {
    var timeout = 2000;
    var refreshInterval = setInterval(function() {
        var random = Math.floor(Math.random() * Math.pow(2, 31));
        $('img#camera').attr('src', '/webcam/webcam.php?r=' + random); //send a random var to avoid cache
    }, timeout);
 })
 </script>
 </head>
<body>
<img height="240" width="320" id="camera" src="">
</body>
</html>

I think I lack some understanding of curl in general but my guess is, that the webserver running curl issues the request to the webcam once the page is requested. If I open my HTML in multiple browsers on multiple computers I get a timeout. So my goal was that I only have one connection opened to the webcam and then just get the images from my webserver. The problem right now is, that somehow I still exceed the maximum number of connections with this solution.


Solution

  • I think you should create a php script which will access camera and save/overwrite file on server, later create cron job to lets say every 5 seconds access this script. When you do that use this saved file to display it on server, because your script at the moment is just creating new connection to the camera for every user.