Reputation: 1065
As a part of a school project, i am making a call of the school's api.
Method: GET Response Format: Xml
Now i use curl to make the web request.
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data=curl_exec($ch);
curl_close($ch);
Now how do i parse the xml output to get the data i want?
Hope the question is clear
Upvotes: 1
Views: 1614
Reputation: 9056
Take a look at SimpleXML
, DOMDocument
, XMLParser
and XPath
. I usually prefer SimpleXML
, but as many people, as many opinions...
You will find lots of examples in the documentation to this PHP classes.
Upvotes: 1
Reputation: 237817
This depends on the exact nature of the content and, moreover, its structure.
The basics are to use DOMDocument
(or, alternatively, simplexml
, which I dislike as an API) to parse the document, then to use DOM traversal or XPath to find the content you want.
An example might look like this:
$dom = new DOMDocument;
$dom->loadXML($data); // data from cURL request
$xpath = new DOMXPath($dom);
$names = $xpath->query('//student/name'); // find all name elements that are direct children of student elements
foreach ($names as $name) {
echo $name->nodeValue;
}
The exact code you want depends on the structure of the XML and what content you want to get out of it.
Upvotes: 2