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

Apache 2.2 + mod_fastcgi +php-fpm on Debian 6, Error 404 handling
PHP While loop to increment variable [closed]
PHP Default Parameters Overwriting Passed Values
PHPMailer and PrivateEmail sending delayed emails to Gmail
Run console command from another one
Woocommerce ajax add to cart animation
Symfony entity class seems not to be a managed Doctrine entity
Codeigniter, get value from exploded string
php - constructor of parent class is not being invoked from child class
Not receiving mail sent by mail() function in php [duplicate]
Laravel Default Auth not redirecting to correct path
Php, cache system with file_get_contents, file_put_contents: thread safe?
php wrap specific strings in different tags
Bootstrap: active class
Invalid argument supplied for foreach() in php need tip
how to insert two table related, in laravel 5.1

Categories

HOME
php
oracle
android-espresso
user-interface
knockout.js
ontouchlistener
yii
typeerror
jersey-2.0
drag-and-drop
graphql-js
selection
reportviewer
lc3
grocery-crud
multi-step
pushdown-automaton
amazon-elb
jodatime
jqxgrid
codefluent
clr
spyder
libgit2
gitkraken
kamailio
altium-designer
channel
mdns
instructions
steam-web-api
quickcheck
cloudera-quickstart-vm
microsoft-ui-automation
iscroll
windows-store
vungle-ads
permission-denied
spongycastle
dtd
email-forwarding
nitrousio
yargs
mifos
google-identity-toolkit
nclam
system.web.optimization
bilinear-interpolation
oci
jtag
jeasyui
jeditable
univocity
myfaces
bayesglm
in-app-billing
juniper-network-connect
smart-tv
asp.net-web-api-helppages
modern.ie
web-api
scala-macros
shell-extensions
incron
cout
mobile-country-code
git-reset
two.js
thruway
sqlbindparameter
zend-search-lucene
cpu-time
cloo
particle-swarm
vlab
bcdedit
semantic-merge
getopt-long
gamesalad
prototypal-inheritance
algol68
qt-mobility
canonicalization
nsconnection
explicit
zend-test
html-components
powergui
rtti

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile