title: Bad request error when sending PHP SOAP request but request itself correct
body: <p>I've been stuck on this problem for ages now and have been unable to find a solution after <em>many many</em> hours of searching.</p>
  
  <p>I'm using a Fedex web-service that returns a shipping rate. The following is my php code</p>
  
  <pre><code>try { 
  
      $client = new SoapClient($path_to_wsdl, array(
                       'uri' =&gt; 'http://fedex.com/ws/rate/v14',
                       'location' =&gt; 'https://ws.fedex.com:443/web-services/rate',
                       'soap_version' =&gt; 'SOAP_1_1',
                       'login' =&gt; 'myusername',       //I don't know whether this is needed but but it is an internal company site 
                       'password' =&gt; 'password',      //so I log in with this username and pw to see it
                       'proxy_host' =&gt; 'proxy-host-name',
                       'proxy_port' =&gt; 443,
                       'proxy_login'    =&gt; '',
                       'proxy_password' =&gt; '',
                       'trace' =&gt; 1,
                       'exceptions' =&gt; 1));
  
      $data = array(
          'Action' =&gt; "http://fedex.com/ws/rate/v14/getRates"
      );
      $header = new SoapHeader('http://fedex.com/ws/rate/v14', 'FedexHeader', $data, false);
      $client-&gt;__setSoapHeaders($header);
  
      if(setEndpoint('changeEndpoint')){
          $newLocation = $client-&gt;__setLocation(setEndpoint('endpoint'));
      }
      $response = $client -&gt; getRates($request);
  } catch (SoapFault $exception) {
      echo '&lt;h2&gt;exception&lt;/h2&gt;';
      print_r($exception); 
      echo '&lt;br/&gt;&lt;h2&gt;exception trace&lt;/h2&gt;';
      var_dump($exception-&gt;getTraceAsString());
  
      echo '&lt;br/&gt;Request headers : &lt;br/&gt;&lt;xmp&gt;', 
          $client-&gt;__getLastRequestHeaders(), 
          '&lt;/xmp&gt;&lt;br/&gt;';
      echo 'Request : &lt;br/&gt;&lt;xmp&gt;', 
          $client-&gt;__getLastRequest(), 
          '&lt;/xmp&gt;&lt;br/&gt;';
      echo 'Response headers: &lt;br/&gt;&lt;xmp&gt;', 
          $client-&gt;__getLastResponseHeaders(), 
          '&lt;/xmp&gt;&lt;br/&gt;';
      echo 'Response : &lt;br/&gt;&lt;xmp&gt;', 
          $client-&gt;__getLastResponse(), 
          '&lt;/xmp&gt;&lt;br/&gt;';               
          printFault($exception, $client);        
  }
  </code></pre>
  
  <p>This is actually sample code from Fedex - the only parts I have changed are the pathname to the wsdl file, adding more debug information and setting the SoapHeader.</p>
  
  <p>The following is the Request Header:</p>
  
  <pre><code>POST /web-services/rate HTTP/1.1 Host: ws.fedex.com 
  Connection: Keep-Alive User-Agent: PHP-SOAP/5.3.27 
  Content-Type: text/xml; charset=utf-8 
  SOAPAction: "http://fedex.com/ws/rate/v14/getRates" 
  Content-Length: 2974
  </code></pre>
  
  <p>and the response header:</p>
  
  <pre><code>HTTP/1.1 400 Bad Request 
  Date: Mon, 17 Feb 2014 16:25:56 
  GMT Server: Apache/2.2 
  Content-Length: 226 
  Connection: close 
  Content-Type: text/html; charset=iso-8859-1
  </code></pre>
  
  <p>Here is part of the SOAP request itself that is generated:</p>
  
  <pre><code>&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://fedex.com/ws/rate/v14" xmlns:ns2="http://www.w3.org/2005/08/addressing"&gt;
      &lt;SOAP-ENV:Header&gt;
        &lt;ns2:Action&gt;http://fedex.com/ws/rate/v14/getRates&lt;/ns2:Action&gt;
      &lt;/SOAP-ENV:Header&gt;
  
      &lt;SOAP-ENV:Body&gt;
        &lt;ns1:RateRequest&gt;
           &lt;ns1:WebAuthenticationDetail&gt;
              &lt;ns1:UserCredential&gt;
                 //user credentials
              &lt;/ns1:UserCredential&gt;
           &lt;/ns1:WebAuthenticationDetail&gt;
           &lt;ns1:ClientDetail&gt;
             //client details
           &lt;/ns1:ClientDetail&gt;
           &lt;ns1:RequestedShipment&gt;
              //shipping details ..
           &lt;/ns1:RequestedShipment&gt;
        &lt;/ns1:RateRequest&gt;
      &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>The thing is that when I paste this request into SOAP UI it returns the correct response. Also the Fedex support team have confirmed that it is correct.</p>
  
  <p>This is why I think it has to be an error with the PHP. (I was getting a Could not connect to Host error for a long time - I believe it was related to the proxy host but our ops team have confirmed that this request is hitting the proxy now so I think this is resolved)</p>
  
  <p>I saw references to setting SoapHeader in a few other posts so I have done this - however I'm not sure if I am using the correct namespace. Also I'm wondering if I should be using soapAction instead of Action as the wsdl refers to soapAction? Here is that part from the wsdl file:</p>
  
  <pre><code>&lt;operation name="getRates"&gt;
    &lt;s1:operation soapAction="http://fedex.com/ws/rate/v14/getRates" style="document"/&gt;
  &lt;/operation&gt;
  </code></pre>
  
  <p>However, I have tried using both and it didn't make a difference. Also this request works in SoapUI without having the soap header..</p>
  
  <p>UPDATE: I did think originally that it might be related to SSL Speaking to Fedex support it seems I do not need to use SSL.</p>
  
  <p>The fact that the request works in SoapUI does make me think it has to be something in the code.</p>
  
  <p>Any tips on how I could debug this more or suggestions on what the issue could be would be very much appreciated.</p>