Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. Here is some code to get around this.
  2.  
  3. PHP:
  4.  
  5.   $url = "http://192.168.1.$camera/cgi-bin/snapshot.cgi";
  6.  
  7.   $options = array(
  8.           CURLOPT_URL            => $url,
  9.           CURLOPT_HEADER         => true,
  10.           CURLOPT_VERBOSE        => true,
  11.           CURLOPT_RETURNTRANSFER => true,
  12.           CURLOPT_FOLLOWLOCATION => true,
  13.           CURLOPT_SSL_VERIFYPEER => false,    // for https
  14.           CURLOPT_USERPWD        => $username . ":" . $password,
  15.           CURLOPT_HTTPAUTH       => CURLAUTH_DIGEST
  16.   );
  17.  
  18.   $ch = curl_init();
  19.  
  20.   curl_setopt_array( $ch, $options );
  21.  
  22.   try {
  23.     $raw  = curl_exec( $ch );
  24.  
  25.     // validate CURL status
  26.     if(curl_errno($ch))
  27.         throw new Exception(curl_error($ch), 500);
  28.  
  29.     // validate HTTP status code (user/password credential issues)
  30.     $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  31.     if ($status_code != 200)
  32.         throw new Exception("Response with Status Code [" . $status_code . "].", 500);
  33.  
  34.   } catch(Exception $ex) {
  35.       if ($ch != null) curl_close($ch);
  36.       throw new Exception($ex);
  37.   }
  38.  
  39.   if ($ch != null) curl_close($ch);
  40.  
  41.   $start = strpos($raw,"\xff");
  42.   $end   = strpos($raw,$boundary,$start)-1;
  43.   $frame = substr("$raw",$start,$end - $start);
  44.  
  45.   header("Content-type: image/jpeg");
  46.   echo $frame;
  47.  
  48.  
  49. Javascript -- cache the http credentials in the background so the browser doesn't ask for them:
  50.  
  51. function cacheHttpCameraCredentials(sender='default') {
  52.    // Cache http user/pass for cameras
  53.    $.ajax({
  54.        type: 'GET',
  55.        url: 'http://192.168.1.123/cgi-bin/snapshot.cgi',
  56.         username: 'admin',
  57.         password: 'admin',
  58.         success : function(data) {
  59.         //Success block
  60.         console.log('Cached webserver camera credentials: ' + sender);
  61.        },
  62.         error: function (xhr,ajaxOptions,throwError){
  63.         //Error block
  64.         console.log('Error caching camera credentials: ' + sender);
  65.       },
  66.     });
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement