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

HybridAuth: Authentication failed! LinkedIn returned an invalid Token
Weird behavior of php datetime object when attempting to retrieve the week number
php collect dates between 2 dates and create entry for each
How do I capture require errors + error_handlers
jQuery - Search Mysql for results, then display within table, but also have a click link for each record
How to disable the link dashboard for subscribers?
Remove only these type of chars …… using php preg_replace
Localization of strings in the database [duplicate]
Disable CategoryTree Sidebar for anonymous users
CSS background-image does not show
Return To Previous Page - Mobile HTML
XML node extraction with PHP and simplexml_load_file
Zend OPcache and HHVM both together [closed]
php - check multiple date ranges for gaps within wider range
Form empty itself without doing anything
Redirect loop in Chrome

Categories

HOME
qt
xcode
facebook-messenger
opencv-python
bots
concurrency
tcsh
apk
google-webmaster-tools
gentelella
navigation
save
onvif
fireloop
jquery-select2-4
freertos
amazon-elb
user-defined-types
exe
xsl-fo
deepfreeze
home
azure-machine-learning
yii2-basic-app
dashdb
hdl
acl
device
hdmi
sap-lumira
miniprofiler
django-import-export
sendmessage
amazon-cloudtrail
phpstorm-2016.1
memory-fences
nsmutablearray
universal
osx-lion
adminer
nppexec
pdfminer
subclassing
unification
assemblies
get-event-store
hdiv
wysihtml5
section508
viewmodel
cloudwatch
rm
deployd
clrs
dtd
bpms
annotatorjs
petsc
multi-touch
livereload
declarative
mnesia
diawi
gdl
decidable
mousehover
ora-04091
android-studio-import
gql
angular-amd
deque
device-admin
zend-mail
dmake
webautomation
screwturn
sensormanager
colon
application-server
revmob
coalesce
quickgraph
jacob
chartfx
flv
towers-of-hanoi
image-formats
thejit
netstream
delphi-prism
datacontracts
representation
kohana-auth
servlet-container
dip
bass
self-tracking-entities
ppc
projectpier
inversion
accumulator
chronic
rootkit

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile