Reputation: 4218
I have a webcam via Android IP Camera App. I'm writing a php script perform an OCR on a temperature sensor to read the digits and warn me when it goes past a specific temperature. The URL I wish to grab is a shot.jpg via the cam's IP, however the webcam has a username and password of authorization: basic. I was able to get through the authentication with the first method, but it simply outputs a shit ton of random characters. The second method loads a picture, but the src is incorrect. Any suggestions on how to properly format the first method with the content-type would be amazing. Thanks
//METHOD 1
$context = stream_context_create(array(
'http' => array('header' => "Authorization: Basic " . base64_encode("$username:$password"))
));
$data = file_get_contents($url, false, $context);
echo $data;
//METHOD NUMBER 2
header('Authorization: Basic' . base64_encode("$username:$password"));
header('Content-Type: image/jpg');
$data = file_get_contents($url, false);
echo $data;
Upvotes: 1
Views: 1029
Reputation: 145482
You're not outputting the correct Content-Type:
in example 1. file_get_contents
does not pass it on. So the image will be sent to the browser under the assumption it were a HTML page.
This way:
$data = file_get_contents($url, false, $context);
header('Content-Type: image/jpg');
echo $data;
Also, try curl
(which also supports HTTP), for simplifying the login credential use.
In your second example you are using a page response header()
. The Authorization:
line will not be passed over the file_get_contents() URL request there.
Upvotes: 2