Three simple lines of code:
$input = #file_get_contents("php://input");
$event_json = json_decode($input,true);
print_r($event_json);
I have used first 2 lines to convert json data into array in php, and third line prints the following array (a portion I show here):
Array
(
[created] => 1326853478
[livemode] =>
[id] => evt_00000000000000
[type] => charge.succeeded
[object] => event
[request] =>
[pending_webhooks] => 1
[api_version] => 2014-07-26
[data] => Array
(
[object] => Array
(
[id] => ch_00000000000000
[object] => charge
[amount] => 100
[amount_refunded] => 0
My question is: How do I echo amount (which is 100) or amount_refunded (which is 0)?
echo $event_json['data']['object']['amount'];
echo $event_json['data']['object']['amount_refunded'];
Related
I am able to receive the following JSON data with the following PHP code:
$json = file_get_contents('https://xxxx');
$data = json_decode($json,true);
$forex = $data['items'];
echo "<pre>";
print_r($forex);
exit;
This gives me the following JSON data:
Array
(
[0] => Array
(
[new] =>
[data] => Array
(
[direction] => 1
[pip] => 0
[exchange] => FOREX
[symbol] => USDCHF
[interval] => 15
[pattern] => Resistance
[complete] =>
[identified] => 2020-02-26T14:45:00.000Z
[age] => 0
[length] => 259
[found] => 2020-02-26T14:45:56.381Z
[result_type] => KeyLevel
[result_uid] => 642525551
[prediction_price_from] => 0
[prediction_price_to] => 0
[group_name] => FX Majors
[symbol_name] => USDCHF
[symbol_id] => 0
[analysis_text] => Approaching Resistance level of 0.9800 identified at 2/26 14:45. This pattern is still in the process of forming. Possible bullish price movement towards the resistance 0.9800 within the next 15 hours.
[expires_at] => 2020-02-27T05:48:36.701Z
)
[links] => Array
(
[0] => Array
(
[rel] => detail
[href] => https://xxxx
)
[1] => Array
(
[rel] => analysis
[href] => https://xxxx
)
[2] => Array
(
[rel] => chart-xs
[href] => https://xxxx
)
[3] => Array
(
[rel] => chart-sm
[href] => https://xxxx
)
[4] => Array
(
[rel] => chart-md
[href] => https://xxxx
)
[5] => Array
(
[rel] => chart-lg
[href] => https://xxxx
)
[6] => Array
(
[rel] => icon-arrow
[href] => https://xxxx
)
)
)
What I am trying to do is to save the information into my MySQL database, however I am unable to get the data displayed even by testing to echo the data:
foreach($forex as $info) {
echo $info->symbol . '<br>';
}
Anyone with a possible solution for me to save the data or even enable me to display the data for each Array (Example: symbol, pattern, link chart-md).
Below the commented code:
//Save json text into variable $json
$json = file_get_contents('https://xxxx');
//Convert json into PHP array
$data = json_decode($json,true);
//Set $forex as $data['items'];
$forex = $data['items'];
//Print
echo "<pre>";
print_r($forex);
exit;
$forex is a PHP array variable (not a JSON string).
You could save your data into MySQL VARCHAR[Length] field using json string variable $json instead of PHP array $forex.
When you need to get the data from DB and display it, you could get JSON data from MySQL and convert it from JSON to Array using PHP json_decode($json,true).
If you want to use a value into a PHP array, use $info["symbol"] instead of $info->symbol
I am trying to display the data from Bigcommerce through php when I run the Bigcommerce::getCategories() i getting a array of data like this:
[0] => Bigcommerce\Api\Resources\Category Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[fields:protected] => stdClass Object
(
[id] => 88
[parent_id] => 0
[name] => Dell
[description] =>
[sort_order] => 0
[page_title] =>
[meta_keywords] =>
[meta_description] =>
[layout_file] =>
[parent_category_list] => Array
(
[0] => 88
)
[image_file] =>
[is_visible] => 1
[search_keywords] =>
[url] => /dell/
)
[id:protected] => 88
[ignoreIfZero:protected] => Array
(
)
[fieldMap:protected] => Array
(
)
)
but when I try to pass this to JQuery so that I can display it using this statement: <?php echo json_encode($categories); ?> I am getting an array of empty objects, what is the right way to get the array of objects in Bigcommerce API? Thanks.
From the first line of your code:
[0] => Bigcommerce\Api\Resources\Category Object
You have an object, not an array. Try casting it to an array first:
$array = (array) $yourObject;
I have json string and I want to fetch product data from this string How can I achieve this. Please some one help me.
Below is my string,
{"num_rows":2,"row":{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},"rows":[{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},{"setting":"a:6:{s:4:\"name\";s:17:\"Featured Products\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"146\";}s:5:\"limit\";s:1:\"4\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"}]}
There is a mix of JSON and PHP-serialized data.
<?php
$string = '{"num_rows":2,"row":{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},"rows":[{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},{"setting":"a:6:{s:4:\"name\";s:17:\"Featured Products\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"146\";}s:5:\"limit\";s:1:\"4\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"}]}';
$dataObject = json_decode($string);
foreach($dataObject->rows as $row){
$productData = unserialize($row->setting);
print_r($productData);
}
Will result in
Array
(
[name] => Featutred
[product] => Array
(
[0] => 145
[1] => 148
)
[limit] => 5
[width] => 200
[height] => 200
[status] => 1
)
Array
(
[name] => Featured Products
[product] => Array
(
[0] => 145
[1] => 146
)
[limit] => 4
[width] => 200
[height] => 200
[status] => 1
)
NOTE: There is no error checking in code above, since it is written for your particular example. If you are not sure that your input data is correct (that is usual), you need to check if JSON is ok, object is object and also has needed properties, etc.
Array
(
[response] => Array
(
[success] => 1
[result] => Array
(
[success] => Array
(
[0] => Array
(
[id] => 1958856
[device_id] => 7565
[message] => ....
[status] => pending
[send_at] => 1416564932
[queued_at] => 0
[sent_at] => 0
[delivered_at] => 0
[expires_at] => 1416568532
[canceled_at] => 0
[failed_at] => 0
[received_at] => 0
[error] =>
[created_at] => 1416564932
[contact] => Array
(
[id] => 312505
[name] => 123
[number] => 123
)
)
)
[fails] => Array
(
)
)
)
[status] => 200
i need to get [id] => 1958856.
$array['response']['result']['success'][0]['id'];
this should get to it
Additional details from iWontStop:
How it works
This is called a multi-dimensional array. They're found in many languages (most useful ones I'd say).
To access an item within the Array, for php it uses []. You can then place a string, number or variable containing one of the two within the [] like so:
$variable['key'];
If an item within an Array is also an Array you simply repeat the process of the [] like so:
$variable['key']['keytwo'];
I have the following stdObject obtained thru cURL / json_decode():
stdClass Object
(
[response] => stdClass Object
(
[status] => OK
[num_elements] => 1030
[start_element] => 0
[results] => stdClass Object
(
[publisher] => stdClass Object
(
[num_elements] => 1030
[results] => Array
(
[0] => stdClass Object
(
[id] => 1234
[weight] => 4444
[name] => Pub 1
[member_id] => 1
[state] => active
[code] =>
)
[1] => stdClass Object
(
[id] => 1235
[weight] => 4444
[name] => Pub 2
[member_id] => 2
[state] => active
[code] =>
)
)
)
)
[dbg_info] => stdClass Object
(
[instance] => instance1.server.com
[slave_hit] => 1
[db] => db1.server.com
[reads] => 3
[read_limit] => 100
[read_limit_seconds] => 60
[writes] => 0
[write_limit] => 60
[write_limit_seconds] => 60
[awesomesauce_cache_used] =>
[count_cache_used] =>
[warnings] => Array
(
)
[time] => 70.440053939819
[start_microtime] => 1380833763.4083
[version] => 1.14
[slave_lag] => 0
[member_last_modified_age] => 2083072
)
)
)
I'm looping through it in order to obtain each result's ID:
foreach ($result->response->results->publisher->results as $object) {
$publishers .= $object->id.",";
}
And although the code is working fine, PHP is rising the following notices/warnings:
PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1
PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1
PHP Warning: Invalid argument supplied for foreach() in /var/www/vhosts/domain.net/script.php on line 1
Any ideas? Thanks in advance!
Is it possible you have set the second parameter $assoc in json_decode() to true? Like this:
$result = json_decode($json_from_curl, true);
Thereby, getting back $result with all stdClass Objects converted to associative array.
EDIT:
If the $result you are getting is indeed an associative array, then we should treat it as such. Try replacing your foreach with this:
foreach ($result['response']['results']['publisher']['results'] as $arr) {
$publishers .= $arr['id'] . ",";
}
EDIT2:
From my own testing based on your code, everything should be working correctly. The notices/warnings should not occur. Perhaps some other code not shown in your question is causing it.