How can I get the last price from Coinmarketcap API - php

I'm trying to get the last price in USD from this coin:
<?php
$json = file_get_contents('https://api.coinmarketcap.com/v2/ticker/2424/');
echo $json;
?>
I tried two different options on the JSON object like [0]['price]
Nothing works.

The response from coinmarketcap is in JSON, so you need to json_decode it.
The second paramter of json_decode lets you decide if its a object or array. For an array, mark this as true
<?php
$json = file_get_contents('https://api.coinmarketcap.com/v2/ticker/2424/');
$objResponse = json_decode($json, true);
echo $objResponse['data']['quotes']['USD']['price'];

Related

PHP get curl array results from inside a key value

I'm trying to get the access_token value from a curl response. The key value which is [0] renders out like so, {"access_token":"430f8a7d4f721a9e51e3558689ff28ec592923d2","expires_in":3600,"token_type":"Bearer","scope":null}.
However, I just need the value nested inside of access_token, which is 430f8a7d4f721a9e51e3558689ff28ec592923d2.
The output is rendered using this:
$cmd="curl -u testuser:123456 http://localhost/oauth2/server/token.php -d 'grant_type=client_credentials'";
exec($cmd,$result);
echo '<pre>'; print_r($result[0]); echo '</pre>';
How might go about doing this?
Btw, none of this is sensitive data. I'm just experimenting with oauth2.
Use the json_decode function on the results.
<?php
$json = '{"access_token":"430f8a7d4f721a9e51e3558689ff28ec592923d2","expires_in":3600,"token_type":"Bearer","scope":null}';
// Decode the JSON into an object
$decodedJson = json_decode($json);
$accessToken = $decodedJson->access_token;
var_dump($decodedJson,$accessToken);
// Decode the JSON into an array (note the true on the decode)
$decodedJson = json_decode($json,true);
$accessToken = $decodedJson['access_token'];
var_dump($decodedJson,$accessToken);
Encoding the json result, finds the subset.
$json = $result[0];
$json = json_decode($json, true);
echo $json['access_token'];

How do I "trim" this JSON with PHP?

I have a JSON that's strangely formatted ...but it's the way I receive it. Because the arrays inside are huge, simply copying and pasting it takes a long time, so I'm wondering if there's a PHP way to do it.
The way I get it is like this:
{"count":459,"results":[{"title":"Something ....}],"params":{"limit..},"type":"Listing","pagination":{"..":5}}
But I want to get only the "results" array, basically the part of [{"title":"Something ....}]. How would I do that?
Do
$arr = json_decode(your_json, true);
If you ned it as JSON again, do
json_encode($arr['results']);
You can get to that part as follows:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit":".."},"type":"Listing","pagination":{"..":5}}';
$obj = json_decode($json);
echo $obj->results[0]->title;
Outputs:
Something ....
You have to decode your json. Be sure that the json is valid!
Your decoded json returns an object of type stdClass instead of an array!
See below the tested code:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit": "foo"},"type":"Listing","pagination":{"foo":5}}';
$decoded = json_decode($json);
So you can access array results from the object $decoded:
print_r($decoded->results);
But, in the array, there are objects, thus:
echo $decoded->results[0]->title; // Something ....

GET info from external Array/API/URL using PHP

I have the url http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132 which leads to an array.
I want to get the value of the first 'sellorders' which in this case is: 0.00000048 and store it in the variable $sellorderprice.
Can anyone help?
Thanks.
Just access the url contents thru file_get_contents. Your page actually return a JSON string, to get those values into meaningful data, decode it thru json_decode, after that access the data needed accordingly:
$url = 'http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132';
$data = json_decode(file_get_contents($url), true);
$sellorderprice = $data['return']['DOGE']['sellorders'][0]['price'];
echo $sellorderprice;
That code actually points directly to index zero 0 which gets the first price. If you need to get all items an just simply echo them all you need to iterate all items thru foreach:
foreach($data['return']['DOGE']['sellorders'] as $sellorders) {
echo $sellorders['price'], '<br/>';
}
Its simple, you just have to decode json like this:
$json = file_get_contents("http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132");
$arr = json_decode($json, true);
$sellorderprice = $arr['return']['DOGE']['sellorders'][0]['price'];

How do I use JSON objects from Facebook with PHP?

http://developers.facebook.com/docs/reference/api/event
I'm trying to grab some values from a Facebook JSON even object in PHP. Namely, title of event, location, and people attending. Using the Graph API.
<?php
$jsonurl = "https://graph.facebook.com/331218348435?access_token=2227470867|2.rtBZMkVIVgKGZ7Xr4px3Dw__.3600.1280822400-662817093|apY_UHK_2SKQFel3XxpKJ09GEo4.";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
How can echo the values from JSON output? I'm assuming it will be returned as an array.
Thank you!
If you're using PHP, you can use the PHP SDK to query the API. This means you can make calls like:
$user = $facebook->api('/someusername', array('fields' => 'id,first name,last_name ...'));
However, with your example, you could do the following:
$url = "https://graph.facebook.com/331218348435?access_token=2227470867|2.rtBZMkVIVgKGZ7Xr4px3Dw__.3600.1280822400-662817093|apY_UHK_2SKQFel3XxpKJ09GEo4.";
$user = json_decode(file_get_contents($jsonurl));
echo $user['first_name'];
As if you use json_decode, it should decode the result into a native PHP array (or in same cases, an object).
Specify the second argument of true to json_encode to convert it to array and then you can print the output like this:
$json_output = json_decode($json, true);
echo '<pre>';
print_r($json_output);
echo '</pre>';
Now you can get specific item like this:
echo $json_output['title'];

Reading multiple json values with php

I am trying to read certain values from a json string in php, I am able to do a simple json string with only one value such as
$json = '{"value":"somevalue"}';
Using this:
<?php
$json = '{"value":"somevalue"}';
$obj = json_decode(json_encode($json));
print $obj->{'value'};
?>
But when i try an get a value from the following json string it throws an error...
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
I validated the json on JSONlint but not sure how to access the values within this with php.
Thanks
You can try this:
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
//since $json is a valid json format you needn't encode and decode it again
$obj = json_decode($json);
print_r($obj->filed);
print_r($obj->rule);
You can pass true as a second parameter to json_decode() to get the results as an array
$my_arr = json_decode($json, true);
var_dump($my_arr);
Should help you. You can then step through the array as you would normally.
use var_dump to print out the object with all it's members and hierarchy. you should then be able to find the value you are looking for

Categories