0

When trying to retrieve a webcam image remotely with PHP cURL, I get a "connection refused" error. If I try to retrieve the image using the URL in a browser on my local machine, I am prompted for credentials, and then it gives me the image. Why is this happening? There shouldn't be any difference in who's asking for the image, either myself locally or the script on another server, correct?

My code:

    <?php 
$token = "user:password";
$url = "http://000.000.000.000:000/cgi-bin/image.jpg";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_USERPWD, $token);
/*curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);*/                    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

if( curl_exec($ch) === false ){
    echo curl_error($ch);
}else{
    $data = curl_exec($ch);
}
curl_close($ch);

echo "<pre>";
print_r($data);
echo "</pre>";
?>
andrew
  • 19
  • 5
  • Possible duplicate of [How do I make a request using HTTP basic authentication with PHP curl?](https://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) – Ryan Tuosto Aug 10 '17 at 23:55

1 Answers1

1

This is most likely Basic access authentication. If you go to a url in the browser it shows a login popup, but you can also include the username and password in the url like this:

https://Aladdin:OpenSesame@www.example.com/index.html

In this case it would be:

$url = "http://user:password@000.000.000.000:000/cgi-bin/image.jpg";

EDIT Since you are using curl, it has a function for sending the authentication header.

curl_setopt($process, CURLOPT_USERPWD, $username.':'.$password);

In this case:

curl_setopt($process, CURLOPT_USERPWD, $token);

Also, see this answer.

Peter Rakmanyi
  • 1,475
  • 11
  • 13