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
Related
I'm trying to print the value from currency converter API JSON data.
Anyone can help me to print the value from this URL
https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y?
You have to use file_get_contents() along with json_decode()
<?php
$json_data = file_get_contents('https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y');
$array = json_decode($json_data, true);
var_dump($array["USD_IDR"]["val"]);
?>
I have tested it on the local machine and working fine:-
https://prnt.sc/jd1kxo And https://prnt.sc/jd1l7w
Use Json_decode
$data = json_decode('{"USD_IDR":{"val":13965}}', TRUE);
var_dump($data["USD_IDR"]["val"]); //int(13965)
Try this:
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$jsontoarr = json_decode($response);
echo $jsontoarr->USD_IDR->val;
Good luck.
I have the below code and what I am trying to do is get the json value from "total_reviews" into a php variable which we can call $total_reviews.
however what is happening is i am getting an error that says
Notice: Undefined property: stdClass::$total_reviews
Here is my code
//URL of targeted site
$url = "https://api.yotpo.com/products/$appkey/467/bottomline";
// Initiate curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
$data = json_decode($result);
echo "e<br />";
echo $data->total_reviews;
// close curl resource, and free up system resources
curl_close($ch);
?>
If I print_r the result I have the below print out
{"status":{"code":200,"message":"OK"},"response":{"bottomline":{"average_score":4.2,"total_reviews":73}}}
If you want the JSON data as an array then use the second parameter on json_decode() to make it convert the data to an array.
$data = json_decode($result, true);
If you want the JSON data as it was passed to you and I assume that is as an Object then use
$data = json_decode($result);
echo "<br />";
echo $data->total_reviews;
The json_decode() manual page
But either way, if this is your JSON String,
{"status":
{
"code":200,
"message":"OK"
},
"response":
{
"bottomline":
{
"average_score":4.2,
"total_reviews":73
}
}
}
then the total_reviews value would be
echo $data->response->bottomline->total_reviews;
or if you used parameter 2 to convert a perfectly good object into an array
echo $data['response']['bottomline']['total_reviews'];
json_decode() defaults to extracting into a stdClass object. If you want an array, pass a truthy value as the second argument:
$data = json_decode($result, true);
So I have this api: http://85.17.32.4:8707/status-json.xsl
And I want to extract a few things from it, so I started off really basic:
<?php echo json_decode('http://85.17.32.4:8707/status-json.xsl');
It gave absolutely no result. Next try:
<?php
$var = json_decode('http://85.17.32.4:8707/status-json.xsl');
var_dump($var);
It just retourned NULL.
Then I tried making a cURL function:
<?php
$url = 'http://85.17.32.4:8707/status-json.xsl';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
This also returned NULL. Do I have any further options?
Thanks.
Yes, you do have further options: check json_last_error_msg and see whether there was an issue with the json decoding:
$json = json_decode($result, true);
# check if there has been an error decoding:
if (! isset($json)) {
echo "Decoding error: " . json_last_error_msg() . PHP_EOL;
}
Output:
Decoding error: Syntax error
i.e. there is a syntax error in the JSON. You need to tell the JSON provider that they are not providing valid JSON.
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"];
Hi I'm a little new at CURL, but I'm trying to request some json data and then parse the results. I am having success with retrieving the data, but I can't handle the response. Here's the code
function bitBucketCurl($url)
{
global $bitPassword;
global $bitUsername;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$bitUsername:$bitPassword");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$commitinfo = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
return $commitinfo;
}
$json = bitBucketCurl($url);
echo $json; // This seems to work in that, when I load the page, I can see the json data
//turn json data into an array - this is what does not seem to be working
$obj_a = json_decode($json, true);
print_r ($obj_a); //the result is simply a 1 rather than the array I would expect
The basic problem is the json data shows up when I echo $json but when I try to turn that data into an array it doesn't work. When I print the array, I just get a '1'.
I got the required result by adding the following line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);