如何仅获取此字符串中的访问令牌?

时间:2019-06-13 05:54:00

标签: php oauth-2.0

{"access_token":"OeB89uEWZZzLPLMIsVBvJtIlnObM1sX7NNNWqwtjdzI.8LlxO2skN028HmcMw8F_BDiqEj9kDRfdVWc9f02WmiY","expires_in":43199,"scope":"","token_type":"bearer"}

如何仅通过access_token访问它,如下所示:

$str = "OeB89uEWZZzLPLMIsVBvJtIlnObM1sX7NNNWqwtjdzI.8LlxO2skN028HmcMw8F_BDiqEj9kDRfdVWc9f02WmiY";

2 个答案:

答案 0 :(得分:0)

以这种方式获取令牌,

<?php

$t = '{"access_token":"OeB89uEWZZzLPLMIsVBvJtIlnObM1sX7NNNWqwtjdzI.8LlxO2skN028HmcMw8F_BDiqEj9kDRfdVWc9f02WmiY","expires_in":43199,"scope":"","token_type":"bearer"}';

$a = json_decode($t);

print_r($a->access_token);

?>

输出

OeB89uEWZZzLPLMIsVBvJtIlnObM1sX7NNNWqwtjdzI.8LlxO2skN028HmcMw8F_BDiqEj9kDRfdVWc9f02WmiY

答案 1 :(得分:0)

您可以使用json_decode()

<?php

$json = '{"access_token":"OeB89uEWZZzLPLMIsVBvJtIlnObM1sX7NNNWqwtjdzI.8LlxO2skN028HmcMw8F_BDiqEj9kDRfdVWc9f02WmiY","expires_in":43199,"scope":"","token_type":"bearer"}';

$array = json_decode($json,true);

print_r($array['access_token']);

输出:-{https://3v4l.org/MPuCn