Skip to content

Instantly share code, notes, and snippets.

@sirtawast
Last active October 24, 2018 10:12
Show Gist options
  • Save sirtawast/d4c4cb94a383355183a55477094e8468 to your computer and use it in GitHub Desktop.
Save sirtawast/d4c4cb94a383355183a55477094e8468 to your computer and use it in GitHub Desktop.
Curl stream and buffer webcam stuff, exit from CURLOPT_WRITEFUNCTION with own function
<?php
class CamStream {
protected $code_done = 99999999;
protected $buffer = "";
public function start_stream($url="https://example.org", $passwd="hello:world") {
$exit_ok = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, $passwd);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'my_stream_handler_func'));
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
if(curl_errno($ch)){
$exit_ok = strpos(curl_error($ch), "$this->code_done") !== false;
if ($exit_ok) {
curl_close($ch);
return my_success_func();
}
}
curl_close($ch);
return $exit_ok;
}
protected function my_stream_handler_func($ch, $data) {
// .. imagine our stream would contain "lorem", "ipsum", "dolor", "sit", "amet" repeatedly
$buffer .= $data;
if($buffer === "lorem ipsum dolor sit amet") {
return $this->code_done;
}
return strlen($data);
}
protected function my_success_func() {
// return true; // or whatevs
}
}
$cam = new CamStream();
$success = $cam->start_stream();
var_dump($success);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment