title: Invoking WCF service with PHP (with federated security)
body: <p>I’m trying to invoke a WCF service (.NET) from PHP. It’s a little more complicated than just using a <code>SoapClient</code> since the service uses a <code>WS2007FederationHttpBinding</code> to authenticate.</p>
  
  <p>Here’s the code I’m using at the moment. I haven’t even added credentials as I’m not sure how, but regardless, I’m not even at the point where I’m getting access denied errors.</p>
  
  <pre><code>$wsdl = "https://slc.centershift.com/sandbox40/StoreService.svc?wsdl";
  $client = new SoapClient($wsdl,array(
           //'soap_version'=&gt;SOAP_1_2 // default 1.1, but this gives 'Uncaught SoapFault exception: [HTTP] Error Fetching http headers'
          ));
  $params = array();
  $params['SiteID'] = 123;
  $params['GetPromoData'] = false;
  
  $ret = $client-&gt;GetSiteUnitData(array('GetSiteUnitData_Request'=&gt;$params));
  print_r($ret);
  </code></pre>
  
  <ol>
  <li><p>Which WSDL should I be pointing to?</p>
  
  <ul>
  <li><code><a href="https://slc.centershift.com/Sandbox40/StoreService.svc" rel="nofollow noreferrer">https://slc.centershift.com/Sandbox40/StoreService.svc</a><strong>?wsdl</strong></code><br>
  Seems to be very short, but includes a reference to (note the <code>wsdl0</code>) <code><a href="https://slc.centershift.com/Sandbox40/StoreService.svc" rel="nofollow noreferrer">https://slc.centershift.com/Sandbox40/StoreService.svc</a><strong>?wsdl=wsdl0</strong></code></li>
  <li><code><a href="https://slc.centershift.com/Sandbox40/StoreService.svc" rel="nofollow noreferrer">https://slc.centershift.com/Sandbox40/StoreService.svc</a><strong>?singleWsdl</strong></code><br>
  Seems to have everything in it.</li>
  </ul></li>
  <li><p>Do I need to specify SOAP 1.2? When I do, I get a connection timeout (<code>[HTTP] Error Fetching http headers</code>). When I don’t, the default of SOAP 1.1 is used and I get a <code>[HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'</code>. Is this because I’m not authenticated yet, or because I’m using the wrong SOAP version?</p></li>
  <li><p>How to authenticate in PHP? Here’s the corresponding .NET/C# code. Do I need to somehow put these as SOAP headers? Or am I thinking about it all wrong, and I need to do some kind of authentication before I even call the method (from what I read, I’m supposed to get a token back and then use it for all future method calls – I think I see an example of this in <a href="https://stackoverflow.com/a/24637585">an answer here on Stack Overflow</a>.
  <a href="https://i.stack.imgur.com/oeRGE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oeRGE.png" alt=""></a></p></li>
  </ol>
  
  <p>If I call <code>$client-&gt;__getFunctions()</code>, using either WSDL and either SOAP version, I’m getting a valid list of all functions, so I assume either of these is fine and my real issue is the authentication.</p>
  
  <p>Other programmers I’ve talked to had spent time trying to get this to work, but gave up and instead implemented a proxy in .NET. They pass their parameters from PHP to their own unsecured .NET service, which in turn calls this secure service. It works, but seems crazily inefficient to me, and counter-productive, as the purpose of WCF is to support all types of clients (even non-HTTP ones!).</p>
  
  <p>I’ve read <a href="https://msdn.microsoft.com/en-us/library/aa347982(v=vs.110).aspx" rel="nofollow noreferrer">How to: Create a WSFederationHttpBinding</a> on MSDN, but it didn’t help.</p>