Are there any alternatives to json_decode? - php

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().

Related

Convert html to json in php laravel

Im hitting this url
https://graph.facebook.com/1843624489016097?fields=link&access_token=EAAD3ZBKkIhYMBAL3KRi9eZBXlYJADZARRFMSe0Nc35WTP92X2etkccVqnjNcjJgKbd8ABtX5pyDPN0nAA7jORyjpOGexZCYp1Sf2iw0DJjCf8UkPiLwhuApSGDGZBvy5w7vk3U0Ba97FZA2DO7J4m4UjvbIolDaRP9TRpemEmLyQZDZD
with the below code in php,
$url ='https://graph.facebook.com/' . $connection->provider_id . '?fields=link&access_token=' . $connection->token;
$ch = curl_init($url);
$json= curl_exec ($ch);
I have this html coming from Facebook, I want to use only "Link" in this,
{"link":"https:\/\/www.facebook.com\/app_scoped_user_id\/YXNpZADpBWEdrUlJiUzc0SWFWZATI4SEVJUmJHTTJQVHU2M3owcTJLOHh5MnJYOTI0LWdMT3VFUC1veXNWdXBhM3o3RzdkQmV4cjNfTC1nSkdheGFhV19pWWU5T1ZAWSzlkN0NBTUl4NVZAKTE9oRjlFbjdObU5i\/","id":"1686741234967769"}1
I tried converting that into JSON but its not working, Its coming in same format, since im doing this in a API, im checking this under Postman, I did like this..
$request = json_encode($json, JSON_HEX_QUOT | JSON_HEX_TAG);
The format is not getting convert to json, Im doing in PHP Laravel.
There's a 1 at the end of the output, possibly you're echoing something extra that you shouldn't .
I suspect you expect curl to return the actual result but you are not using the appropriate flag. The reason I suspect that is because you are assigning the return result to $json but without the flag CURLOPT_RETURNTRANSFERwill return true and not any json value.
Here's what you can try:
$url ='https://graph.facebook.com/' . $connection->provider_id . '?fields=link&access_token=' . $connection->token;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER);
$json= curl_exec ($ch);
$jsonArray = json_decode($json, true);
$link = $jsonArray["link"];
More information on the curl flags in the manual
You should use json_decode in place of json_encode. So you should try like:
$request = json_decode($json, true);
$link = $request["link"];
Also use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); to save response in variable just after curl_init.

return Json string not working

I want to get data from api..
but I have trouble with json ..I can not get json string .. it print as normal string or xml..
I try multi method from net but no solution..
this my code:
$url = "http://fv4online.com/egov_api/v1/students/7";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept:
application/json;charset=UTF-8'));
// 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);
print_r($output);
$t=json_decode($output,true);
print_r($t);
// close curl resource to free up system resources
curl_close($ch);
result of print ($output) is string:
7345345343testtesttesttest2000testliterary4564620171testDamascusar_lang300600350en_lang160400200fr_lang160400200national80200100history120300200geograph120300200philosophy160400300religion802001501700155060successful
and result of print(var_dump($output)) is xml:
'
7345345343testtesttesttest2000testliterary4564620171testDamascusar_lang<'... (length=1544)
note: Previously I was able to get this data without any problems, but suddenly the problem arose
That string contains all values the XML from the url returns, just without the tags around them. If you do 'View Source' you'll see more.
You get XML, not JSON. Could be they've changed that (which would be a bit odd, but I've seen worse). If you want is as JSON:
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);

How to create JSON response in PHP file?

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().

PHP string specialchars

I'm setting up a little webservice usable via an app on Android.
The function in the server is this:
$lat=43.0552592;
$lng=12.4483577;
$radius=2000;
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey;
$urlW = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=43.0552592,12.4483577&radius=2000&types=restaurant&sensor=false&key=XXXXxxxXXxxXXxxxxXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
return $response;
The trouble is that if I query Google with the $url (as in the above example) it returns the JSON with 'INVALID REQUEST', but if the query at the first curl_opt is done with $curlW will works like a charm.
While debugging that I've discovered, making $url return, that gets every & converted (before the curl_init!) in &amp...!
So I've tried almost every PHP string function to force every & decode or replacing every &entities to & only without any result.
Any suggestion?
you've done something wrong,
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey
should be
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$radius."&types=restaurant&sensor=false&key=".$apiKey;
but i don't guess that is what is doing the problem, try using file_get_contents() instead of curl, so $response will be like this:
$response = file_get_contents($url);

Decode json string returned from Flickr API using PHP, curl

Im trying to decode a json string returned from flickr within my PHP code. Im using CURL but it keeps returning a string even when I wrap json_decode() around the json sring variable. Any ideas?
$api_key = '####';
$photoset_id = '###';
$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=json&jsoncallback=1';
$ch = curl_init(); // open curl session
// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session
var_dump(json_decode($data));
Your request URL ends with:
&format=json&jsoncallback=1';
The correct name of the parameter is nojsoncallback, so the right URL you should be using ends like this:
&format=json&nojsoncallback=1';
Change that and it should work.
Regards.
That's because the returned data is not valid JSON. Its valid JavaScript, though.
The returned data is wrapped inside a default callback function called jsonFlickrApi.
You need to get rid of the JSON callback which wraps the JSON inside a callback function which is then supposed to be executed on the client side. You need to do some string manipulation on the returned JSON to remove the default callback jsonFlickrApi and then pass it to json_decode
$api_key = '####';
$photoset_id = '###';
$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=json';
$ch = curl_init(); // open curl session
// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session
$data = str_replace( 'jsonFlickrApi(', '', $data );
$data = substr( $data, 0, strlen( $data ) - 1 ); //strip out last paren
$object = json_decode( $data ); // stdClass object
var_dump( $object );
Even better instead of using a format=json in your url, use format=php_serial and get a serialize string then you wont have to worry about valid formating from flickr and you get an array in return
$api_key = '####';
$photoset_id = '###';
$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=php_serial';
$ch = curl_init(); // open curl session
// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session
$output = unserialize ($data);
stack overflow saves the day again. I scoured the flickr documentation and found NO MENTION of this "nojsoncallback" paramater.
who makes such a feature by default, then doesn't tell anyone how to disable it?
even worse, why would it be written that you have to ENable it in order to DISable the function?!
ridiculous... but thanks for the heads up, this fixed my problem!
The details of nojsoncallback is at the bottom this page https://www.flickr.com/services/api/response.json.html

Categories