0

I need to check for the HTTP Response from a XML endpoint in PHP http_response_code() returns 200ok, but I need to know the response from the actual endpoint. I think I am blocked on the firewall because of prior failures.

Please be kind and ask me if I am not making myself clear. I understand it should be fairly straightforward.

$auth = base64_encode ( $this->config->item ( "user" ) . ":" . $this->config->item ( "pass" ) );
$context = stream_context_create ( array ('http' => array ('header' => "Authorization: Basic $auth" ) ) );
    
$unitsXML = file_get_contents ( $this->config->item ( "server_url" ) . "/", false, $context );

libxml_use_internal_errors ( true );
$objXML = simplexml_load_string ( $unitsXML );
var_dump(http_response_code()); // <- this returns 200ok, ut how to get this code but from simplexml http request?

At the moment I only have a blank page with null

helpsergio
  • 33
  • 1
  • 6
  • `http_response_code()` is the response code _you_ set in your current script for the client that called that PHP code. It has nothing to do with any requests your code makes. Also, `simplexml_load_string()` expects to get a string containing XML and doesn't make any http request at all. To load XML from a URL, you should use [simplexml_load_file()](https://www.php.net/manual/en/function.simplexml-load-file.php). – M. Eriksson Jun 02 '22 at 14:40
  • @M.Eriksson I also need to include a basic authentication header. I am updating my question. – helpsergio Jun 02 '22 at 14:45
  • Does this solve your issue [HTTP requests with file_get_contents, getting the response code](https://stackoverflow.com/questions/15620124/http-requests-with-file-get-contents-getting-the-response-code)? And the title is wrong. You're not looking for simplexml http response code, you're looking for `file_get_contents()` response code (which is the function that actually makes the http request) – M. Eriksson Jun 02 '22 at 14:50
  • @M.Eriksson I believe I cannot use that function because I need to authenticate first to the endpoint so it returns the response. – helpsergio Jun 03 '22 at 11:54
  • _"I believe I cannot use that function"_ - What function? Please be more specific. Your current code should already work fine (except from the `http_response_code()` which doesn't belong there), as long as all the config params are correct. The only thing you need to do is check the response code after your `file_get_contents()`-call (the link in my second comment shows how). – M. Eriksson Jun 03 '22 at 12:57
  • @M.Eriksson I Can't use file_get_contents() because I don't see how to authenticate with it. – helpsergio Jun 03 '22 at 13:04
  • What do you mean? You're literally adding a Basic Authentication header to the `$context`, which should handle the authentication, as long as the URL uses basic authentication (which we obviously have _no idea_ about) and the credentials are correct. – M. Eriksson Jun 03 '22 at 13:07

0 Answers0