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?

9
  • 1
    PHP has classes/functions for reading XML data in a structured way. What have you researched, what have you tried so far? Commented Oct 14, 2020 at 13:29
  • I tried this -> $xml = simplexml_load_string($APIResponse); but its empty. Commented Oct 14, 2020 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 showing how you actually fetch and process the data. Commented Oct 14, 2020 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 Commented Oct 14, 2020 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. Commented Oct 14, 2020 at 13:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.