2017-09-16 20 views
0

curlレスポンスからaccess_token値を取得しようとしています。 [0]のキー値は、{"access_token":"430f8a7d4f721a9e51e3558689ff28ec592923d2","expires_in":3600,"token_type":"Bearer","scope":null}のようにレンダリングされます。PHPがキー値の中からカール配列の結果を取得する

しかし、access_tokenの内部に入れ子になっている値が必要です(430f8a7d4f721a9e51e3558689ff28ec592923d2)。

出力は、これを使用してレンダリングされます。

$cmd="curl -u testuser:123456 http://localhost/oauth2/server/token.php -d 'grant_type=client_credentials'"; 
exec($cmd,$result); 

echo '<pre>'; print_r($result[0]); echo '</pre>'; 

このことについてどのように行くかもしれませんか?

Btw、これは機密データではありません。私は単にoauth2を試しています。

答えて

1

結果にjson_decode関数を使用してください。

<?php 
$json = '{"access_token":"430f8a7d4f721a9e51e3558689ff28ec592923d2","expires_in":3600,"token_type":"Bearer","scope":null}'; 

// Decode the JSON into an object 
$decodedJson = json_decode($json); 
$accessToken = $decodedJson->access_token; 
var_dump($decodedJson,$accessToken); 

// Decode the JSON into an array (note the true on the decode) 
$decodedJson = json_decode($json,true); 
$accessToken = $decodedJson['access_token']; 
var_dump($decodedJson,$accessToken); 
+0

これは素晴らしいです。私はjsonを解読することを考えなかった。しかし、私はあなたのスニペットを試して、それはちょうど息子の結果を投げ捨てて、結果をエンコードしませんでした。私はこれを悩ましました。トークンをきれいにするようです: '$ json = $ result [0]; $ json = json_decode($ json、true); echo $ json ['access_token']; ' – rniller

0

json結果をエンコードして、サブセットを見つけます。

​​
関連する問題