douhuangjie4503 2019-07-30 03:19
浏览 72
已采纳

从API调用获取响应时如何从数组中获取变量?

After successfully logging in for a 3rd party API access, using the following code:

$response = Requests::post('https://apidomain.com/v3/account/login/api', $headers, $data);

When I print:

echo '<pre>'; print_r($response); echo '</pre>';

I get the following:

Requests_Response Object
(
    [body] => {"access_token":"blabla",".issued":"2019-07-30T02:47:14.4326684Z",".expires":"2019-08-01T02:47:14.4326686Z"}
    [raw] => HTTP/1.1 200 OK
etc...

Now I want to access the access_token as a variable so that I can pass for further processing.

I tried to access like: $access_token=$response[body'] and $access_token=$response[1][body']

Unfortunately nothing worked.

How do I solve the problem?

  • 写回答

1条回答 默认 最新

  • dongpiao1983 2019-07-30 04:35
    关注

    Using @catcon 's proposal, I could use the following code and get it worked:

    $token = json_decode($response->body, true)['access_token']; echo $token;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀