Get Json Value within XML - PHP - php

Say I have the following php:
$Url = sprintf( "http://www.wowhead.com/item=%u?xml", $EntryId );
$Xml = file_get_contents( $Url );
//echo htmlentities($Xml, ENT_COMPAT, 'UTF-8');
$Xml = simplexml_load_string( $Xml);
and lets say this is the xml:
http://www.wowhead.com/item=31065?xml
I know if T want say DisplayID I can do:
$DisplayId = $Xml->item->icon["displayId"];
However I want to get the values within the <json> part of the file.
<json>
<![CDATA[
"armor":464,"classs":4,"displayid":117596,"id":31064,"level":146,"name":"3Hood of Absolution","reqclass":16,"reqlevel":70,"slot":1,"slotbak":1,"source":[5],"sourcemore": [{"n":"Tydormu","t":1,"ti":23381}],"specs":[258],"subclass":1
]]>
</json>
I want to get the slotbak value, but I'm unsure how to do it. I did echo htmlentities($Xml, ENT_COMPAT, 'UTF-8'); to ensure I'm getting the xml file fine, and I am. But when I use json_decode or json_encode it tends to just either return { } { } { } or simply the object and not the value.
Does anyone know how I can do this?

You need to remove the <![CDATA[ and ]]> for json_decode to work.
Try this:
$json = $Xml->item->json;
$cleanedJson = str_ireplace(array('<![CDATA[', ']]>'), array('{', '}'), $json);
$jsonObject = json_decode($cleanedJson);

Related

Cannot parse XML from a URL using file_get_contents() in PHP

I'm trying to pull information from the following URL (https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL) using PHP, but for some reason I keep getting no information back.
After searching around a bit, I ended up with
<?php
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=1&stationString=KORL';
$xml = json_decode(file_get_contents($url));
echo $xml;
?>
EDIT: above code is obviously wrong... my updated (and still wrong) code is below.
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL';
$xml = simplexml_load_file($url) or die("feed not loading");
$string_data = $xml;
$xmlstr = simplexml_load_string($string_data);
$data = (string) $xmlstr->data->METAR->raw_text;
echo $data;
The information I need to get from this is <raw_text>.
Any help is greatly appreciated here!
No need to simplexml_load_string() when you simplexml_load_file(). Simply load the XML and access it like you used to:
<?php
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL';
$xml = simplexml_load_file($url) or die("feed not loading");
$data = (string) $xml->data->METAR->raw_text;
echo $data;
will output:
KORL 191653Z 08019G24KT 10SM SCT039 BKN085 29/18 A3018 RMK AO2 RAB17E26 SLP222 P0000 T02890183

Text decoding in PHP with HTMLEntities and Mandarin?

I have the following string in an XLF file:
<h2><a href=â€/SOMEURLâ€>尋找é©åˆæ
If I apply html_entity_decode, I end up with:
<a href=â€/SOMEURLâ€>å°‹æ‰
It correctly dealt with:
< .... <
> .... >
But the problem is with the:
â
This should be:
"
So what must I do to get the correctly encoded string back when reading from the file?
MORE BACKGROUND INFO
This is how i am converting the XLF file into a PHP array:
function website_export_xml_to_array($data) {
$xml = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$xmlArray = json_decode($json,TRUE);
return $xmlArray;
}
$xml = file_get_content("myxmlfile.xlf");
$xml = website_export_xml_to_array($xml);
Perhaps something is going wrong at this point already?

base64_encode acts strange

I'm trying to base64_encode and decode an XML request:
$xml = '<root>
<term id="KEY">VAL</term>
<term id="KEY2">VAL2</term>
<term id="KEY3">VAL3</term>
</root>';
echo base64_encode( $xml );
echo base64_decode( $xml );
Why does this return this strange stuff on decode?
®Š-µêæ‰Ò„aPþ׫š×«š'J•½¿µêæµêæ‰Ò„cu#/íz¹¿®Š-
You are trying to decode the original string of XML, not the base64 encoded string.
$xml = '<root>
<term id="KEY">VAL</term>
<term id="KEY2">VAL2</term>
<term id="KEY3">VAL3</term>
</root>';
$encoded_xml = base64_encode( $xml );
echo $encoded_xml;
echo base64_decode( $encoded_xml );
You're trying to decode the original XML string, without having saved the encoded version.
Try
$xml = '...';
$encoded = base64_encode($xml);
$decoded = base64_decode($encoded);
^^^^^^^^
It's because you are trying to encode the XML and not the actual encoded string.
Because you should decode something that is already encoded, and $xml is not.

PHP and XML answer from server

Hello i have this part of php code
$google_url = "http://uclassify.com/browse/uClassify/Sentiment/ClassifyText?readkey=jnsdnjdsnjnjsdnjsnjdsd&text=".$text."&version=1.01";
$result = file_get_contents($google_url);
$obj = simplexml_load_string($result);
$zaab = toArray($obj);
echo($zaab);
and the answer i get is
<?xml version="1.0" encoding="UTF-8" ?>
<uclassify xmlns="http://api.uclassify.com/1/ResponseSchema" version="1.01">
<status success="true" statusCode="2000"/>
<readCalls>
<classify id="cls1">
<classification textCoverage="0.6">
<class className="negative" p="0.999984"/> <-----p_value
<class className="positive" p="1.60692e-005"/> <-----p_value
</classification>
</classify>
</readCalls>
</uclassify>
how can i access p_values?
no after updating it gives me an error having to do with toArray()
You can get the all *p*s attribute values by looping through all *class*es like this:
$classes = $obj->readCalls->classify->classification->class;
foreach ($classes as $class) {
$p_val = $class->attributes()->p;
echo $p_val . "<br />";
}
I see you also tried to convert the xml string to array, probably the simplest way to do it is:
$json = json_encode($obj);
$xml_array = json_decode($json,TRUE);
To see the results use:
echo "<pre>";
print_r($xml_array);
echo "</pre>";
Use a xml parser.
http://php.net/manual/en/book.xml.php

Can not parse xml File with Html Umlauts

I parse an xml file with this code:
$file = file_get_contents('test.xml');
$xml = $file;
echo '<pre>';
$xml = htmlentities_decode ($xml);
print_r (simplexml_load_string($xml));
function htmlentities_decode( $string ){
$trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
$trans = array_flip($trans);
return strtr($string, $trans);
}
My xml File has Umlauts like this decoded: &amul; or ß.
How do I have to decode/encode my output, that I have to decode/encode them, that they are shown in the same way like above? ( &amul; or ß).
Simple xml can not read them directly, so I have to decode them first, that simple xml can work with it.
Afterwards (after the pasring) I want to save the as utf8 to the database.
What is the best way, to do that?

Categories