Search code examples
phpxmlsimplexml

How to get data from an phrase in remote XML in php?


I wanna get the data from an XML file on remote site from a particular node. But im getting the following error

Warning: simplexml_load_file(): in php line

on the warning 2 : Its loading the File data. Result I wanna is to get the GRate.

Note: I have enabled SimpleXML module on my php installation.


<?php
  $url = "http://api.srinivasajewellery.com/getrate/getrate";
  $xml = simplexml_load_file($url) or die("not open");

  ?><pre><?php //print_r($xml); ?></pre><?php

  foreach($xml->GRate as $GRate){
    printf('$GRate');
  }
?>

I have expected to get "3640.00" on my output but error is as follows

Warning: simplexml_load_file(): http://api.srinivasajewellery.com/getrate/getrate:1: parser error : Start tag expected, '<' not found in H:\root\home\srinivasauser-001\www\goldrate\wp-content\themes\twentynineteen\footer.php on line 24

Warning: simplexml_load_file(): {"GRate":"3640.00","SRate":"49.00","PRate":"0.00"} in H:\root\home\srinivasauser-001\www\goldrate\wp-content\themes\twentynineteen\footer.php on line 24

Warning: simplexml_load_file(): ^ in H:\root\home\srinivasauser-001\www\goldrate\wp-content\themes\twentynineteen\footer.php on line 24 not open.


Solution

  • When the URL "http://api.srinivasajewellery.com/getrate/getrate" is requested from PHP with the default settings, it will return the data as JSON. Which might be even easier to parse in this case:

    <?php
    
    $url = "http://api.srinivasajewellery.com/getrate/getrate";
    $json = json_decode(file_get_contents($url));
    
    echo '$GRate: ' . $json->GRate, "\n";
    

    Output:

    $GRate: 3670.00
    

    This can be easily checked by fetching the URL and output it verbatim:

    $buffer = file_get_contents($url);
    echo $buffer, "\n";
    
    {"GRate":"3670.00","SRate":"50.00","PRate":"0.00"}
    

    As has been demonstrated by Vijay Dohare it is possible to tell that server that XML is preferred. To check if that works, is possible this way, too:

    stream_context_get_default(['http' => ['header' => 'Accept: application/xml']]);
    $buffer = file_get_contents($url);
    echo $buffer, "\n";
    

    The output is not that beautified then (I guess if the data is more, the JSON wouldn't be that easy to read as well as it also would grew larger):

    <GetRateController.Rate xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Savings.Controllers"><GRate>3670.00</GRate><PRate>0.00</PRate><SRate>50.00</SRate></GetRateController.Rate>
    

    This might be similar to when opening the URL in the browser. This is because the browser also sends the Accept request header and it contains XML as well:

    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
    

    Because browser normally accept XML as well (albeit they prefer HTML over it).

    So in the end it depends what you prefer. Either the JSON which is less verbose compared to XML (see the very first example code above) or if you want to use XML with SimpleXML:

    <?php
    
    $url = "http://api.srinivasajewellery.com/getrate/getrate";
    stream_context_get_default(['http' => ['header' => 'Accept: application/xml']]);
    $xml = simplexml_load_file($url) or die("not open");
    
    echo '$GRate: ' . $xml->GRate, "\n";
    

    Output:

    $GRate: 3670.00