Retrieving JSON from site equals NULL - php

$url="https://alcurex.com/api/market.php?pair=mrc_ltc&price=buy";
$json = file_get_contents($url);
$data = json_decode($json);
echo $json;
var_dump($data);
Results in a null response. However when you visit the page you get a html read out of
{
"pair": MRC_LTC,
"time": "2014-07-23 07:34:54,
"price": 0.00000017,
"volume": 558.99741176,
"type": Buy
},

Response is not valid JSON format.
{ "pair": MRC_LTC, "time": "2014-07-23 07:34:54, "price": 0.00000017, "volume": 558.99741176, "type": Buy },
You could check it from Firefox console, correct JSON should be this:
var s = {
"pair": "MRC_LTC",
"time": "2014-07-23 07:34:54",
"price": 0.00000017,
"volume": 558.99741176,
"type": "Buy"
};
Errors:
Missing " for pair, time and type
Unnecessary comma at the end of string

You have three problems with this JSON:
pair value is not a valid type, if it should be string, it's missing quotes
time has string start quote but does not end it
type value is not a valid type, if it should be string, it's missing quotes
This is a valid JSON:
{
"pair": "MRC_LTC",
"time": "2014-07-23 07:34:54",
"price": 0.00000017,
"volume": 558.99741176,
"type": "Buy"
}

Related

Show JSON data from Request Body POST [duplicate]

As a test, this JSON data is being POSTed to my website:
{
"order": {
"id": null,
"created_at": null,
"status": "new",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"total_native": {
"cents": 2263,
"currency_iso": "USD"
},
"custom": "123456789",
"button": {
"type": "buy_now",
"name": "Test Item",
"description": null,
"id": null
},
"transaction": {
"hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"confirmations": 0
}
}
}
Since it is being sent server-to-server I cannot visibly see the data. I have tried sending the $_POST array to a text file, but it comes up blank. What I think I need to do is:
$data = json_decode($jsonData);
But how do I set the variable $jsonData?
You can try to use wrappers for reading raw POST query.
$data = file_get_contents("php://input");
Have you tried this, store the sting obtained to a variable and then decoding it?
$postedJsonData= '{"order":{"id":null,"created_at":null,"status":"new","total_btc":
{"cents":100000000,"currency_iso":"BTC"},"total_native":
{"cents":2263,"currency_iso":"USD"},"custom":"123456789","button":
{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":
{"hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}';
var_dump(json_decode($postedJsonData, true));
The true parameter would return an associative array
$data = json_decode($jsonData, True);

How to read JSON results from PHP

How to access element of below json output using php ..
{
"ISBN:9781430215752": {
"bib_key": "ISBN:9781430215752",
"preview":
"restricted",
"preview_url":
"https://archive.org/details/linuxrecipesforo00kuhn",
"info_url": "https://openlibrary.org/books/OL23936576M/Linux_recipes_for_Oracle_DBAs",
"details": {
"lc_classifications": ["QA76.9.D3 K84 2009"],
"latest_revision": 2,
"ocaid": "linuxrecipesforo00kuhn",
"contributions": ["Kim, Charles.", "Lopuz, Bernard."],
"source_records": ["marc:marc_loc_updates/v37.i44.records.utf8:10470755:1047"],
"title":
"Linux recipes for Oracle DBAs",
"languages": [{
"key": "/languages/eng"
}],
"subjects": ["Linux", "Oracle (Computer file)", "Relational databases", "Database management"],
"publish_country": "cau",
"by_statement": "Darl Kuhn, Charles Kim, Bernard Lopuz.",
"type": {
"key": "/type/edition"
},
"revision": 2,
"other_titles": ["Linux recipes for Oracle DataBase Administrators"],
"publishers": ["Apress", "Distributed to the book trade by Springer-Verlag"],
"last_modified": {
"type": "/type/datetime",
"value": "2014-04-06T06:55:36.956977"
},
"key": "/books/OL23936576M",
"authors": [{
"name": "Darl Kuhn",
"key": "/authors/OL1484587A"
}],
"publish_places": ["Berkeley, CA", "New York"],
"oclc_number": ["243543902"],
"pagination": "xxv, 501 p. :",
"created": {
"type": "/type/datetime",
"value": "2009-11-24T23:42:39.524606"
},
"dewey_decimal_class": ["005.75/65 22", "005.26/8"],
"notes": {
"type": "/type/text",
"value": "Includes index."
},
"number_of_pages": 501,
"isbn_13": ["9781430215752"],
"lccn": ["2009277832"],
"isbn_10": ["1430215755"],
"publish_date": "2008"
}
}
}
I tried using below code,
it doesn't work.
$json = json_decode($body);
echo $json->ISBN:9780980200447->info_url;
It's giving an error..
Is there any other easy way to read all elements?
You are on the right track. : is just an invalid character for a property and must be handled special.
echo $json->{'ISBN:9781430215752'}->info_url;
will get you the expected result.
You may also decode as an associative array
$json = json_decode($body, true);
echo $json['ISBN:9781430215752']['info_url'];
I would probably stick to this approach rather than the object oriented, since encapsulating keys makes code less readable
You must use
echo $json->{'ISBN:9780980200447'}->info_url;
since 'ISBN:9780980200447' is your class name.
Reference: php.net
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
?>
Use braces like this:
echo $json->{'ISBN:9781430215752'}->info_url;

Decode json in php and create variables

I a trying to decode a json callback.
The json code is posted to callback.php - Here is an example of the json:
{
"order": {
"id": "5RTQNACF",
"created_at": "2012-12-09T21:23:41-08:00",
"status": "completed",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"total_native": {
"cents": 1253,
"currency_iso": "USD"
},
"custom": "order1234",
"receive_address": "1NhwPYPgoPwr5hynRAsto5ZgEcw1LzM3My",
"button": {
"type": "buy_now",
"name": "Alpaca Socks",
"description": "The ultimate in lightweight footwear",
"id": "5d37a3b61914d6d0ad15b5135d80c19f"
},
"transaction": {
"id": "514f18b7a5ea3d630a00000f",
"hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"confirmations": 0
},
"customer": {
"email": "coinbase#example.com",
"shipping_address": [
"John Smith",
"123 Main St.",
"Springfield, OR 97477",
"United States"
]
}
}
}
I can echo the json and get the following response:
{"order""id":null,"created_at":null,"status":"completed","total_btc":{"cents":100000000,"currency_iso":"BTC"},"total_native":{"cents":83433,"currency_iso":"USD"},"custom":"123456789","receive_address":"1A2qsxGHo9KjtWBTnAopTwUiBQf2w6yRNr","button":{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":{"id":"52d064b59eeb59985e00002c","hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}
However if I try to decode the json using the following:
$array = json_decode($jsonString, true);
echo $array;
I get the following response: "200 Array"
I want to be able turn each json parameter in to a php variable.
You can access the variables within $array, for example by doing:
echo $array['custom']; // prints out "order1234"
You don't want to extract the variables directly into the local lexical scope of your program as that would create security concerns. Just use the data as indicated in the snippet above.

How do I turn a POSTed JSON value into PHP?

As a test, this JSON data is being POSTed to my website:
{
"order": {
"id": null,
"created_at": null,
"status": "new",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"total_native": {
"cents": 2263,
"currency_iso": "USD"
},
"custom": "123456789",
"button": {
"type": "buy_now",
"name": "Test Item",
"description": null,
"id": null
},
"transaction": {
"hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"confirmations": 0
}
}
}
Since it is being sent server-to-server I cannot visibly see the data. I have tried sending the $_POST array to a text file, but it comes up blank. What I think I need to do is:
$data = json_decode($jsonData);
But how do I set the variable $jsonData?
You can try to use wrappers for reading raw POST query.
$data = file_get_contents("php://input");
Have you tried this, store the sting obtained to a variable and then decoding it?
$postedJsonData= '{"order":{"id":null,"created_at":null,"status":"new","total_btc":
{"cents":100000000,"currency_iso":"BTC"},"total_native":
{"cents":2263,"currency_iso":"USD"},"custom":"123456789","button":
{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":
{"hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}';
var_dump(json_decode($postedJsonData, true));
The true parameter would return an associative array
$data = json_decode($jsonData, True);

PHP JSON Data Selection

How would I go about selecting the data of each title from the following JSON?
I have the JSON decoded, but I'm not sure how to select the part I want.
{
"responseData": {
"results": [
{
"title": "Justin Giesbrecht 749",
"titleNoFormatting": "Justin Giesbrecht 749",
},
{
"title": "Gopher dunes 09",
"titleNoFormatting": "Gopher dunes 09",
},
{
"title": "dirtbike Justin",
"titleNoFormatting": "dirtbike Justin",
},
{
"title": "A Warming",
"titleNoFormatting": "A Warming",
}
],
"cursor": {
"pages": [
{
"start": "0",
"label": 1
},
{
"start": "4",
"label": 2
}
],
"estimatedResultCount": "6",
"currentPageIndex": 0,
}
},
"responseDetails": null,
"responseStatus": 200
}
I thought it would be something like this, but I don't get anything:
echo "Response ". $jsonS->responseData->results[1]->title;
Actually you've got the reading of the title part right, it's the JSON that is invalid.
Copying the JSON into a JSON validator/lint e.g. http://www.jsonlint.com/ will show that the you have additional , (commas) after the last object attribute in a few places (5 places to be exact, after each 'titleFormatting' attribute and after 'currentPageIndex').
If you fix those errors and parse it using json_decode e.g.:
$jsonS = json_decode($json_text);
Then your own code:
echo "Response " . $jsonS->responseData->results[1]->title;
Will output the second (index 1 being the second index) results title
Response Gopher dunes 09
When you json_decode something it is converted to a regular PHP array, so you can reference it like $decodedObject["nodeName"].
When I parse that JSON as you quoted it with PHP's json_decode method, it gives me NULL because PHP doesn't think it's valid JSON. Add a var_dump($jsonS); to see if this is happening to you as well. If it is, you may need to be sure your JSON is valid.
Here's what I did, for reference:
$json_data = <<<END_OF_JSON
{
"responseData": { "results": [
{
"title": "Justin Giesbrecht 749", "titleNoFormatting": "Justin Giesbrecht 749",
},
{
"title": "Gopher dunes 09",
"titleNoFormatting": "Gopher dunes 09",
},
{
"title": "dirtbike Justin",
"titleNoFormatting": "dirtbike Justin",
},
{
"title": "A Warming",
"titleNoFormatting": "A Warming",
}
],
"cursor": {
"pages": [ {
"start": "0",
"label": 1
},
{
"start": "4",
"label": 2
}
],
"estimatedResultCount": "6",
"currentPageIndex": 0,
}
},
"responseDetails": null,
"responseStatus": 200
}
END_OF_JSON;
$jsonS = json_decode($json_data);
var_dump($jsonS);
echo "Response ". $jsonS->responseData->results[1]->title;
And the output was:
NULL Response
If you're using different JSON, please edit your question and share it. But if you're actually using the above, it's not valid...
In JSON you are not allowed to leave trailing comma in array/object definition. So, when in PHP is perfectly valid to write:
$a = array(1,2,3,);
(note last comma), in JSON
a : [1,2,3,]
or
a : {x :'y',}
is invalid.

Categories