如何在curl PHP中正确提取JSON响应?

时间:2018-12-07 10:37:55

标签: php json curl

我在php中有此代码,用于从API检索登录身份验证。我的问题是,我无法从此代码中正确提取curl响应。

$baseURL = "http://127.0.0.1:8000/api";
$loginURL = $baseURL."/login";

$data = array(
        'email'=>'sample@email.com',
        'password'=>'sample'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$preAuthToken = curl_exec($ch);
curl_close($ch);
echo "Response: ".$preAuthToken;

页面中显示的内容类似于tjis

 {
    "access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xMjcuMC4wLjE6ODAwMFwvYXBpXC9sb2dpbiIsImlhdCI6MTU0NDE3ODU3NSwiZXhwIjoxNTQ0MTgyMTc1LCJuYmYiOjE1NDQxNzg1NzUsImp0aSI6IjRSVEhiNGx3aUxoYTlJZjciLCJzdWIiOjUsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEiLCJtZXJjaGFudENvZGUiOiI4NTYyMzJiYSIsImhhc2hlZElkIjoiOHYiLCJsb2dpblR5cGUiOiJtZXJjaGFudFVwbG9hZCIsIjJmYUVuYWJsZWQiOmZhbHNlfQ.gnpmb8HbeRK7FlG5b-GDh-CMR5oBA5qXWfjcEtZZTs8",
    "token_type":"bearer",
    "expires_in":3600
 }Response: 1

我很困惑为什么回显的响应是Response: 1? 如何正确提取JSON响应并获取access_token字段?

0 个答案:

没有答案