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);
Related
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
I'd like to get JSON response on my second server with the domain example.com where the getit.php file contains:
$arr = array('antwort' => 'jo');
echo json_encode($arr);
Now when I try to get it with my first server:
$url = 'http://www.example.com/getit.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$antwort = curl_exec($ch);
curl_close($ch);
$antwort = json_decode($antwort);
echo '<pre>';
var_dump($antwort);
echo '</pre>';
I get:
object(stdClass)#2 (1) {
["antwort"]=>
string(4) "jo"
}
And not an normal array, how do I convert this to array?
I allready tried $antwort = json_decode(json_encode($antwort), True); but I only get a weird string with that?!
JSON's definition is: JavaScript Object Notation. JavaScript arrays can only have numeric keys. Your array has string keys, therefore it MUST be encoded as a JavaScript OJBECT, which do allow string keys.
Tell json_decode() you want arrays instead:
$arr = json_decode($json, true);
^^^^
as per the docs
Where how did you do your json_decode(json_encode($antwort))? The ONLY way that'd return a string is if you encoded the $antwort you'd gotten from curl_exec().
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.
I have recently started working with the PHP curl() function and I am trying to convert my retrieved JSON object to an associated array. Can anybody point me in the right direction? Thanks!
<?php
$ch = curl_init("https://canvas.instructure.com/api/v1/courses?access_token=7~8SXvaXHjMFZFHAdU5yU0pxNmVwAj40sjW7jRHw1Bvzq09QTFWrJRFxTu4pHAqSZU");
curl_exec($ch);
curl_close($ch);
?>
The response:
[{"account_id":81259,"course_code":"CS50","default_view":"feed","id":870674,"name":"CS50","start_at":"2014-08-05T18:29:18Z","end_at":null,"public_syllabus":false,"storage_quota_mb":250,"apply_assignment_group_weights":false,"calendar":{"ics":"https://canvas.instructure.com/feeds/calendars/course_6QRRvAKDngrrXtTBhzCA5Oz46g3aLgfRt7PNH0NN.ics"},"enrollments":[{"type":"student","role":"StudentEnrollment","enrollment_state":"active"}],"hide_final_grades":false,"workflow_state":"available"}]
Use CURLOPT_RETURNTRANSFER to capture the result in a string, which is what you pass to json_encode. I think you're passing $ch to json_decode which is not what you want. (As the error message states, $ch is a resource and json_decode expects to be passed a string).
$ch = curl_init("https://canvas.instructure.com/api/v1/courses?access_token=7~8SXvaXHjMFZFHAdU5yU0pxNmVwAj40sjW7jRHw1Bvzq09QTFWrJRFxTu4pHAqSZU");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// ...
$response = curl_exec($ch);
// $response will be false if the curl call failed
if($response) {
var_dump(json_decode($response, true));
}
See curl_setopt documentation for more information.
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);