Google Geolocation mithilfe von Cell Tower Info - Curl 400 Fehlerhafte Anfrage PHP
Ich versuche, mit @ Breiten- und Längengrad von den Informationen zum Mobilfunkmast zu erhalte Googles Geolocation-API. Es ist ein gültiger JSON mit Informationen wie MCC, MNC, cellId, lac usw. erforderlich. Meine PHP-Post-Anfrage sieht folgendermaßen aus.
<?php
header("Access-Control-Allow-Origin: *");
$mcc = $_POST["mcc"];
$mnc = $_POST["mnc"];
$cellId = $_POST["cellId"];
$lac = $_POST["lac"];
$post_array = array(
"cellId" => (int) $cellId,
"locationAreaCode" => (int) $lac,
"mobileCountryCode" => (int) $mcc,
"mobileNetworkCode" => (int) $mnc,
);
$post_data = json_encode(array('cellTowers' => array($post_array)));
echo $post_data;
$url = "https://www.googleapis.com/geolocation/v1/geolocate?key=".$api_key; // not including api key here but its there in my code
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => $post_data
));
$result = curl_exec($ch);
echo "Result: ".$result;
curl_close($ch);
?>
Ich erhalte jedoch die Fehlermeldung, dass die Antwort eine falsche Anfrage enthält. Der Fehler wird unten angezeigt.
Result: {
"error": {
"errors": [
{
"domain": "geolocation",
"reason": "invalidRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
Ich dachte, mein JSON hat nicht das richtige Format, aber es funktioniert mit der folgenden Befehlszeilenausführung. Das kann also nicht das Problem sein.
$ curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY"
Der obige Befehl im Terminal gibt die korrekte Länge und Breite mit demselben JSON in einer Datei an. Was mache ich falsch