title: SOAP XML Request with Basic Authentication in PHP
body: <p>I am trying to setup a SOAP request in PHP using HTTP basic Authentication. For some reason I keep getting, <em>HTTP/1.1 401 Unauthorized</em> error.</p>
  
  <p>This is an example of the request I'm trying to create:</p>
  
  <pre><code>POST https://url/someurl/soap HTTP/1.1
  Accept-Encoding: gzip,deflate
  Content-Type: text/xml;charset=UTF-8
  SOAPAction: "SomeSoapAction"
  User-Agent: SomeUser Client-HttpClient/3.1
  Content-Length: 1503
  Authorization: Basic dGsomebasicreyteryheyp0ZXN0ZXI=
  Host: somehost.com  
  </code></pre>
  
  <p>This is a snippet of my code:</p>
  
  <pre><code>ini_set('display_errors',1);
  ini_set("soap.wsdl_cache_enabled", "0");
  error_reporting(E_ALL);
  $wsdl = "urltosomewsdlfile.wsdl";
  $url = "somerl";
  $username = "username";
  $password = "password";
  $encodedstuff = base64_encode("{$username}:{$password}");
  
  $client = new SoapClient($wsdl, array('exceptions' =&gt; true,'trace' =&gt; true,'login' =&gt; 'somelogin', 'password' =&gt; 'somepw'));
  $header = new SoapHeader($url, 'Authorization: Basic', $encodedstuff);
  $client-&gt;__setSoapHeaders($header);
  
  try {
      $result = $client-&gt;SomeSoapAction();
      } catch (SoapFault $soapFault) {
          var_dump($soapFault);
          echo "&lt;br /&gt;Request :&lt;br /&gt;", htmlentities($client-&gt;__getLastRequest()), "&lt;br /&gt;";
          echo "&lt;br /&gt;Response :&lt;br /&gt;", htmlentities($client-&gt;__getLastResponse()), "&lt;br /&gt;";
      }
  </code></pre>
  
  <p>If I take the username and password out of the <em>new SoapClient section</em> it throws up <em>Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from...</em></p>
  
  <p>If I put in the base64 encoded variable to the <em>new SoapClient section</em> instead of the username/pw it throws the same Parsing error as above:
      <em>$client = new SoapClient($wsdl, array( 'authentication' => "Basic $encodedstuff", 'exceptions' => true,'trace' => true,));</em></p>
  
  <p>The username and password work when loading the URL directly.</p>
  
  <p>Can anyone point me in the right direction? Thanks for any help.</p>
  
  <hr>
  
  <p>Update 12/18/18: I've been trying this as a new method suggested, but still receive the same 401 error message:</p>
  
  <pre><code>$opts = array( 'http'=&gt;array( 'method'=&gt;"POST", 'header' =&gt; "Authorization: Basic " . base64_encode("{$user}:{$pw}") ) ); 
  $context = stream_context_create($opts); 
  $client = new SoapClient($wsdl, array('stream_context' =&gt; $context,'exceptions' =&gt; true,'trace' =&gt; true,)); 
  $header = new SoapHeader($url, 'Authorization: Basic', $encodedstuff); 
  $client-&gt;__setSoapHeaders($header);
  </code></pre>
  
  <hr>
  
  <p>Update 15/01/19: 
  I'm still having issues with this. </p>
  
  <p>Using SOAPUI I can make a successful request and gain authentication. I can't see what the difference is between the SOAPUI request and my own PHP request.</p>
  
  <p>I've noticed that the request header created by my PHP code alters the POST and Host endpoint URLs. The first part of the POST URL has the domain removed and the Host also has the first section of the URL removed. </p>
  
  <p>SOAPUI request header (correct returns 200):</p>
  
  <pre><code>POST https://test-example.com/file/SOAP HTTP/1.1
  Host: test-example.com
  </code></pre>
  
  <p>PHP request header (returns a 401):</p>
  
  <pre><code>POST /file/SOAP HTTP/1.1
  Host: example.com
  </code></pre>
  
  <p>This seems to be causing the problem as it looks as if I'm pointing to the wrong endpoint. Does anyone have any advice on what might be wrong with my PHP code to break this request? Thanks.</p>