file_get_contents() 从 url 而不是 json 返回原始数据
file_get_contents() returning raw data from url instead of json
这个问题之前已经被问过很多次,但是 none 的解决方案似乎有效。我想使用 file_get_contents()
return JSON 来自 this url 的数据,但它 returning JSON 原始格式的数据(string ).
这是我的代码:
$data = file_get_contents('https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?origins=27.696694861033567,83.46625594498187&destinations=28.233514383842977,83.98651076641227&travelMode=driving&key=bingmaps_api_key&distanceUnit=km');
dd($data);
我试过使用 curl 但结果是一样的 return JSON 原始格式的数据 (string)
$url = 'https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?origins=27.696694861033567,83.46625594498187&destinations=28.233514383842977,83.98651076641227&travelMode=driving&key=ingmaps_api_key&distanceUnit=km';
if (!function_exists('curl_init')) {
die('The cURL library is not installed.');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
dd($output);
我想要的输出
它给出的输出
JSON 只不过是一个字符串。 file_get_contents 或 cURL 将始终是 return 字符串,您必须使用 json_decode 方法解码字符串,该方法应该为您提供 JSON 对象。
这个问题之前已经被问过很多次,但是 none 的解决方案似乎有效。我想使用 file_get_contents()
return JSON 来自 this url 的数据,但它 returning JSON 原始格式的数据(string ).
这是我的代码:
$data = file_get_contents('https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?origins=27.696694861033567,83.46625594498187&destinations=28.233514383842977,83.98651076641227&travelMode=driving&key=bingmaps_api_key&distanceUnit=km');
dd($data);
我试过使用 curl 但结果是一样的 return JSON 原始格式的数据 (string)
$url = 'https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?origins=27.696694861033567,83.46625594498187&destinations=28.233514383842977,83.98651076641227&travelMode=driving&key=ingmaps_api_key&distanceUnit=km';
if (!function_exists('curl_init')) {
die('The cURL library is not installed.');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
dd($output);
我想要的输出
它给出的输出
JSON 只不过是一个字符串。 file_get_contents 或 cURL 将始终是 return 字符串,您必须使用 json_decode 方法解码字符串,该方法应该为您提供 JSON 对象。