【发布时间】:2016-08-29 21:43:45
【问题描述】:
这是我的代码
public function getAllCountries() {
$countries = array();
$response = $this->client->get($this->geoCountryAPIURL, [
'query' => ['username' => $this->username]
]);
$body = json_decode($response->getBody()->getContents());
if ($response->getStatusCode() == 200) {
if (array_key_exists('geonames', $body)) {
$dataRespone = $body->geonames;
// error or status response
foreach ($dataRespone as $country)
{
$countries[] = array(
'geonameId' => $country->geonameId,
'countryName' => $country->countryName
);
}
}
}
return $countries;
}
在我的 PHP 错误日志中,我收到此错误:
PHP 致命错误:无法在第 35 行使用 stdClass 类型的对象作为数组
第 35 行是
if ($response->getStatusCode() == 200) {
【问题讨论】:
-
var_dump($respons);会帮助你。 -
我正在了解国家/地区。我从 stdClass 错误中得到这个数组。任何方式我都可以通过 guzzle 做到这一点