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>";
?>
1

1 Answer 1

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:[email protected]/index.html

In this case it would be:

$url = "http://user:[email protected]: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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.