I Have this data from my API Call.
[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"'42060106''9405510200830072094975'\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]
How can I convert this data into PHP Array? I tried using json_decode($result,true) but it is not working properly. Thanks in advance.
UPDATE:
My PHP Code
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode(stripslashes($result), true);
$json=str_replace("\\",'', $result);
$jsondata=json_decode($json,true);
print_r($jsondata);
//echo $result;
use json_decode() for this:
// It will convert the given json to an array
$arr = json_decode($json, true);
// The second param is for array, o/w it will return an object
Reference
$json='[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"420601069405510200830072094975\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]';
$json=str_replace("\\",'', $json);
$jsondata=json_decode($json,true);
print_r($jsondata);
CURL output is json and in line $json_result = json_decode($result, true); you decoded it to an array, after str_replace you have an array but you decoded it again, may be if you remove $jsondata=json_decode($json,true); and return $json you receive an array
Your string \"trackingnumber\":\"'42060106''9405510200830072094975'\" has syntax error.
$result = '[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"420601069405510200830072094975\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]';
$json_result = json_decode(stripslashes($result), true);
$json=str_replace("\\",'', $result);
$jsondata=json_decode($json,true);
print_r($jsondata);
Try this,
$jsonData = stripslashes(html_entity_decode($result));
$output = json_decode($jsonData, true);
echo "<pre>";
print_r($output);
echo "</pre>";
Or Try this way,
$out = preg_replace('/\\\"/',"\"", $result);
$output = json_decode($out, true);
echo "<pre>";
print_r($output);
echo "</pre>";
Related
I'm using this to get my response (only relevant code below):
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
$my_resp = $result['total_count'];
var_dump($my_resp);
echo $result gives me an object that appears to be valid JSON. Like this:
{
"total_count": 1
}
However, $my_resp comes back as NULL. What am I doing wrong here?
You need to turn it into an object from a string
$result = json_decode( curl_exec($ch));
or an associative array
$result = json_decode( curl_exec($ch), true );
If it is JSON, you should json_decode() it before trying to read fields.
Does Php have a method that calls a Json URL, and save in a variable the data it fetches
for example can I used:
"https://www.googleapis.com/blogger/v3/blogs/1/posts/"
I been trying with this code, but i am not sure if this is wrking
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/blogger/v3/blogs/1/posts/");
curl_setopt($ch, CURLOPT_HEADER, 0);
$df = curl_exec($ch);
curl_close($ch);
You can use the bulti-in function json_decode http://php.net/manual/it/function.json-decode.php
$json = file_get_contents("https://www.googleapis.com/blogger/v3/blogs/1/posts/");
$obj = json_decode($json); //returns an object of json values
$array = json_decode($json, true); //returns an array of json values
The problem is http*s*, try adding these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
I am trying to hit an API and get some JSON back. I can’t seem to figure out what I am doing wrong here. I am getting null as the output.
Here is my PHP code:
<?php
$query_string_full = 'https://api.cityofnewyork.us/calendar/v1/search.htm?app_id=39563317lalaland&app_key=somethingsomething&categories=City%20Government%20Office';
$json = file_get_contents($query_string_full);
$obj = json_decode($json);
echo '<pre>'. json_encode($obj, JSON_PRETTY_PRINT) .'</pre>';
?>
The function file_get_contents doesn’t work with https. You should use the cURL functions instead.
<?php
$query_string_full = 'https://api.cityofnewyork.us/calendar/v1/search.htm?app_id=39563317&app_key=8396021a9bde2aad2eaf8ca9dbeca353&categories=City%20Government%20Office';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query_string_full);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$obj = json_decode($json);
echo '<pre>'. json_encode($obj, JSON_PRETTY_PRINT) .'</pre>';
?>
I made this little function to return the json from a few different apis. You need to be using curl.
function exposeJSON ($apiUrl) {
$json_url = $apiUrl;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $json_url);
// Built in authentication if needed.
//curl_setopt($ch, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
// grab URL and pass it to the browser
$response = curl_exec($ch);
$return = json_decode($response[0], true);
// close cURL resource, and free up system resources
curl_close($ch);
return $return;
}
So you could do something like
$apiData = exposeJSON('urltoapi');
echo $apiData; // Should be the json.
I have this PHP code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$jsondata = curl_exec($ch);
if (curl_error($ch))
die("Connection Error: ".curl_errno($ch).' - '.curl_error($ch));
curl_close($ch);
$arr = json_decode($jsondata);
echo "\nResponse: ".htmlentities($jsondata)."\n\nArray: ".print_r($arr,true);
Which outputs:
Response: {"result":"success","clientid":83} Array:
stdClass Object ( [result] => success [clientid] => 83 )
I want to grab the value 83 stored in the variable called $clientid.
But I can't figure out how to do it.
You are confusing a PHP object with a PHP array.
You should be doing this: echo $arr->clientid;
Example 1, access a value of an object like this:
<?php
$jsondata = '{"firstName":"John", "lastName":"Doe"}';
$arr = json_decode($jsondata);
echo gettype($arr);
echo $arr->firstName;
?>
This prints:
object
John
Example 2, access a value of an array like this:
<?php
$yarr = array(5,6,7);
echo $yarr[0];
?>
This prints:
5
Your main mistake is assuming json_decode returns an array.
$arr = json_decode($jsondata);
Will set up $arr as an object. If you wanted to access it as an array, you can do this:
$arr = json_decode($jsondata, TRUE);
The extra parameter on the end tells json_decode to return an array instead of an object. Then you could do:
echo $arr["clientid"];
try echo $arr["clientid"] to get data from array
I tried to use JSON decode to get the youtube API feed. However, when I paste the JSON result in http://www.jsonlint.com/ I noticed something like
"media$group": {
"media$category": [
Unfortunately some symbols are rejected by php. Here is my code, I tried to remove this $ symbol, but maybe not success. How do I solve this?
$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
...
}
Your problem is the usage of PHP identifiers to access the contents. The simplest solution here would be to get an array instead of an object:
$data = json_decode ( $json , $assoc = true );
This allows access to fields with:
echo $result['media$group']['media$description'];
If you want to keep the object syntax, that's possible with this kludge:
echo $result->{'media$group'}->{'media$category'};
(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)
This work:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
var_dump($result);
}
?>