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().
Related
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);
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
Was wondering how i'd be able to extract the index part of the json in php.
Has been bugging me for ages. Thanks!
$ch = curl_init('https://domain.com/blah/blah');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$promo = curl_exec($ch);
curl_close($ch);
$promo = json_decode($promo, true);
.
{"something":"blahhhh","name":"bob!","id":"7","select":[{"Index":1,"code":"a1","name":"hello","description":"more text"},{"Index":2,"code":"a2","name":"bye","description":"test.."},{"Index":3,"code":"a3","name":"ayeee","description":"Morning!"},{"Index":4,"code":"a4","name":"Cheese!","description":"Yummy!"},{"Index":5,"code":"a5","name":"Water","description":"why is it cloudy? :( "}],"chant":"Free the ducks!","joined":"2015-01-01T16:49:05.000+0000","cool":false}
When the 2nd parameter of json_decode is set to true the function return an associative array who's really close to the structure of the JSON.
From there all what you got to do is put back the JSON path into an associative php array to access the wanted value !
foreach($promo['select'] as $select){
echo $select['Index'].' ';
}
example : http://codepad.org/OMg7WTr0
If you're looking to extract other value and you don't figure out the path just do var_dump($promo); it will give you a clear view of the array path to use
I am using cURL to get json information from a site that pulls a random tumblr picture from a list of sources and I am interested of putting the json data retrieved into php variables so I can call for example, just the image url
$url = "http://someurl.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$a = curl_exec($ch);
$json = var_dump(json_decode($a, true));
I get:
array(3) {
'image' =>
array(1) {
[0] =>
string(62) "http://picture.jpg"
}
'source' =>
string(31) "http://source.tumblr.com"
'page' =>
string(9) "/page/164"
}
What would I now do in order to just print the url for the image?
I have tried
$url = $json["image"][0];
and then calling $url, but it gives me nothing in return. What I am doing wrong?
I have never worked with json before so I am at a loss here, any help is appreciated!
according to code looks:-
try
change
$json = var_dump(json_decode($a, true));
to
$json = json_decode($a, true);
I have a JSON string that passes the checks in http://www.freeformatter.com/json-validator.html, which returns:
The JSON input is valid in JavaScript.
The JSON input is valid according to RFC 4627 (JSON specfication).
However, when I run:
$string = json_decode($string);
print_r($string);
It prints the string exactly the same way it was before the json_decode($string).
Is there something else I can try? Or maybe something I don't know or didn't think about?
The string is here: http://pubapi.cryptsy.com/api.php?method=marketdatav2
To get the string, I was using:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=marketdatav2");
$ret = curl_exec($ch);
and then
$ret = substr($ret, 0,-1);
As it returned a 1 at the end, although I had originally tried it without removing the last character and got the same result.
$s = file_get_contents("http://pubapi.cryptsy.com/api.php?method=marketdatav2");
$s = json_decode($s);
print_r($s);
works like a charm
here is my curl version
$url = "http://pubapi.cryptsy.com/api.php?method=marketdatav2";
$ch=curl_init();
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
$data=curl_exec($ch);
curl_close($ch);
$s = json_decode($data);
print_r($s);
It looks like that page is being served as windows-1252, but I believe JSON must be in UTF-8. Try converting it to UTF-8 first, and then run it through json_decode().