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

Skip controller loading in url for codeigniter
Profile page that displays user information from MySQL database
How to use updateMany query in PHP with MongoDB
json_encode not working on prodution server
SSLv3 Error in Woocommerce with Paypal Gateway
Laravel #include via ajax
Laravel 500 error when using with nginx
How do I redirect with dev_appserver?
Eager load using where relationship on hasManyThrough
How do i fix subquery returns more than 1 row error
Laravel Eloquent: How to store model values before saving?
Error:Not Registered - GCM Push Notification
LDAP Finding Members of a group PHP
error while inserting data mysql database,
Working form - how to plug in PHPMailer for attachment function only
How to assign an array to variable in php

Categories

HOME
openshift
plesk
systemd
plpgsql
braintree
adobe
yql
dronekit-python
malware
drag-and-drop
sonicwall
reselect
mixpanel
cocos2d-android
cloudsim
home
clr
akka-persistence
uislider
generator
pyopencl
tracing
hdmi
code-climate
basic-authentication
source-insight
sql-execution-plan
kendo-listview
assertions
extbase
reset
swift2.3
head.js
cordova-ios
skip-lists
android-sharing
palindrome
patternlab.io
emacs25
mpmovieplayercontroller
pbx
dmarc
pyaudio
frame-grab
android-async-http
django-redis
wepay
cortana
python-cffi
android-mapview
360-degrees
flow-js
web-api
tkx
xhprof
jquery-mobile-flipswitch
lmax
visual-c++-2012
webmethod
radius-protocol
jchartfx
lsa
rautomation
ember-table
objective-c-runtime
sunstudio
chefspec
cyrillic
icenium
telerik-ajax
boost-foreach
upshot
grails-validation
libxslt
downgrade
main-method
downcasting
rakudo
efs

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App