2016-04-12 6 views
1

私は、次を使用する場合:PHPプルと表示値

$homepage = file_get_contents('http://www.website.com/api/v1/members/102210'); 
echo $homepage; 

次のようにデータが表示されます。

{"user":{"profile":{"Id":{"Label":"User Id","Value":102210},"User":{"Label":"User Name","Value":"tom"} 

私は別のポストにここに発見された以下のことを試してみましたユーザー以外のページは空白のままです。

$curl = curl_init(); 

curl_setopt_array($curl, array(
    CURLOPT_URL => "http://www.website.com/api/v1/members/102210", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "GET", 
    CURLOPT_HTTPHEADER => array(
     "cache-control: no-cache" 
    ), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 


$response = json_decode($response, true); //because of true, it's in an array 
echo 'User: '. $response['User']['User Name']; 
+0

問題は何ですか? Jsonをフォーマットしてバディを貼り付けてもよろしいですか? –

+0

'$ response' varを印刷できますか? 'print_r($ response);' –

答えて

2

JSONデータを誤解しています。 jsonオブジェクトのルートにはUserキーはなく、その内にはUser Nameキーもありません。あなたvisualize the JSON、これは、それがどのように見えるのとき:あなたが探しているように見える何

json

です:

echo 'User: ' . $response['user']['profile']['User']['Value']; 

私はあなたがあれば、カールに切り替えるだろう、なぜわからないんだけどfile_get_contentメソッドが動作します。

$homepage = file_get_contents('http://www.website.com/api/v1/members/102210'); 
$response = json_decode($homepage, true); 

echo 'User: ' . $response['user']['profile']['User']['Value']; 
+1

ツリー図の作成にどのツールを使用していますか?すべてに感謝と幸運。 – shellter

+0

@shellter jsoneditoronline.orgは、答えの例へのリンクを追加しました。 – Oldskool

+0

@Oldskoolそれは空白のページだけを表示する –

関連する問題