I am using currencylayer JSON API to get realtime currency conversion value - does anybody know how I could grab both the "result" value and the "quote" value from the API response below using PHP?
I am new to PHP, and I am wondering if it's possible to store it in a variable.
This is the JSON:
{
"success":true,
"terms":"https:\/\/currencylayer.com\/terms",
"privacy":"https:\/\/currencylayer.com\/privacy",
"query":{
"from":"CAD",
"to":"GBP",
"amount":234
},
"info":{
"timestamp":1432829168,
"quote":0.524191
},
"result":122.660694
}
I have played around with file_get_contents("URL") but I didnt understand how to get a single value out.
Request URL looks like this:
https://apilayer.net/api/convert?access_key=...&from=CAD&to=GBP&amount=234
Thanks for the help!
Ok, lets say that json response is in a variable named $response, you must use json_decode and then do as follows:
$decoded = json_decode($response);
$result = $decoded->result;
$quote = $decoded->info->quote;
var_dump($result, $quote);
Try this
$jsonArray = file_get_contents($yourUrl);
$jsonObject = json_decode($jsonArray);
echo $jsonObject->result;
echo $jsonObject->info->quote;
Related
I have a working api call to get basic user info and I am trying to return just the "followed_by" portion of the json data from "counts".
This link should return just the json page on your browser
https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0
And here is my code to try and parse the result, but all I am getting is a blank screen.
$jsondata = file_get_contents("https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0");
$json = json_decode($jsondata, true);
$json2 = json_decode($jsondata);
//method one to parse, not working
$parsedvalue = $json['counts'][1]['followed_by'];
echo $parsedvalue;
//method two to parse, not working
$followercount = $json2->counts->followed_by;
echo $followercount;
Here is the fix I got figured out for //method 2
$jsondata = file_get_contents("https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0");
$json2 = json_decode($jsondata);
$followercount = $json2->data->counts->followed_by;
This properly returns your follower count, but I still need help understanding how to fix method one
I have the following code in a codeigniter REST app (built using: https://github.com/chriskacerguis/codeigniter-restserver)
public function fullname_get()
{
$fullname = array("fname"=>"john", "lname"=>"doe");
$data["json"] = json_encode($fullname);
$this->response($data["json"], 200);
}
When I call the API, this return json that looks like this:
{\"fname\":\"john\",\"lname\":\"doe\"}
The above json string fails http://jsonlint.com/ test because of the escape character "\".
Just wondering how I can work around this?
I'm building a REST api that is supposed to return json ... and I have to make sure it's legit json.
Thanks.
Try this
$fullname = array("fname"=>"john", "lname"=>"doe");
$this->response($fullname, 200);//it sends data json format. You don't need to json encode it
You got that response because your data is json encoded twice
You have to strip slashes, use this stripslashes(json_encode($fullname)). Full code mention below:
public function fullname_get()
{
$fullname = array("fname"=>"john", "lname"=>"doe");
$data["json"] = stripslashes(json_encode($fullname));
$this->response($data["json"], 200);
}
I hope this will solve your issue.
It is legit JSON - and you didn't write a test ;)
{
"fname": "john",
"lname": "doe"
}
Please see the demo over at http://ideone.com/5IW1Ef
The class you are using does magical things:
$this->response($this->db->get('books')->result(), 200);
and based on the format specified on the URL the response data is converted to JSON. You don't have to do the JSON encoding.
Please read the examples provides here
https://github.com/chriskacerguis/codeigniter-restserver#responses
$fullname = array("fname"=>"john", "lname"=>"doe");
$this->response($fullname, 200);
http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814
I have the following code in a codeigniter REST app (built using: https://github.com/chriskacerguis/codeigniter-restserver)
public function fullname_get()
{
$fullname = array("fname"=>"john", "lname"=>"doe");
$data["json"] = json_encode($fullname);
$this->response($data["json"], 200);
}
When I call the API, this return json that looks like this:
{\"fname\":\"john\",\"lname\":\"doe\"}
The above json string fails http://jsonlint.com/ test because of the escape character "\".
Just wondering how I can work around this?
I'm building a REST api that is supposed to return json ... and I have to make sure it's legit json.
Thanks.
Try this
$fullname = array("fname"=>"john", "lname"=>"doe");
$this->response($fullname, 200);//it sends data json format. You don't need to json encode it
You got that response because your data is json encoded twice
You have to strip slashes, use this stripslashes(json_encode($fullname)). Full code mention below:
public function fullname_get()
{
$fullname = array("fname"=>"john", "lname"=>"doe");
$data["json"] = stripslashes(json_encode($fullname));
$this->response($data["json"], 200);
}
I hope this will solve your issue.
It is legit JSON - and you didn't write a test ;)
{
"fname": "john",
"lname": "doe"
}
Please see the demo over at http://ideone.com/5IW1Ef
The class you are using does magical things:
$this->response($this->db->get('books')->result(), 200);
and based on the format specified on the URL the response data is converted to JSON. You don't have to do the JSON encoding.
Please read the examples provides here
https://github.com/chriskacerguis/codeigniter-restserver#responses
$fullname = array("fname"=>"john", "lname"=>"doe");
$this->response($fullname, 200);
http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814
How can I get a value of a second level JSON node using PHP from a Ajax request?
If I have the next JSON data in client:
var Data = {idJS: "1", dataToSet: "example", another:{ field1: "example2"} };
When the Ajax request is done, in PHP, dataToSet is get as follows: $_POST['dataToSet'], in particular, I use Codeigniter, then I use $this->input->post('dataToSet'). But, How can I get another->field1?
[Solved]:
$postdata = $this->input->post();
$postdata['another']['field1'];
I don't know how you do it with codeigniter, but you have to decode the json. Afterwards you can acces the field. This might look something like this:
$postdata = json_decode($this->input->post);
$postdata['another']['field1];
This should do it:
$data = $this->input->post('another');
print_r($data->field1);
// or you can do it in one line:
$this->input->post('another')->field1;
Try this:
$ata = json_decode($this->input->post('dataToSet'), true);
$field1 = $data['another']['field1'];
http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json
is the url, which contains json data. I want to use that data and send SMS to a particular phone no if value of latitude and longitude(Extracted from JSON).
Check constraints, we can use through php. But the main problem is How to extract data from JSON file?
I don't want to give you the solution, so the below should be enough to get you started.
Read in the JSON data using file_get_contents
Parse the JSON string into a PHP object using json_decode
Your code should look something like this:
$url = "http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json";
$contents = file_get_contents($url);
$jsonObj = json_decode($contents);
You mean something like this?
<?php
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->trends as $trend )
{
echo "{$trend->name}\n";
}