Im使用netbeans 8.0.2和wampserver 3.0.0来运行php。到目前为止一切都很好。
当试图向bungie的API发出返回JSON对象的curl请求时,接收到的对象为null。问题是,我试着在中运行相同的代码http://phpfiddle.org/它确实有效,所以现在我不知道问题出在哪里。
PHP代码:
$apiKey = 'xxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.bungie.net/platform/destiny/{myplatform}/Stats/GetMembershipIdByDisplayName/{mygamertag}/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Key: ' . $apiKey));
$json = json_decode(curl_exec($ch));
echo $json->Response;
Curl已经启用,php运行良好,所以我现在一无所知,因为我不知道问题出在哪里。
我在echo命令中得到的错误是"正在尝试获取非对象的属性"。
您可以使用var_dump
:
var_dump(json_decode($json));
或print
,例如(来自文档):
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
或类似于打印的回声。。。