I am doing a curl request and getting the following back:
//....curl stuff....//
$result = curl_exec($curl);
curl_close ($curl);
print_R($result);
<html><body onload="if (parent.submitterLoaded)
parent.submitterLoaded();">{"AuthenticationType":0,
"DateDisplayFormat":1, "SystemURL":"https://rmm.server.com",
"Username”:”user”, "UserID":"12205_1", "Error":"", "Success":true,
"ClientID":1, "SessionGuid":"9eb91231b04-feca-4704-b445-
cc5b369581e3", "tag":"", "LastRequestDateTime":"636421428277379996"}
</body></html><?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<LoginResponse xmlns="http://Iris.net" /></soap:Body></soap:Envelope>
I have tried xml_parser_create and
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
and i get a garbled mess in return.
Warning: simplexml_load_string(): Entity: line 1: parser error : XML
declaration allowed only at the start of the document in
/var/www/cron/billing/test.php on line 68
PHP Warning: simplexml_load_string(): b6-bd4dd8a0760b",
"LastRequestDateTime":"636421426011959977"}</body></html><?xml in
/var/www/cron/billing/test.php on line 68
PHP Warning: simplexml_load_string():
^ in /var/www/cron/billing/test.php on line 68
I can see what appears to be some json at the {"Keys" area of the response. How can i parse this correctly?
What other info do you need to help answer question?
The first warning indicate the parser doesn't like the second part <?xml version... So get rid of it:
$result = substr($result, 0, strpos($result, '<?xml version'));
Then to pull out the JSON string, use:
$jsonString = (string) simplexml_load_string($result)->body;
$array = json_decode($jsonString);
extract the json with DOMDocument, and parse it with json_decode
$domd=#DOMDocument::loadHTML($response);
$json_data=json_decode(trim($domd->getElementsByTagName("body")->item(0)->textContent));
now the stuff in the json can be accessed like $UserID=$json_data->UserID;, ... and the stuff in the HTML can be accessed in $domd, like $loginResponse=$domd->getElementsByTagName("LoginResponse")->item(0)->textContent; - didn't see anything useful in the html other than the json, though..
Related
I'm trying to use simplexml_load_string to get the Status and Text values from the following XML response;
<?xml version="1.0" encoding="utf-16"?>
<cXML payloadID="online" xml:lang="en" timestamp="2017-12-04T15:57:47.6693296+00:00">
<Response>
<Status code="402" text="
product 325552not in customer[20690] pricelist" />
</Response>
</cXML>
In my PHP code I am getting the XML above from $reply:
$reply = curl_exec($curl);
I am then using simplexml_load_string like so:
$responseData = simplexml_load_string($reply);
echo 'Sync Order - '. $order->getIncrementId() . ' Status '. $responseData->Response->Status['code'] .' - '. $responseData->Response->Status['text'];
But this doesn't seem to get the code and text from the XML response above. Wondering if anyone has any ideas to help?
Thank you.
Note: The cXML is correct.
I've tried it with the added xml header and UTF-16 encoding bit and it fails to load with an error...
PHP Warning: simplexml_load_string(): Entity: line 1: parser error :
Document labelled UTF-16 but has UTF-8 content in
A simple but crude way round it is to change the UTF in the xml element to UTF8...
$reply = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $reply);
$responseData = simplexml_load_string($reply);
This then gives the output as expected...
Status 402 -
product 325552not in customer[20690] pricelist
I'm using a python script from with in php to call some xml date from a c# server. When I pass it through simplexml_load_string to parse my data. The python script is called for a few different things, but for one of the calls it gives me
Warning: simplexml_load_string(): Entity: line 1: parser error : StartTag: invalid element name
My PHP is:
$output = exec("python Auth.py -M authenticate -p 17df30b0-37c1-4ebf-b45e-39353bd971a9 -P 5b9abe609e1ab31555562dd959fc050d");
$xml = simplexml_load_string($output, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
My Python is:
params = urllib.urlencode(data);
conn = httplib.HTTPConnection("localhost", 8003);
conn.request("POST", path, params)
response = conn.getresponse()
print response.read();
And my XML is:
<?xml version="1.0"?>
<ServerResponse>
<Result>Success</Result>
<Token>25027fc3-e3c9-46a8-a1b7-5d217fecfe2c</Token>
</ServerResponse>
I can run my Python script from my Ubuntu command line just fine and I wrote the output into a PHP file as XML data and it worked fine on parsing it.
So I don't know what's wrong. Can anybody help?
You are only capturing the last line of the Python print when assigning exec() to a variable. And this last line is not sufficient to create a SimpleXMLElement with simplexml_load_string.
Instead, pass the output stream into an array using the additional output and return_var arguments of PHP's exec(). Once you receive the $output array, iterate through it to build an XML string:
exec("python Auth.py -M authenticate -p 17df30b0-37c1-4ebf-b45e-39353bd971a9" .
" -P 5b9abe609e1ab31555562dd959fc050d", $output, $return_var);
$strXML = '';
foreach ($output as $line) {
$strXML = $strXML . $line;
}
$xml = simplexml_load_string($strXML, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
This question already has answers here:
XML Parser Error Start Tag Expected
(2 answers)
Closed 7 years ago.
I am using below code for converting xml response to php string but I am getting below error
"Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found....."
"Warning: simplexml_load_string(): example.com...."
"Warning: simplexml_load_string(): ^ in /home/example/public_html/example.php on line 9"
<?php
echo '<?xml version="1.0" encoding="utf-8" ?>';
$url= "example.com";
$xml = simplexml_load_string($url);
echo $xml->status;
?>
Well, simplexml_load_string expects an XML string to be passed as parameter, which is not the case of your code: the content of $url variable is not a valid XML string, thus, you get those errors.
You may want to load the $url as a file instead. Example:
$xml = simplexml_load_file($url);
Final code:
<?php
echo '<?xml version="1.0" encoding="utf-8" ?>';
$url ="example.com";
$xml = simplexml_load_file($url);
echo $xml->status;
I’m setting up a PHP site which will gather information from a Dell iDRAC. I want to use the returned information to create a PHP object. The information returned from the first part of the script looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope" xmlnwsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlnwsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlnn1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView" xmlnxsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</To>
<Action>http://schemas.xmlsoap.org/ws/2004/09/enumeration/PullResponse</Action>
<RelatesTo>uuid:e5ef952f-fb48-1b48-8003-06c7395d1500</RelatesTo>
<MessageID>uuid:36a3a786-fb4c-1b4c-8012-fc140555dbe0</MessageID>
</Header>
<Body>
<PullResponse>
<Items>
<DCIM_SystemView>
<AssetTag/>
<BIOSReleaseDate>01/20/2014</BIOSReleaseDate>
<BIOSVersionString>2.1.2</BIOSVersionString>
<BaseBoardChassisSlot>NA</BaseBoardChassisSlot>
<BatteryRollupStatus>1</BatteryRollupStatus>
<BladeGeometry>255</BladeGeometry>
<BoardPartNumber>03015MA01</BoardPartNumber>
<BoardSerialNumber>CN7475128I0205</BoardSerialNumber>
<CPLDVersion>1.0.0</CPLDVersion>
<CPURollupStatus>1</CPURollupStatus>
<ChassisModel/>
<ChassisName>Main System Chassis</ChassisName>
<ChassisServiceTag>5P5KMW1</ChassisServiceTag>
<ChassisSystemHeight>5</ChassisSystemHeight>
<DeviceDescription>System</DeviceDescription>
<ExpressServiceCode>12404926945</ExpressServiceCode>
<FQDD>System.Embedded.1</FQDD>
<FanRollupStatus>1</FanRollupStatus>
<HostName/>
<InstanceID>System.Embedded.1</InstanceID>
<LastSystemInventoryTime>20140608040932.000000+000</LastSystemInventoryTime>
<LastUpdateTime>20140522204842.000000+000</LastUpdateTime>
<LicensingRollupStatus>1</LicensingRollupStatus>
<LifecycleControllerVersion>2.1.0</LifecycleControllerVersion>
<Manufacturer>Dell Inc.</Manufacturer>
<MaxCPUSockets>2</MaxCPUSockets>
<MaxDIMMSlots>12</MaxDIMMSlots>
<MaxPCIeSlots>6</MaxPCIeSlots>
<MemoryOperationMode>OptimizerMode</MemoryOperationMode>
<Model>PowerEdge T420</Model>
<NodeID>5P5KMW1</NodeID>
<PSRollupStatus>1</PSRollupStatus>
<PlatformGUID>31574d4f-c0b5-4b80-3510-00504c4c4544</PlatformGUID>
<PopulatedCPUSockets>2</PopulatedCPUSockets>
<PopulatedDIMMSlots>4</PopulatedDIMMSlots>
<PopulatedPCIeSlots>1</PopulatedPCIeSlots>
<PowerCap>317</PowerCap>
<PowerCapEnabledState>3</PowerCapEnabledState>
<PowerState>2</PowerState>
<PrimaryStatus>1</PrimaryStatus>
<RollupStatus>1</RollupStatus>
<ServiceTag>5P5KMW1</ServiceTag>
<StorageRollupStatus>1</StorageRollupStatus>
<SysMemErrorMethodology>6</SysMemErrorMethodology>
<SysMemFailOverState>NotInUse</SysMemFailOverState>
<SysMemLocation>3</SysMemLocation>
<SysMemMaxCapacitySize>393216</SysMemMaxCapacitySize>
<SysMemPrimaryStatus>1</SysMemPrimaryStatus>
<SysMemTotalSize>16384</SysMemTotalSize>
<SystemGeneration>12G Monolithic</SystemGeneration>
<SystemID>1273</SystemID>
<SystemRevision>0</SystemRevision>
<TempRollupStatus>1</TempRollupStatus>
<UUID>4c4c4544-0050-3510-804b-b5c04f4d5731</UUID>
<VoltRollupStatus>1</VoltRollupStatus>
<smbiosGUID>44454c4c-5000-1035-804b-b5c04f4d5731</smbiosGUID>
</DCIM_SystemView>
</Items>
<EndOfSequence/>
</PullResponse>
</Body>
</Envelope>
When I try to use the simplexml_load_string function, it returns the following errors and does not process the data as a string.
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the document in php shell code on line 1
PHP Warning: simplexml_load_string(): in php shell code on line 1
PHP Warning: simplexml_load_string(): ^ in php shell code on line 1
However, if I use the EXACT same XML and manually create the variable like this :
<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope" xmlnwsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlnwsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlnn1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView" xmlnxsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</To>
<Action>http://schemas.xmlsoap.org/ws/2004/09/enumeration/PullResponse</Action>
<RelatesTo>uuid:641403a8-fb4a-1b4a-8003-06c7395d1500</RelatesTo>
<MessageID>uuid:b4caac95-fb4d-1b4d-8051-fc140555dbe0</MessageID>
</Header>
<Body>
<PullResponse>
<Items>
<DCIM_SystemView>
<AssetTag/>
<BIOSReleaseDate>01/20/2014</BIOSReleaseDate>
<BIOSVersionString>2.1.2</BIOSVersionString>
<BaseBoardChassisSlot>NA</BaseBoardChassisSlot>
<BatteryRollupStatus>1</BatteryRollupStatus>
<BladeGeometry>255</BladeGeometry>
<BoardPartNumber>03015MA01</BoardPartNumber>
<BoardSerialNumber>CN7475128I0205</BoardSerialNumber>
<CPLDVersion>1.0.0</CPLDVersion>
<CPURollupStatus>1</CPURollupStatus>
<ChassisModel/>
<ChassisName>Main System Chassis</ChassisName>
<ChassisServiceTag>5P5KMW1</ChassisServiceTag>
<ChassisSystemHeight>5</ChassisSystemHeight>
<DeviceDescription>System</DeviceDescription>
<ExpressServiceCode>12404926945</ExpressServiceCode>
<FQDD>System.Embedded.1</FQDD>
<FanRollupStatus>1</FanRollupStatus>
<HostName/>
<InstanceID>System.Embedded.1</InstanceID>
<LastSystemInventoryTime>20140608040932.000000+000</LastSystemInventoryTime>
<LastUpdateTime>20140522204842.000000+000</LastUpdateTime>
<LicensingRollupStatus>1</LicensingRollupStatus>
<LifecycleControllerVersion>2.1.0</LifecycleControllerVersion>
<Manufacturer>Dell Inc.</Manufacturer>
<MaxCPUSockets>2</MaxCPUSockets>
<MaxDIMMSlots>12</MaxDIMMSlots>
<MaxPCIeSlots>6</MaxPCIeSlots>
<MemoryOperationMode>OptimizerMode</MemoryOperationMode>
<Model>PowerEdge T420</Model>
<NodeID>5P5KMW1</NodeID>
<PSRollupStatus>1</PSRollupStatus>
<PlatformGUID>31574d4f-c0b5-4b80-3510-00504c4c4544</PlatformGUID>
<PopulatedCPUSockets>2</PopulatedCPUSockets>
<PopulatedDIMMSlots>4</PopulatedDIMMSlots>
<PopulatedPCIeSlots>1</PopulatedPCIeSlots>
<PowerCap>317</PowerCap>
<PowerCapEnabledState>3</PowerCapEnabledState>
<PowerState>2</PowerState>
<PrimaryStatus>1</PrimaryStatus>
<RollupStatus>1</RollupStatus>
<ServiceTag>5P5KMW1</ServiceTag>
<StorageRollupStatus>1</StorageRollupStatus>
<SysMemErrorMethodology>6</SysMemErrorMethodology>
<SysMemFailOverState>NotInUse</SysMemFailOverState>
<SysMemLocation>3</SysMemLocation>
<SysMemMaxCapacitySize>393216</SysMemMaxCapacitySize>
<SysMemPrimaryStatus>1</SysMemPrimaryStatus>
<SysMemTotalSize>16384</SysMemTotalSize>
<SystemGeneration>12G Monolithic</SystemGeneration>
<SystemID>1273</SystemID>
<SystemRevision>0</SystemRevision>
<TempRollupStatus>1</TempRollupStatus>
<UUID>4c4c4544-0050-3510-804b-b5c04f4d5731</UUID>
<VoltRollupStatus>1</VoltRollupStatus>
<smbiosGUID>44454c4c-5000-1035-804b-b5c04f4d5731</smbiosGUID>
</DCIM_SystemView>
</Items>
<EndOfSequence/>
</PullResponse>
</Body>
</Envelope>
XML;
It works like a charm. So, my question is simple. How can I tell PHP to process the string the same way it processes the XML like it does if the XML identifier is used. I have tried to reprocess the string like this:
$new_string = <<<XML
$string
XML;
But no go. Any other ideas?
Are you using file_get_contents to actually load the XML file into a string before using simplexml_load_string which—as the name states—loads XML from a string?
$xml_file = file_get_contents('test.xml');
$xml = simplexml_load_string($xml_file);
echo '<pre>';
print_r($xml);
echo '</pre>';
And the output is good when I use the XML from your post:
SimpleXMLElement Object
(
[#attributes] => Array
(
[xmlnwsa] => http://schemas.xmlsoap.org/ws/2004/08/addressing
[xmlnwsen] => http://schemas.xmlsoap.org/ws/2004/09/enumeration
[xmlnn1] => http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView
[xmlnxsi] => http://www.w3.org/2001/XMLSchema-instance
)
And so on…
And so on…
And so on…
But that said, I can recreate your error exactly if I add a space or line to the beginning of the XML file like this; note the one simple space before <?xml version="1.0" encoding="UTF-8"?>:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope" xmlnwsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlnwsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlnn1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView" xmlnxsi="http://www.w3.org/2001/XMLSchema-instance">
And so on…
And so on…
And so on…
And here is my error:
Warning: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the document in /Applications/MAMP/htdocs/test.php on line 5
Warning: simplexml_load_string(): in /Applications/MAMP/htdocs/test.php on line 5
Warning: simplexml_load_string(): ^ in /Applications/MAMP/htdocs/test.php on line 5
So hey! I know your pain!
Anyway, the quick solution I tried is to use trim on the $xml_file to get rid of extraneous white space at the beginning & end of the of the file like this:
$xml_file = file_get_contents('test.xml');
$xml = simplexml_load_string(trim($xml_file));
echo '<pre>';
print_r($xml);
echo '</pre>';
And all works great!
function retrieveProfile()
{
$url = "http://steamcommunity.com/profiles/{$this->steamID64}/?xml=1";
$profileData = simplexml_load_string($url);
if(!empty($profileData->error)) { return NULL; }
$this->friendlyName = (string) $profileData->steamID;
}
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found
Warning: simplexml_load_string() [function.simplexml-load-string]: http://steamcommunity.com/profiles/76561197991676121/?xml=1
Warning: simplexml_load_string() [function.simplexml-load-string]: ^
This is the XML file:
http://steamcommunity.com/profiles/76561197991676121/?xml=1
I got no ideea why it does not work and yes it have <?xml version="1.0" encoding="UTF-8" standalone="yes"?> header !
Tried $profileData = new SimpleXMLElement(file_get_contents($url)) too but I get the same result.
Why is this happening ? How can I solve it? I am searching for 3 hours and see only answers that won't help me .
Errors : http://img824.imageshack.us/img824/9464/errorszz.png
EDIT1 : simplexml_load_string was one of my mistakes but now I get even more errors with simplexml_load_file - Tells me that the document is empty... (Not true !)
You are loading the URL as your XML source. You should have:
$profileData = simplexml_load_file($url);
Url was changing from steamcommunity.com/profiles/76561197991676121/?xml=1 to http://steamcommunity.com/id/virusbogdan/?xml=1 .
Obviously the first link returns null so there was an error. Problem solved !