从 PHP 调用 Bing 距离矩阵 - 无法读取 JSON 输入
Call Bing Distance Matrix from PHP - JSON input could not be read
在我的应用程序中,我从 mysql 中提取数据并调用 bing 距离矩阵服务。虽然我可以使用邮递员等让电话正常工作。我尝试使用各种 cURL 方法和 unirest 的任何方法都没有成功。我回来了...
JSON input could not be read
综上所述,这就是我正在做的...
这是我正在使用的一些示例 JSON,它是 $content...
{"origins":{"latitude":"41.654570","longitude":"-71.49605"},"destinations":[{"latitude":"41.172536","longitude":"-71.555274"},{"latitude":"41.18259","longitude":"-71.567168"},{"latitude":"41.341878","longitude":"-71.695282"},{"latitude":"41.356934","longitude":"-71.63798"},{"latitude":"41.361308","longitude":"-71.625706"},{"latitude":"41.347763","longitude":"-71.67328"},{"latitude":"41.373887","longitude":"-71.665345"},{"latitude":"41.488602","longitude":"-71.38332"},{"latitude":"41.493772","longitude":"-71.137993"},{"latitude":"41.486615","longitude":"-71.246164"}],"travelMode":"driving"}
它在 JSONLint 中传递得很漂亮,也适用于邮递员。
$content 是从这个基础 $jsonbase 构建的...
$jsonbase = array(
"origins" => [],
"destinations" => [],
"travelMode" => "driving",
);
这是我从 mysql...
得到 return 后使用的位
$url = "https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix";
$content = json_encode($jsonbase);
$headers = array('Content-Type' => 'application/json',
'Accept' => 'application/json',
'Content-Length' => strlen ($content),
'key' => 'THISISMYKEY');
$body = Unirest\Request\Body::Json($jsonbase);
$response = Unirest\Request::post($url, $headers, $body);
我一定是遗漏了什么,我就是一辈子都弄不明白。在邮递员中,我有相同的 header 键设置。对于 body,我选择了原始类型和内容 JSON。任何帮助或指导将不胜感激。谢谢!
通过聊天弄明白了,问题是 API 需要 origins
和 destinations
是 lat/lng 的数组,但仔细观察结果JSON origins
不是因为它是原始数据中的单个值。
所以将单个 origin
元组放入数组中解决了这个问题,结果是 JSON:
{
"origins": [{
"latitude": "41.654570",
"longitude": "-71.49605"
}],
"destinations": [
{
"latitude": "41.172536",
"longitude": "-71.555274"
},
{
"latitude": "41.18259",
"longitude": "-71.567168"
},
{
"latitude": "41.341878",
"longitude": "-71.695282"
},
{
"latitude": "41.356934",
"longitude": "-71.63798"
},
{
"latitude": "41.361308",
"longitude": "-71.625706"
},
{
"latitude": "41.347763",
"longitude": "-71.67328"
},
{
"latitude": "41.373887",
"longitude": "-71.665345"
},
{
"latitude": "41.488602",
"longitude": "-71.38332"
},
{
"latitude": "41.493772",
"longitude": "-71.137993"
},
{
"latitude": "41.486615",
"longitude": "-71.246164"
}
],
"travelMode": "driving"
}
在我的应用程序中,我从 mysql 中提取数据并调用 bing 距离矩阵服务。虽然我可以使用邮递员等让电话正常工作。我尝试使用各种 cURL 方法和 unirest 的任何方法都没有成功。我回来了...
JSON input could not be read
综上所述,这就是我正在做的...
这是我正在使用的一些示例 JSON,它是 $content...
{"origins":{"latitude":"41.654570","longitude":"-71.49605"},"destinations":[{"latitude":"41.172536","longitude":"-71.555274"},{"latitude":"41.18259","longitude":"-71.567168"},{"latitude":"41.341878","longitude":"-71.695282"},{"latitude":"41.356934","longitude":"-71.63798"},{"latitude":"41.361308","longitude":"-71.625706"},{"latitude":"41.347763","longitude":"-71.67328"},{"latitude":"41.373887","longitude":"-71.665345"},{"latitude":"41.488602","longitude":"-71.38332"},{"latitude":"41.493772","longitude":"-71.137993"},{"latitude":"41.486615","longitude":"-71.246164"}],"travelMode":"driving"}
它在 JSONLint 中传递得很漂亮,也适用于邮递员。
$content 是从这个基础 $jsonbase 构建的...
$jsonbase = array(
"origins" => [],
"destinations" => [],
"travelMode" => "driving",
);
这是我从 mysql...
得到 return 后使用的位$url = "https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix";
$content = json_encode($jsonbase);
$headers = array('Content-Type' => 'application/json',
'Accept' => 'application/json',
'Content-Length' => strlen ($content),
'key' => 'THISISMYKEY');
$body = Unirest\Request\Body::Json($jsonbase);
$response = Unirest\Request::post($url, $headers, $body);
我一定是遗漏了什么,我就是一辈子都弄不明白。在邮递员中,我有相同的 header 键设置。对于 body,我选择了原始类型和内容 JSON。任何帮助或指导将不胜感激。谢谢!
通过聊天弄明白了,问题是 API 需要 origins
和 destinations
是 lat/lng 的数组,但仔细观察结果JSON origins
不是因为它是原始数据中的单个值。
所以将单个 origin
元组放入数组中解决了这个问题,结果是 JSON:
{
"origins": [{
"latitude": "41.654570",
"longitude": "-71.49605"
}],
"destinations": [
{
"latitude": "41.172536",
"longitude": "-71.555274"
},
{
"latitude": "41.18259",
"longitude": "-71.567168"
},
{
"latitude": "41.341878",
"longitude": "-71.695282"
},
{
"latitude": "41.356934",
"longitude": "-71.63798"
},
{
"latitude": "41.361308",
"longitude": "-71.625706"
},
{
"latitude": "41.347763",
"longitude": "-71.67328"
},
{
"latitude": "41.373887",
"longitude": "-71.665345"
},
{
"latitude": "41.488602",
"longitude": "-71.38332"
},
{
"latitude": "41.493772",
"longitude": "-71.137993"
},
{
"latitude": "41.486615",
"longitude": "-71.246164"
}
],
"travelMode": "driving"
}