authentication - PHP basic auth file_get_contents() -


this question has answer here:

i need parse xml data website. xml data in raw format, before need authentificate (basic webserver based auth, username & password).

i tried:

$homepage = file_get_contents('http://user:password@ip:port/folder/file'); 

but following error:

failed open stream: http request failed! http/1.0 401 unauthorized

php seems have problems authentification. idea how fix this?

you need add stream context data in request. try following untested code. it's based on 1 of examples on php documentation file_get_contents():

$auth = base64_encode("username:password"); $context = stream_context_create(['http' => ['header' => "authorization: basic $auth"]]); $homepage = file_get_contents("http://example.com/file", false, $context ); 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -