Get JSON data from API into string - php

Im trying to get some data into a string from an API..
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://api.feathercoin.com/?output=usd");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$result = curl_exec($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
// close curl resource to free up system resources
curl_close($ch);
?>
The above gives me: array(1) { ["usd"]=> float(1.210935) }
Now all I need to do is get the 1.210935 into a string of $coinvalue.
Can anyone help me do this?!!
Thank you
Jason

$result = json_decode($result, true);
$coinvalue = (string) $result["usd"];

Related

Is there a way to get value of json object from a URL in PHP?

So i am trying to get values of json object from a url, when i hit that url on post man i get something like this
{
"error": "0",
"errorString": "",
"reply": {
"nonce": "5e415334832a8",
"realm": "VMS"
}
}
So, i am trying to write a php code that displays the value of nonce in the browser but it is not working
i have the following code
$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect, true);
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;
I also did this
$ch = curl_init("https://example.com/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
echo $ch->reply[0]->nonce;
But it does not seem to work still.
Below your 2 code samples with suggested corrections.
Using file_get_contents : remove 2nd argument of json_decode
$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect); // note removal of 2nd parameter
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;
Using curl : you forgot to parse curl output with json_decode
$ch = curl_init("https://example.com/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data); // added this line, because curl doesn't decode json automatically
echo $ch->reply[0]->nonce;

GET a JSON Array with an API in PHP?

Basically I'm trying to GET an API that gives me a JSON array. It should only be one integer. Whenever I try to though I receive the error:
Notice: Trying to get property of non-object in /public_html/call.php on line 16
Here's call.php:
<?php
require 'connection.php';
$players2weeks = '(removed api)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $players2weeks);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
print_r($response);
echo $response->players_2weeks;
?>
I have the print_r for troubleshooting but I'm getting nothing. My apologies for a noob question by the way, I have no experience with JSON.
Appreciate the help!
I think you can use this Code :
<?php
require 'connection.php';
$players2weeks = '(removed api)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $players2weeks);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
$responses = json_decode($response);
print_r($responses);
echo $responses['players_2weeks'];
?>
You should change the format of echo when the decoded JSON objects. and the Variable same means some clashes in the print so I changed the $response variable also...
You have to make array to json string by using the json_encode() function on the api.
For example: xxx.php (api)
$result = array(); //return array
echo json_encode($result);
Then you can get result by json array on the api caller.
var_dump(json_decode($result)); // Object
var_dump(json_decode($result, true)); // Associative array

Json decode comes up as NULL

#input url
$url = 'http://www.example.com';
#get the data
$json = file_get_contents($url);
$contents = utf8_encode($json);
#convert to php array
$php_array = json_decode($json);
var_dump($php_array);
exit;
I'm trying to decode a website but once I decode it my page comes up as NULL, does anyone know how I can fix it? Thanks
In your case http://www.example.com this URL returns 404 error. so file_get_contents($url) get null value.
$url = 'http://www.example.com';
$json = file_get_contents($url); // HTTP 404
echo $json; //returns null
This works fine
<?php
$url = 'http://www.example.com';
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$php_array = json_decode($output, true); // true for returning to an array
echo "<pre>";
print_r($php_array);
echo "</pre>";
Extra Tip:
I too faced null issue sometimes. You could ask json_last_error() to get definite information.
You can use CURL request here:
Example:
<?php
$url = 'http://www.example.com'; // your url
$ch = curl_init(); // initiate
curl_setopt($ch, CURLOPT_URL,$url); // curl url
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch); // curl output
$php_array = json_decode($result,true);
var_dump($php_array);
?>
Output:
array(3) { ["timestamp"]=> string(8) "10:38:38" ["error_num"]=> int(404) ["error_msg"]=> string(20) "File Not Found Error" }
CURL give you more options to fetch remote content and error checking than to file_get_content.

How to extract data from a decoded JSON object in php

I wanted to try to get data from a JSON string which is loaded from another page. I currently have used Curl to get the data from the webpage but I can't acces the data in it.
I've already tried:
var_dump(json_decode($result->version, true));
var_dump(json_decode($result[3][0]["date"], true));
But this does't seem to work as it always returns NULL
$url="https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201";
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
First decode the JSON, then get the properties you want. Like this:
$yourObject = json_decode($result);
var_dump($youObject->version);
this is working for me.
<?php
$url = "https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201";
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
$data = json_decode($result);
//echo $data->data[0]['date'];
echo "<pre>";
print_r($data->data[0]->date);
}
?>
if you want to get date of all index then try this in loop.
Firstly if your using GET there is no need to use CURL,
$result = file_get_contents(https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201);
Will work just as well without any of the overhead. I suspect that your CURL isn't returning the page content so using file_get_contents() will fix it.

How to pass result for CURL request from another domain

I have a function that will give some results.
Suppose anyone connecting to my url with CURL and trying to get result, how should I return result for the CURL.
I need to pass JSON in return of that CURL
Is that I need to echo the result or return?
I tested with,
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $myURL); // here passed my url
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$result = curl_exec($ch);
$info = curl_getinfo($ch);
// close cURL resource, and free up system resources
curl_close($ch);
In the url I am doing this,
$model = new Application_Model_Listings();
$result['communtityDetails'] = $model->getCommunityWidgetDetails();
$result = json_encode($result['communtityDetails']);
// return $result; or echo $result ??
CURL connection is happening, but result is not getting.
How should I pass this result??

Categories