php


Json encoded array Split Using php


I'm actually new to the Api world, i'm working on an Api that fetches and sends back data using a Curl Request. On Fetch the information below is passed
{"posts":{"userinfo":{"fullname":"Precious Tom","user_name":"Kendrick","email":"tomprezine#gmail.com","gender":"Male","country":"Nigeria","city":"Port Harcourt","state":"Rivers","year":"1997","month":"9","day":"6"}}}
content type: text/html; charset=UTF-8
http code: 200
Splitting the Array seem to be my Problem, I'm sorry if this question hs been asked before, but please, i need your help.
Thanks.
For More Info, Here is my Curl Request
<?php
session_start();
# data to be sent
$data = array(
'public_key' => 'pk_test_3gc9ffb0hccggf5f3b4e258da848343dff4ae900',
'app_name' => 'Circlepanda',
'app_id' => '2147483647'
);
$curl = curl_init();
# you can also set the url you wanna communicate with by setting
# $curl = curl_init('http://localhost/circlepanda');
# We post Data
curl_setopt($curl, CURLOPT_POST, 1);
# Set the url path we want to call
curl_setopt($curl, CURLOPT_URL, 'http://localhost:8888/circlepanda/api/userinfo');
# Make it so the data coming back is put into a string
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
# You can also bunch the above commands into an array if you choose using: curl_setopt_array
# Send the request
$result = curl_exec($curl);
# Get some cURL session information back
$info = curl_getinfo($curl);
echo '<br> content type: ' . $info['content_type'] . '<br>';echo 'http code: ' . $info['http_code'] . '<br>';
# Free up the resources $curl is using
curl_close($curl);
?>
I'll be passing a Variable not a direct Array.
Tried Direct arrays your code worked fine, burr on passing a variable, it stopped working...
$city_names = json_decode('{"posts":{"userinfo":{"fullname":"Precious Tom","user_name":"Kendrick","email":"tomprezine#gmail.com","gender":"Male","country":"Nigeria","city":"Port Harcourt","state":"Rivers","year":"1997","month":"9","day":"6"}}}', true);
print_r($city_names);
ans
Array
(
[posts] => Array
(
[userinfo] => Array
(
[fullname] => Precious Tom
[user_name] => Kendrick
[email] => tomprezine#gmail.com
[gender] => Male
[country] => Nigeria
[city] => Port Harcourt
[state] => Rivers
[year] => 1997
[month] => 9
[day] => 6
)
)
)
$city_names = json_decode($json, true);
print $arr['posts']['userinfo']['fullname'];
You've 2 options:
1 - Convert json to an object using:
$obj = json_decode($json);
print $obj->posts->userinfo->fullname;
2 - convert json to an array using:
$arr = json_decode($json, true);
print $arr['posts']['userinfo']['fullname'];
Learn more about json_decode

Related Links

Laravel : sync ids issue
PHP - Dynamic Social Icon URLs in Wordpress
Symfony swiftmailer via smtp gmail on localhost openssl error
Data not being inserted into database?
Laravel Query builder - query not working but works in SQL console
PHPMailer does not work with Gmail SMTP [closed]
PHP exec / diff / No such file
How to assign new keys to a PHP associative array
How to fetch number of same values in PDO, PHP
Add & Remove <select></select> is not working dynamically for PHP + Jquery
I am trying to make the 3rd, 4th, 6th, 7th etc wordpress blog posts display in one news box. The pictures i have attached explains better
extract photo from rss with [enclosure] and [#attributes] and put in php
Symfony3 Facebook Login - redirect_uri URL gets converted to relative
php mysql select with different values
Connection falied: could not find driver
Storing database value into variable

Categories

HOME
visual-studio
knockout.js
workflow
qc
bots
plpgsql
braintree
amp
capistrano
stored-procedures
azure-graph-api
jquery-select2-4
multi-step
squirrel-sql
exe
playframework-2.0
xsl-fo
cpu-usage
pe
unity-container
sharedpreferences
exchange-server-2010
react-dnd
unordered-multimap
ipa
clipboard
hevc
bobo-browse.net
bonita
sfml
region
credit-card
john-the-ripper
cucumber-junit
custom-keyboard
apptentive
db2-luw
autoencoder
ftp-server
aurelia-cli
utf
slideshow
jacoco-maven-plugin
web-inspector
tinyioc
cordova-ios
gtk#
nrf51
controllers
e
p6spy
wininet
zynq
nsdata
dojo-build
timestamping
ibm-was
featuretoggle
wlanapi
green-threads
google-gdk
kotlin-android-extensions
camus
baseadapter
google-hangouts
famo.us
utf8-decode
nesper
code-complexity
code39
sql-view
hints
ax
unicoins
notin
proc-open
icenium
taskkill
kaazing
flymake
jmenu
channelfactory
libxslt
hungarian-notation
stretchblt

Resources

Encrypt Message