从PHP调用Bing距离矩阵服务遇JSON输入无法读取问题求助
Hey there, let's dig into why your cURL and Unirest calls are failing even though Postman works—super frustrating when that happens, right? Here are the most likely culprits and actionable fixes to try:
1. Missing or Incorrect Content-Type Header
Postman automatically sets the correct header for JSON requests, but cURL and Unirest don't always do this by default. Bing's Distance Matrix API explicitly requires Content-Type: application/json to parse your request body properly.
- For cURL: Add this header flag:
curl -X POST "YOUR_BING_API_URL" \ -H "Content-Type: application/json" \ -d "$content" \ -d "key=YOUR_API_KEY" - For Unirest (PHP example):
$response = Unirest\Request::post( "YOUR_BING_API_URL", array("Content-Type" => "application/json"), // Critical line $content );
2. Hidden Special Characters or Malformed JSON from MySQL
Even if your base $jsonbase is valid, data pulled from MySQL might introduce invisible characters (like line breaks, tabs, or non-UTF-8 characters) that break the JSON structure when sent via cURL/Unirest. Postman often cleans these up automatically.
- Try re-encoding your JSON after building it to standardize it:
// If using PHP, this will strip invalid characters and reformat $clean_content = json_encode(json_decode($content), JSON_PRETTY_PRINT); - Print the final
$contentto your console, copy it directly into a cURL command, and test—if it works, the issue is how your code is passing the variable to cURL/Unirest.
3. Terminal Quote Escaping Problems (for cURL)
If you're running cURL directly in a terminal, unescaped double quotes in your JSON will cause the shell to truncate the request body.
- Linux/macOS: Wrap your entire JSON in single quotes:
curl -X POST "YOUR_BING_API_URL" \ -H "Content-Type: application/json" \ -d '{"origins": [{"latitude": 47.6062, "longitude": -122.3321}], "destinations": [{"latitude": 40.7128, "longitude": -74.0060}]}' \ -d "key=YOUR_API_KEY" - Windows Command Prompt: Escape double quotes with
"":curl -X POST "YOUR_BING_API_URL" ^ -H "Content-Type: application/json" ^ -d "{\"origins\": [{\"latitude\": 47.6062, \"longitude\": -122.3321}], \"destinations\": [{\"latitude\": 40.7128, \"longitude\": -74.0060}]}" ^ -d "key=YOUR_API_KEY"
4. Unirest Request Body Handling
Some Unirest versions require explicit handling of the request body to avoid sending it as form data instead of raw JSON.
- Ensure you're passing the JSON string directly as the body parameter, not wrapping it in an array or form fields. For example, in Node.js:
unirest.post('YOUR_BING_API_URL') .headers({'Content-Type': 'application/json'}) .send($content) // Pass raw JSON string here .end(function (response) { console.log(response.body); });
5. Validate the Full Request URL
Double-check that your Bing API key is appended as a URL parameter (not included in the JSON body) and that there are no typos in the endpoint URL. Postman might have auto-appended the key, but your cURL/Unirest code could be missing it.
- Correct URL format:
https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?key=YOUR_API_KEY
6. Test with a Minimal Valid JSON
Start with the smallest possible JSON payload that works in Postman, then send it via cURL/Unirest. If it succeeds, gradually add back the data pulled from MySQL to isolate which part of the data is causing the failure.
内容的提问来源于stack exchange,提问作者jkuhns5

