2017-05-16 20 views
0

私は実際にApiの世界では新しく、私はCurl Requestを使用してデータをフェッチして返信するApiで作業しています。フェッチでは、以下の情報が渡されます。Jsonでエンコードされた配列PHPを使用して分割

{"posts":{"userinfo":{"fullname":"Precious Tom","user_name":"Kendrick","email":"[email protected]","gender":"Male","country":"Nigeria","city":"Port Harcourt","state":"Rivers","year":"1997","month":"9","day":"6"}}} 

コンテンツタイプ:text/html;文字セット= UTF-8

HTTPコード:アレイを分割200

は私の問題のように見える、ごめん、この質問のHSは以前に頼まれて、しかし、私はあなたの助けを必要としてください場合。おかげさまで

は、詳細については、ここでは私のカールリクエスト

<?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); 
?> 

私は変数ではない直接の配列を渡すことになりますです。 jsonを変換する -

1: しようとした直接の配列は、あなたのコードは、あなたが2つのオプションをした、それは動作を停止...

+0

詳細については、こちらをご覧くださいので、問題 – JYoThI

+0

は、あなたがこれまでに – Kuru

+0

ますprint_r(json_decode( '{ "投稿を" 試されている任意のコードを表示するものである:jsonを使用してarrayに変換します{"userinfo":{"fullname": "Precious Tom"、 "user_name": "Kendrick"、 "email": "[email protected]"、 "gender": "男性"、 "国" 「都市」、「ポートハーコート」、「州」、「川」、「年」、「1997」、「月」:「9」、「日」:「6」}}} '、真)); – JYoThI

答えて

1
$city_names = json_decode('{"posts":{"userinfo":{"fullname":"Precious Tom","user_name":"Kendrick","email":"[email protected]","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] => [email protected] 
        [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']; 
+0

ちょうど編集後の投稿、@Exprator親切にチェックして助けてください。ありがとう... –

+0

私のansも編集しました – Exprator

0

変数を渡すには、細かなバリを働きましたobjectに:

$obj = json_decode($json); 
print $obj->posts->userinfo->fullname; 

2 - :

$arr = json_decode($json, true); 
print $arr['posts']['userinfo']['fullname']; 

json_decode

関連する問題