base64_encode acts strange - php

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.

Related

Converting External XML feed to JSON

I am trying to convert an xml feed to json, but using this code it fails to do anything. Any ideas?
<?php
$xml_string = 'https://xml.betfred.com/Horse-Racing-Daily.xml';
//read the XML file
$xml = file_get_contents($xml_string);
//encode the formatted data
$json = json_encode();
//generate the JSON file
header('Content-Type: application/json');
echo $json;
?>
You forgot to put $xml in the json_encode() method as parameter. Adjust your code like this:
$json = json_encode($xml);
this will however only convert the $xml string to json and that is probably not what you want.
To get close to what you are trying to do you could do:
$simpleXml = simplexml_load_string($xml);
$json = json_encode($simpleXml);
Your json might contain alot of junk, but maybe it will be enough for your needs.

PHP: XML to JSON fails to convert

I am using the following function to convert XML to Json:
function XMLtoJSON($xml) {
$xml = file_get_contents($xml); // gets XML content from file
$xml = str_replace(array("\n", "\r", "\t"), '', $xml); // removes newlines, returns and tabs
// replace double quotes with single quotes, to ensure the simple XML function can parse the XML
$xml = trim(str_replace('"', "'", $xml));
$simpleXml = simplexml_load_string($xml);
return stripslashes(json_encode($simpleXml)); // returns a string with JSON object
}
I am trying to convert the following XML file:
https://www.comdinheiro.com.br/temp/COMDINHEIRO_GabrielVilella1146066400.xml
So my code is:
$myVar1 = XMLtoJSON("https://www.comdinheiro.com.br/temp/COMDINHEIRO_GabrielVilella1146066400.xml");
var_dump($myVar1);
Here is the var_dump result:
string(455) "{"controle":{"data_hora":"2017-05-12 20:26:47","usuario":"GabrielVilella","ferramenta":"ExtratoCarteira016"},"variaveis":{"nome_portfolio":"ApelidoTodos","data_ini":"2015-05-05","data_fim":"2016-08-12","cas_dec_q":"2","cas_dec_v":"2","discrimina_fundos":"0","discrimina_fundos_cvm":"0","discrimina_ativos":"1","discrimina_cadgeral":"0","data_fim_agenda":"2017-06-05","filtro_eventos":"todos","filtro_tipo_ativo":"todos","flag_export":"xml"},"resposta":{}}"
The conversion works properly. However it fails to fill the elements of "resposta":{}. No error message is shown. So I ask: Why "resposta":{} is empty?

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?

Get Json Value within XML - 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);

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