Accessing a URL protected by htpasswd (Basic Auth) with php

It is possible to access a URL protected by htpasswd with Basic Authentication through php scripts. Here is an example :

<?php 
$url="url to access";
$https_user="";
$https_password="";
$opts = array('http' =>
  array(
    'method'  => 'POST',
    'header'  => "Content-Type: text/xml\r\n".
      "Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
    'content' => $body,
    'timeout' => 60
  )
);
                       
$context  = stream_context_create($opts);

$result = file_get_contents($url, false, $context, -1, 40000);
echo $result;
?> 

You may also like...