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

Get invalid node libxml
unable to run codeception in virtual machine -Mink could not be found and loaded
PHP isset for GET variable in ternary operator [closed]
$_REQUEST and setcookie Deprecated
Sort array - digits, special chars, letters [duplicate]
How to get data from facebook Ads on the Graph API?
Admin panel not accessible in wordpress [duplicate]
Yii framework blank page issue for many times
PHP assign same value to multiple variables in this case
Finding string position with for loop
Incorrect SQL syntax, no obvious mistakes [duplicate]
display message with array data in confirmation box [closed]
PHP Composer behind http proxy
Zend Framework 2 SOAP AutoDiscover and complex types
I can't parse the data from a JSON encoded string
count the number of rows inserted using PHPEXCEL

Categories

HOME
jboss
azure-functions
functional-programming
vagrant
adfs3.0
applescript
transactions
apk
android-emulator
magento-2.0
cypher
azure-mobile-services
video-streaming
graphql-js
colors
documentum
clish
bootstrap-switch
multi-step
jquery-file-upload
c#-7.0
wsf
stackexchange.redis
pdf.js
firemonkey-style
raima
ipa
hdmi
android-service
dotcms
phpstorm-2016.1
overwrite
adminer
telephonymanager
task-parallel-library
classnotfoundexception
vb.net-to-c#
access-denied
fluid-mac-app-engine
apache-toree
jenkins-slave
adblock
spongycastle
libreoffice-writer
spring.net
punctuation
android-mediarecorder
asp.net-web-api-routing
pecl
.when
dymola
boost-compute
cefpython
ndk-build
ftp-client
msbuild-4.0
cdo.message
gnucash
odp.net-managed
sharepoint-apps
textblock
flipclock
seadragon
voronoi
ioexception
outlook.com
nette
org-babel
ampersand
webproject
ocmockito
cakephp-2.2
flurry-analytics
windows-rt
object-code
lumia-imaging-sdk
android-2.3-gingerbread
nachos
senchatouch-2.4
hyperloglog
xmi
profiles
colon
infinite
non-ascii-characters
iirf
quickgraph
statechart
axacropdf
delphi-xe4
days
cyrillic
simplecov
towers-of-hanoi
zend-pdf
cakeyframeanimation
radscheduler
play2-mini
winsxs
simile
ios-4.2
odac
urchin
iphone-sdk-4.3
asdoc
gtktextview
motif
twitter-feed
suggestbox
chronic
webclient.uploaddata

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App