首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从guzzle响应中获取url不起作用

从guzzle响应中获取url不起作用
EN

Stack Overflow用户
提问于 2018-06-23 03:02:04
回答 1查看 884关注 0票数 0

下面的代码如下:

代码语言:javascript
复制
$url = json_decode($getClient->getBody()->getContents());
dd($url);

显示:

代码语言:javascript
复制
{#302 ▼
  +"output": {#304 ▼
    +"url": "https://...▶"
  }
}

我想将url存储在一个变量中,但使用$urlLink = $url['url'];时,它显示了错误:

代码语言:javascript
复制
Cannot use object of type stdClass as array

$urlLink = $url->url;也显示了一个错误:

代码语言:javascript
复制
Undefined property: stdClass::$url

你知道为什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-23 03:11:28

这是因为url位于output对象内部。

代码语言:javascript
复制
$response = $getClient->getBody()->getContents();

$url = null;
if(!empty($response)) {
  $response = json_decode($response);

  $url = !empty($response->output->url) ?: $response->output->url : '';
}

// Handle the redirection this way.
// If $url == null then that means no response from API.
// If $url == '' then the file hasn't been completely uploaded.

这就是应该怎么做的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50994190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档