Cannot read the xml file using simplexml_load_file (); - php

I am having a problem reading data from an online xml file, when I use the simplexml_load_file () function; I encountered an error.
My code PHP:
$url = "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet";
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml);
echo '</pre>';
input eror
Warning: simplexml_load_file(https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in D:\All In One\xammp\htdocs\ty_gia_api\index.php on line 12
Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet" in D:\All In One\xammp\htdocs\ty_gia_api\index.php on line 12

Try to get contents from URL like:
$url = "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet";
$xml = simplexml_load_string(shell_exec("curl -k '$url'"));
echo '<pre>';
print_r($xml);
echo '</pre>';
I see the page you try to get is not XML, its JSON.
This will work:
$url = 'https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet';
$data = json_decode(shell_exec("curl -k '$url'"), 1);
echo '<pre>';
print_r($data);
echo '</pre>';
var_dump($data);

Related

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.
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
Try following code,
<?php
$url = "http://api.srinivasajewellery.com/getrate/getrate";
$context = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml) or die("not open");
foreach($xml->GRate as $GRate){
echo '$GRate: '.$GRate;
}
?>

I am trying to get user country of location from geoplugin.net but i am getting an error

The Error I get is:
Warning:
file_get_contents(http://maps.google.com/maps/api/geocode/json?address=&sensor=false)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.0 400 Bad Request in
D:\xampp\htdocs\SwaziTour\index.php on line 57
my code is as below:
echo var_export( unserialize( file_get_contents( 'http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'] ) ) );
Don't use geoplugin.net. Try this:
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
print_r($details);
Did you even try to open your link in browser? If you open your url you'll see that you get an error because you don't have an address.

How to get a json service in php

This is the following code i am using
<?php
$contents= file_get_contents('http://skillpage.enigmateleco.net/rest/login.php?email=sri.sapna14#gmail.com&pass=12345');
$contents = utf8_encode($contents);
$contents_array = json_decode($contents,1);
?>
code is showing error
Warning: file_get_contents(http://...#gmail.com&pass=12345): failed to
open stream: HTTP request failed! HTTP/1.1 403 ModSecurity Action in
C:\xampp\htdocs\skillbook\json1.php on line 9
try to use this : $data = json_decode($request,true); this work only if your request is on JSON format

file_get_contents() error: "Failed to open stream: HTTP request failed! HTTP/1.1 403 ModSecurity Action" [duplicate]

This question already has answers here:
file_get_contents returns 403 forbidden
(11 answers)
Closed 3 months ago.
I'm trying to get the value from this following JSON array in a PHP variable.
This is a var_dump of the array:
array(1) { [0]=> object(stdClass)#1 (1) { ["row"]=> object(stdClass)#2 (1) { ["u_fname"]=> string(6) "Ashish" } } }
This is my code of the page
<?php
$contents = file_get_contents('http://localhost/skillbook/json.php');
$contents = utf8_encode($contents);
$results =var_dump(json_decode($contents));
?>
However when I tried to get a service from online the file_get_contents function is not working.
file_get_contents(http://...#gmail.com&pass=12345): failed to open stream: HTTP request failed! HTTP/1.1 403 ModSecurity Action in C:\xampp\htdocs\skillbook\json1.php
$contents = file_get_contents('http://localhost/skillbook/json.php');
$contents = utf8_encode($contents);
$contents_array = json_decode($contents,1);
$foundvalue= $contents_array[0]["row"]["u_fname"];
echo $foundvalue;
OutPut : Ashish
i think you can use foreach condition for json
$json = file_get_contents('http://localhost/skillbook/json.php');
$json = utf8_encode($json);
$data = json_decode($json,true);
// change the variable name to devices which is clearer.
$devices = $data['0'];
foreach ($devices as $device)
{
echo $device["u_fname"];
}

Parsing of a XML response

I am completely new to XML.
I have a URL which gives me a XML response. I am facing problems in parsing the response and showing only the details that I want. Currently the url is "https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55504&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_US&currencyCode=USD&xml=%3CHotelListRequest%3E%3Ccity%3ESeattle%3C%2Fcity%3E%3CstateProvinceCode%3EWA%3C%2FstateProvinceCode%3E%3CcountryCode%3EUS%3C%2FcountryCode%3E%3CarrivalDate%3E%3C%2FarrivalDate%3E%3CdepartureDate%3E9%2F1%2F2014%3C%2FdepartureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E2%3C%2FnumberOfAdults%3E%3C%2FRoom%3E%3C%2FRoomGroup%3E%3CnumberOfResults%3E25%3C%2FnumberOfResults%3E%3C%2FHotelListRequest%3E"
and I am using the following code:
$feed = file_get_contents($url);
$items = simplexml_load_string($feed);
print_r($items);
and this was the error
file_get_contents( https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55504&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_US&currencyCode=USD&xml=%3CHotelListRequest%3E%3Ccity%3EAmsterdam%3C%2Fcity%3E%3CarrivalDate%3E08/30/2014%3C%2FarrivalDate%3E%3CdepartureDate%3E08/31/2014%3C%2FdepartureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E1%3C%2FnumberOfAdults%3E%3C%2FRoom%3E%3C%2FRoomGroup%3E%3CnumberOfResults%3E25%3C%2FnumberOfResults%3E%3C%2FHotelListRequest%3E): failed to open stream: Invalid argument
I have also tried using simplexml_load_file() and it's showing the same error.
file_get_contents on the URL is returning content of type json, that's why simple_xml_load_file cannot parse it. Use this
$feed = file_get_contents("https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55504&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_US&currencyCode=USD&xml=%3CHotelListRequest%3E%3Ccity%3ESeattle%3C%2Fcity%3E%3CstateProvinceCode%3EWA%3C%2FstateProvinceCode%3E%3CcountryCode%3EUS%3C%2FcountryCode%3E%3CarrivalDate%3E%3C%2FarrivalDate%3E%3CdepartureDate%3E9%2F1%2F2014%3C%2FdepartureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E2%3C%2FnumberOfAdults%3E%3C%2FRoom%3E%3C%2FRoomGroup%3E%3CnumberOfResults%3E25%3C%2FnumberOfResults%3E%3C%2FHotelListRequest%3E");
//echo $feed;
$json = json_decode($feed);
print_r($json);
Currently when viewed on the browser its an xml but in the script is responds a json.
$url = 'https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55504&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_US&currencyCode=USD&xml=%3CHotelListRequest%3E%3Ccity%3ESeattle%3C%2Fcity%3E%3CstateProvinceCode%3EWA%3C%2FstateProvinceCode%3E%3CcountryCode%3EUS%3C%2FcountryCode%3E%3CarrivalDate%3E%3C%2FarrivalDate%3E%3CdepartureDate%3E9%2F1%2F2014%3C%2FdepartureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E2%3C%2FnumberOfAdults%3E%3C%2FRoom%3E%3C%2FRoomGroup%3E%3CnumberOfResults%3E25%3C%2FnumberOfResults%3E%3C%2FHotelListRequest%3E';
$contents = file_get_contents($url);
$data = json_decode($contents, true);
// becomes an array

Categories