它的功能很简单,通过 cURL 请求数据并获取 JSON。我只想在 JSON 中使用数据,但为什么它是很多 stdclass 对象。如何简化此代码?
<?php
function movieRequest()
{
$url = "http://api.com/content/movies?format=json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$headers[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$data = movieRequest();
$data = (array) json_decode($data);
print_r(get_object_vars(get_object_vars($data['categories'])['Access-Channel'])['category']); //Need this data but it still has stdClass Object.
?>
太多了' get_object_vars
'。有更好的获取数据的方法吗?