0

I am working on proxy with PHP. In my php code I am sending proper required headers and expected to get response body and headers. However I am getting response body correctly as I want but not getting headers properly (supposed to get Status 200 but getting 401). When i traced with firefox I found that SAP URL itsself making 2 request internally by using data which I send. so with my first request it is not authenticated so SAP url itslef managining to send same request again and 2nd time it gives both proper response body with headers. Howevber I php code when I get it i get response body from 2nd response and headers from 1st response. here is code.

    $opts = array( 
      'http'=>array( 
             'method'=>"POST", 
         'content' => $xml_request,
             'header'=>array("Host:" . $sap_url,                                                                                                              
           "Content-Type: text/xml; charset=UTF-8",                                                                     
           $authstring,$xml_request) 
              ) 
     );

    $context  = stream_context_create($opts);   
$result = file_get_contents($sap_url, false, $context); 


$http_res_array =   get_headers($sap_url);
user1806296
  • 41
  • 2
  • 11

2 Answers2

0

You should probably use curl functions instead and do BOTH requests yourself. file_get_contents, does the second request for you, but takes away the possibility to fetch the second headers.

nl-x
  • 11,762
  • 7
  • 33
  • 61
0

Maybe a little old but anyways:

You're using the get_headers()-function to get the headers. It's documentation states that:

Fetches all the headers sent by the server in response to a [new] HTTP request

It doesn't empathize that this function will actually send a new request to the server and return the response-header for that request. Therefor, the headers can be slightly different.

Since you're using file_get_contents() to load the content, you can use the global $http_response_header-variable right after your request, which will contain the response-header from the last executed request.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111