Need Help in Getting Webpage Content

hello,
I’m trying to get INR currency USD rates from a Webpage.
my Code is
<?php $url = 'https://api.exchangerate-api.com/v4/latest/USD'; $lines_array=file($url); $lines_string=implode('',$lines_array); echo $lines_string; ?>

and output of this is

{"provider":"https://www.exchangerate-api.com","WARNING_UPGRADE_TO_V6":"https://www.exchangerate-api.com/docs/free","terms":"https://www.exchangerate-api.com/terms","base":"USD","date":"2021-07-17","time_last_updated":1626480002,"rates":{"USD":1,"AED":3.67,"AFN":80.18,"ALL":103.69,"AMD":494.91,"ANG":1.79,"AOA":647.54,"ARS":96.25,"AUD":1.35,"AWG":1.79,"AZN":1.7,"BAM":1.65,"BBD":2,"BDT":84.81,"BGN":1.66,"BHD":0.376,"BIF":1970.43,"BMD":1,"BND":1.35,"BOB":6.89,"BRL":5.1,"BSD":1,"BTN":74.61,"BWP":11.01,"BYN":2.54,"BZD":2,"CAD":1.26,"CDF":1978.53,"CHF":0.919,"CLP":752.52,"CNY":6.47,"COP":3795.72,"CRC":619.32,"CUC":1,"CUP":25.75,"CVE":93.25,"CZK":21.63,"DJF":177.72,"DKK":6.31,"DOP":56.92,"DZD":134.62,"EGP":15.69,"ERN":15,"ETB":43.84,"EUR":0.846,"FJD":2.08,"FKP":0.724,"FOK":6.31,"GBP":0.724,"GEL":3.12,"GGP":0.724,"GHS":5.94,"GIP":0.724,"GMD":51.8,"GNF":9773.06,"GTQ":7.73,"GYD":208.38,"HKD":7.77,"HNL":23.92,"HRK":6.37,"HTG":93.5,"HUF":304.48,"IDR":14524.29,"ILS":3.29,"IMP":0.724,"INR":74.61,"IQD":1453.15,"IRR":41832.79,"ISK":123.56,"JMD":154.24,"JOD":0.709,"JPY":110.11,"KES":108.14,"KGS":84.42,"KHR":4054.24,"KID":1.35,"KMF":416.06,"KRW":1142.42,"KWD":0.3,"KYD":0.833,"KZT":426.63,"LAK":9482.03,"LBP":1507.5,"LKR":199.15,"LRD":171.8,"LSL":14.44,"LYD":4.49,"MAD":8.94,"MDL":18,"MGA":3885.8,"MKD":52.11,"MMK":1646.31,"MNT":2838.26,"MOP":8,"MRU":35.99,"MUR":42.87,"MVR":15.42,"MWK":807.46,"MXN":19.9,"MYR":4.21,"MZN":63.82,"NAD":14.44,"NGN":429.66,"NIO":35.19,"NOK":8.82,"NPR":119.38,"NZD":1.43,"OMR":0.384,"PAB":1,"PEN":3.94,"PGK":3.52,"PHP":50.34,"PKR":159.3,"PLN":3.88,"PYG":6819.15,"QAR":3.64,"RON":4.17,"RSD":99.44,"RUB":74.14,"RWF":1005.2,"SAR":3.75,"SBD":7.93,"SCR":14.29,"SDG":440.9,"SEK":8.67,"SGD":1.35,"SHP":0.724,"SLL":10210.27,"SOS":576.25,"SRD":21.13,"SSP":177.54,"STN":20.72,"SYP":1375.9,"SZL":14.44,"THB":32.77,"TJS":11.32,"TMT":3.5,"TND":2.78,"TOP":2.24,"TRY":8.54,"TTD":6.73,"TVD":1.35,"TWD":27.97,"TZS":2315.3,"UAH":27.25,"UGX":3543.85,"UYU":43.85,"UZS":10537.43,"VES":3513688.8,"VND":22981.75,"VUV":110.25,"WST":2.55,"XAF":554.75,"XCD":2.7,"XDR":0.702,"XOF":554.75,"XPF":100.92,"YER":250.94,"ZAR":14.44,"ZMW":22.62}}

now I only want to store the Value of INR in a Variable How can i Do that??

Welcome to our site! Next time place your code between the correct tags so that it is easier to read!

Well, it is warning you to update to Version6. You should look into that as V4 may not work in the future!
Now, change this section…

<?php 
$url = 'https://api.exchangerate-api.com/v4/latest/USD'; 
$lines_array=file($url); 
$lines_string=implode('',$lines_array); 
echo $lines_string; 
?>

To this type of version of code. I added comments to show how it works.

<?php
//  Get the live data
$url = 'https://api.exchangerate-api.com/v4/latest/USD'; 
$data = file_get_contents($url);
//  Separate the full data into lines
$data_lines = explode(",", $data);
//  Data is separated, now extract the indexes
$live_data = array();
foreach($data_lines as $one_line) {
	$temp = explode(":", $one_line);
	$live_data[$temp[0]] = $temp[1];
}
//  Display the entire table just for this example...  Remove for live code
$temp = print_r($live_data,1);
echo "<pre>".$temp."</pre>";
//  Display the needed data (Note single-quotes around the double-quotes! Important!)
echo $live_data['"INR"'];
?>

Please note that this code includes a display so you can review the entire output. It is not needed of course for your live code. But, it shows how the output has been transformed. Hope this helps!

Sponsor our Newsletter | Privacy Policy | Terms of Service