This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 4 months ago.
$response = curl_exec($ch);
curl_close($ch);
dd($response);
It is of type response string.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns3:GetProductListResponse xmlns:ns3="http://xxx1">
<result>
<status>success</status>
</result>
<products>
<product>
<currencyAmount>900.00</currencyAmount>
<currencyType>1</currencyType>
<displayPrice>900.00</displayPrice>
<isDomestic>false</isDomestic>
<id>557830715</id>
<price>900.00</price>
<productSellerCode>TSRT7777</productSellerCode>
<approvalStatus>6</approvalStatus>
...
To convert this data to xml I used simplexml_load_string()
$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
dd($xml);
And the output is like this.
^ SimpleXMLElement {#435}
I'm trying to access the data in it and try this.
$status = (string)$xml->result->status;
dd($status);
Returns :
^ ""
I tried using simplexml_load_file() and got no results. My main goal is to get this data as json, but I can't do it because I can't read the values.Any help would be great. Thanks in advance.
After #Jacob Mulquin's suggestion I used:
if ($xml === false) {
dump("b");
foreach (libxml_get_errors() as $error) {
dump($error->message);
}
dd("a");
} else {
dd("c");
}
Returned : "c"
Your sample xml is not well formed, for various reasons, but assuming that the actual $response is a well formed xml string, the following should get you what you need:
#first you need to deal with namespaces
$xml->registerXPathNamespace("ns3", "http://xxx1");
#then use xpath to select your target element
$status = $xml->xpath('//ns3:GetProductListResponse//status')[0];
echo $status;
Output should be
success
Related
This question already has answers here:
PHP convert XML to JSON
(23 answers)
Closed 1 year ago.
I need to transform this XML to proper JSON format using PHP, but nothing I've tried seemed to work. It is a substr-ed version of a SOAP Response.
<item><OBJID>00823006</OBJID><SHORT>PUB0007</SHORT><STEXT>Leading at The Speed of Trust</STEXT><SUTXT/><BEGDA>2015-01-01</BEGDA><ENDDA>2015-12-31</ENDDA></item><item><OBJID>00823016</OBJID><SHORT>PUB0017</SHORT><STEXT>The 5 Choices to Extraordinary Prod.</STEXT><SUTXT/><BEGDA>2015-01-01</BEGDA><ENDDA>2015-12-31</ENDDA></item><item><OBJID>00823020</OBJID><SHORT>PUB0021</SHORT><STEXT>8 Etos Kerja Profesional</STEXT><SUTXT/><BEGDA>2015-01-01</BEGDA><ENDDA>2016-12-31</ENDDA></item><item><OBJID>00823028</OBJID><SHORT>PUB0029</SHORT><STEXT>CSS from Design & Implementation to f/u</STEXT><SUTXT/><BEGDA>2015-01-01</BEGDA><ENDDA>2015-12-31</ENDDA></item>
tried using simplexml_load_string and SimpleXMLElement with examples found through google and always end up with either garbled, malformed json or empty string.
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
Could someone here help? many thanks in advance
$xml = "
<parent>
<item>
<OBJID>00823006</OBJID>
<SHORT>PUB0007</SHORT>
<STEXT>Leading at The Speed of Trust</STEXT>
<SUTXT/>
<BEGDA>2015-01-01</BEGDA>
<ENDDA>2015-12-31</ENDDA>
</item>
<item>
<OBJID>00823016</OBJID>
<SHORT>PUB0017</SHORT>
<STEXT>The 5 Choices to Extraordinary Prod.</STEXT>
<SUTXT/>
<BEGDA>2015-01-01</BEGDA>
<ENDDA>2015-12-31</ENDDA>
</item>
<item>
<OBJID>00823020</OBJID>
<SHORT>PUB0021</SHORT>
<STEXT>8 Etos Kerja Profesional</STEXT>
<SUTXT/>
<BEGDA>2015-01-01</BEGDA>
<ENDDA>2016-12-31</ENDDA>
</item>
<item>
<OBJID>00823028</OBJID>
<SHORT>PUB0029</SHORT>
<STEXT>CSS from Design & Implementation to f/u</STEXT>
<SUTXT/>
<BEGDA>2015-01-01</BEGDA>
<ENDDA>2015-12-31</ENDDA>
</item>
</parent>";
$xmlFile = simplexml_load_string($xml);
$json = json_encode($xmlFile);
echo $json;
The above seems to work for me. Problem is, is that your XML doesn't have a single parent-element. If you add that, you get proper decoded XML / JSON back.
To add a parent element, you could do the following.
$xml = "<parent>";
$xml .= $xmlToAdd;
$xml .= "</parent>";
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
My asmx WEB service return this XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<PRODUCT>
<DESC>Vanilla ice cream</DESC>
<CODEERR>0</CODEERR>
</PRODUCT>
Calling WEB service from this PHP code
$SoapCli = new SoapClient('http://www.foo.com/MyService.asmx?WSDL');
$params = array(
'PARAM1' => 'some_param_1',
'PARAM2' => 'some_param_2',
);
$resp_WS = $SoapCli->__soapCall('MyFunction', array($params));
var_dump($resp_WS);
result is
object(stdClass)#11946 (1) {
["MyFunctionResult"]=>
object(stdClass)#11947 (1) {
["any"]=>
string(88) "<product xmlns=""><desc>Vanilla ice cream</desc><codeerr>0</codeerr></product>"
}
}
but, after googling a lot, I don't find PHP code for retreive values of two fields DESC and CODER
You can use json_encode,json_decode,simplexml_load_string to parse the XML response, try the following code snippet to read the XML response
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<PRODUCT>
<DESC>Vanilla ice cream</DESC>
<CODEERR>0</CODEERR>
</PRODUCT>';
$res = json_decode(json_encode((array)simplexml_load_string($xml)),true);
Now you can use $res['DESC'] and $res['CODEERR'] to retrive the values.
This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 7 years ago.
I have an XML that's coming back from an cURL post that's returning:
$output=<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mtMessageRsp carrier="102" messageId="769f2e4f-56da-4865-988a-a9199c387a48"/>
I'm returning this as an XML via:
return simplexml_load_string($output);
$result is catching the return and it's coming back as:
$result = {
"#attributes" : {
carrier : "102",
messageId : "8d691cbe-d188-42b1-9041-387666d39c6a"
}
How can I drill down to get the messageId as plane text? When I use this:
$result['messageId']
I get:
{
"0" : "bf629ae9-c86a-486a-bfb0-704e16448ddf"
}
But I just want:
bf629ae9-c86a-486a-bfb0-704e16448ddf
Figured it out in another post:
$msgID = (string) $result['messageId'];
You have to cast simpleXML Object to a string.
POST: Get value from SimpleXMLElement Object
Hi guys very new to the php world.
I am listening for a PHP post that contains xml, when the xml is retrieved i need to access individual nodes. I am able to echo the full xml file but not individual attributes.
Currently I am just sending the data using a chrome extension Postman. There is no front end code. Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<job>
<job_ref>abc123</job_ref>
<job_title>Test Engineer</job_title>
</job>
And here is my PHP:
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$xml = file_get_contents('php://input');
echo $xml;
$xml=simplexml_load_file($xml);
echo $xml->job_ref . "<br>";
echo $xml->job_title . "<br>";
}else{
die();
}
Any hep wopuld be amazing am I am very stuck.
Many thanks
simplexml_load_file expect PATH to the XML file, not its content. You have to use simplexml_load_string instead:
$xml = simplexml_load_string($xml);
This question already has answers here:
parse an XML with SimpleXML which has multiple namespaces [duplicate]
(5 answers)
Closed 8 years ago.
Any recommendations how to parse this SOAP response and obtain the value of name for the report_type? Notice there are two instances of name; one under report_type and the other under severity.
Here is the SOAP response
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:getIRResponse xmlns:ns1="http://ws.icontent.idefense.com/V3/2">
<ns1:return xsi:type="ns1:IRResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:report_type>
<ns1:id>0</ns1:id>
<ns1:name>Original Vulnerability</ns1:name>
</ns1:report_type>
<ns1:severity>
<ns1:id>0</ns1:id>
<ns1:name>HIGH</ns1:name>
</ns1:severity>
</ns:return>
</ns1:getIRResponse>
</soapenv:Body>
</soapenv:Envelope>
Here is the PHP code I'm using:
<?php
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns1', 'http://ws.icontent.idefense.com/V3/2');
foreach ($xml->xpath('//ns1:report_type/name') as $item)
{
echo 'Name: '.$item,'<br>';
}
?>
The PHP code doesn't echo anything. When I use ($xml->xpath('//ns1:name') as $item) it returns both names (Original Vulnerability and HIGH).
I know I'm missing something stupid. Can you help please? Thank you in advance!
Firstly, I've corrected this element
</ns:return>
and changed it to
</ns1:return>
I seem to get the result you're after by duplicating the namespace prefix in both xpath segments
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns1','http://ws.icontent.idefense.com/V3/2');
foreach ($xml->xpath('//ns1:report_type/ns1:name') as $item)
{
echo 'Name: '.$item,'<br>';
}
output
Name: Original Vulnerability