第一步是檢查您從API獲得的響應。
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
if(get_http_response_code('https://www.googleapis.com/geolocation/v1/geolocate?key=Jsddds6s6sdht43asdsaASasta8962') != "200"){
echo "Error parsing api";
}else{
file_get_contents('https://www.googleapis.com/geolocation/v1/geolocate?key=Jsddds6s6sdht43asdsaASasta8962');
}
參考Dev Guid for Response
或者,您可以嘗試使用此 -
$str = file_get_contents('https://www.googleapis.com/geolocation/v1/geolocate?key=Jsddds6s6sdht43asdsaASasta8962');
$json = json_decode($str, true);
因此,這應該給你一個JSON字符串,然後你可以通過解碼操作它。
謝謝你指出這一點。通常我們會忽略最簡單的通知。 –