Reputation: 161
I'm trying to read this URL:
http://www.anterior.banxico.org.mx/rsscb/rss?BMXC_canal=fix&BMXC_idioma=es
With this XML structure
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:cb="http://staging.bis.org/rss-cb/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3c.org/1999/02/22-rdf-syntax-ns#rdf.xsd">
<channel rdf:about="http://www.banxico.org.mx/rsscb/rss?canal=tipCam&idioma=es">...</channel>
<item rdf:about="http://www.banxico.org.mx/portal-mercado-cambiario/index.html/20190212">
<title>
<![CDATA[ MX: 19.2592 MXN = 1 USD 2019-02-12 BM FIX ]]>
</title>
<link>
http://www.banxico.org.mx/portal-mercado-cambiario/index.html#FIX
</link>
<description>
<![CDATA[
Este tipo de cambio es determinado por el Banco de México los días hábiles bancarios con base en un promedio de las cotizaciones del mercado de cambios al mayoreo para operaciones liquidables el segundo día hábil bancario siguiente.
]]>
</description>
<dc:date>2019-02-12T12:01:34-06:00</dc:date>
<dc:language>es</dc:language>
<dc:format>text/html</dc:format>
<dc:creator>Banco de México</dc:creator>
<cb:simpletitle>FIX</cb:simpletitle>
<cb:statistics>
<cb:country>MX</cb:country>
<cb:institutionAbbrev>BM</cb:institutionAbbrev>
<cb:exchangeRate>
<cb:value frequency="daily business" decimals="4">19.2592</cb:value>
<cb:baseCurrency>USD</cb:baseCurrency>
<cb:targetCurrency>MXN</cb:targetCurrency>
<cb:rateName>FIX</cb:rateName>
</cb:exchangeRate>
</cb:statistics>
</item>
</rdf:RDF>
I'm using the following code:
$url = "http://www.anterior.banxico.org.mx/rsscb/rss?BMXC_canal=fix&BMXC_idioma=es";
$xml = simplexml_load_file($url);
$item = $xml->item;
print_r($item);
I'm getting this result:
SimpleXMLElement Object
(
[title] => SimpleXMLElement Object
(
)
[link] => http://www.banxico.org.mx/portal-mercado-cambiario/index.html#FIX
[description] => SimpleXMLElement Object
(
)
)
It's not getting the past <dc:date>
:
I need to get to <cb:value frequency="daily business" decimals="4">19.2592</cb:value>
.
So I can get the 19.2592 value.
What I'm doing wrong?
Upvotes: 1
Views: 97
Reputation: 1
You can also register a prefix and namespace URI if you can use DOMXPath ...
Upvotes: 0
Reputation: 57121
You can use XPath to find the element your after without having to search through each node...
$values = $item->xpath("//cb:value[@frequency=\"daily business\"]");
echo (string)$values[0];
The XPath looks for a <value>
element in the cb
namespace (so <cd:value>
) it also looks for the frequency
attribute with a value of "daily business"
.
The xpath()
call will return a list of matching nodes, so use [0]
to take the first element and use (string)
to force it to be a string (or you could use (float)
if you want to use it for calculations).
Upvotes: 1