In my application I am pulling data from mysql and calling the bing distance matrix service. While I can get the call to work fine using postman, etc. Any method I have tried using various cURL methods and unirest have been unsuccessful. I get back...
JSON input could not be read
All that said, here is what I am doing...
This is some sample JSON I am using, which is $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"}
It passes in JSONLint beautifully and also works in postman.
$content is built from this base $jsonbase...
$jsonbase = array(
"origins" => [],
"destinations" => [],
"travelMode" => "driving",
);
Here is the bit I am using after I get my return from mysql...
$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);
I must be missing something, I just can't for the life of me figure it out. In postman, I have the header keys setup the same. and for the body I have chosen type as raw and content as JSON. Any help or guidance would be much appreciated. Thanks!
Figured it out via chat, the problem is that the API requires origins
and destinations
to be an array of lat/lng, but looking closely at the resulting JSON origins
wasn't because it was a single value in the original data.
So putting the single origin
tuple into an array solved it, resulting in this 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"
}