1

Request:

$headers = array(   'Content-Type:application/xml'  ); 
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => 'https://example.com',
            CURLOPT_POST => 1,
            CURLOPT_HEADER => $headers,
            CURLOPT_USERPWD=>'test:test',
            CURLOPT_POSTFIELDS => $XMLData
        ));
        $APIResponse = curl_exec($curl);
        curl_close($curl);

And I get this response from an API

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: ----
X-AspNet-Version: -----
X-Powered-By: ASP.NET
Date: ---- GMT
Content-Length: 100

<?xml version="1.0" encoding="utf-8"?><response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.com"><ID>12345</ID></response>

I want to fetch ID from xml ID tag. How can I get that in my php code?

Akaash K
  • 11
  • 6
  • 1
    PHP has classes/functions for reading XML data in a structured way. What have you researched, what have you tried so far? – ADyson Oct 14 '20 at 13:29
  • I tried this -> $xml = simplexml_load_string($APIResponse); but its empty. – Akaash K Oct 14 '20 at 13:37
  • Ok. So, what was the exact content of `$APIResponse`? Is it just the XML, or the whole text you've shown above? Please make a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) showing how you actually fetch and process the data. – ADyson Oct 14 '20 at 13:38
  • Content in question is what all the content that I am getting in response var -> $APIResponse. I need to extract /read XML from it – Akaash K Oct 14 '20 at 13:41
  • how exactly are you getting that data into PHP then? Most libraries which handle HTTP requests will give you the response data and the headers in separate variables. That's why I asked you to show your code. – ADyson Oct 14 '20 at 13:42
  • I update my question, please take a look on it.. thanks – Akaash K Oct 14 '20 at 13:44
  • I think CURLOPT_HEADER doesn't do what you think it does. That doesn't include your _request_ headers in the request, it sets whether the _response_ headers are included in the output or not. set `CURLOPT_HEADER => 0` and your problem should go away - then only the XML response should be included in the output from curl_exec. At this page: https://www.php.net/manual/en/function.curl-setopt.php the manual described what this option does. – ADyson Oct 14 '20 at 14:00
  • The way you set custom _request_ headers is shown here (and in many other examples online): https://stackoverflow.com/a/8115709/5947043 – ADyson Oct 14 '20 at 14:01
  • Great, I added it as a proper answer below, so please feel free to mark it as "accepted" - thanks :-) – ADyson Oct 14 '20 at 15:07

1 Answers1

1

You're getting the HTTP header data included within your response from cURL, which is making it hard to extract the XML part.

However this is happening due to a simple misunderstanding - the CURLOPT_HEADER option doesn't do what you think it does.

That doesn't include your request headers in the request (as your code seems to be trying to do), instead it sets an option telling cURL whether or not the response headers should be included in the main output or not. If you set

CURLOPT_HEADER => 0 

in your code, your problem should go away - then only the response body (in your case, just the XML) will included in the output from curl_exec.

In the meantime, if you need to set custom HTTP headers in your request, you can do it via the CURLOPT_HTTPHEADER option - a detailed example can be found here and in many other places online.

P.S. Admittedly these option names are not, in themselves, very clear about the difference between them, but the PHP manual does describe what they do in more detail.

ADyson
  • 57,178
  • 14
  • 51
  • 63