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.
Related
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
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?
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?
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.
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?