file_get_contents() returning raw data from url instead of json [duplicate]










-1















This question already has an answer here:



  • How to output JSON data correctly using PHP

    7 answers



  • Display JSON as HTML [closed]

    12 answers



This question have been asked many times before but none of the solution seems to works. I want to return JSON data from this url using file_get_contents() but it returning JSON data in raw format (string).



Here is my code:



$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);


I have tried using curl but result is same it also return JSON data in raw format (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);


Output I want
Produced output



Output it gives
Required output










share|improve this question















marked as duplicate by mario php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 14:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    The question linked as dupicate is not what he was asking for I guess. You're rather looking for the application/json header. See: stackoverflow.com/questions/4064444/… - also, you should remove your API keys.
    – maio290
    Nov 12 at 14:17











  • dd() is Laravel's debug output function, right? Then yes, it'll just show the literal string as is. Usually you'd decode the JSON, and do something with it. If this script is just about playback, then an echo and the right MIME type would make your browser JSON prettifier engage (if this is what you want, then it's perhaps worth mentioning).
    – mario
    Nov 12 at 14:20










  • i will disable this key after a solution
    – Milan Tarami
    Nov 12 at 14:23











  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28
















-1















This question already has an answer here:



  • How to output JSON data correctly using PHP

    7 answers



  • Display JSON as HTML [closed]

    12 answers



This question have been asked many times before but none of the solution seems to works. I want to return JSON data from this url using file_get_contents() but it returning JSON data in raw format (string).



Here is my code:



$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);


I have tried using curl but result is same it also return JSON data in raw format (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);


Output I want
Produced output



Output it gives
Required output










share|improve this question















marked as duplicate by mario php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 14:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    The question linked as dupicate is not what he was asking for I guess. You're rather looking for the application/json header. See: stackoverflow.com/questions/4064444/… - also, you should remove your API keys.
    – maio290
    Nov 12 at 14:17











  • dd() is Laravel's debug output function, right? Then yes, it'll just show the literal string as is. Usually you'd decode the JSON, and do something with it. If this script is just about playback, then an echo and the right MIME type would make your browser JSON prettifier engage (if this is what you want, then it's perhaps worth mentioning).
    – mario
    Nov 12 at 14:20










  • i will disable this key after a solution
    – Milan Tarami
    Nov 12 at 14:23











  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28














-1












-1








-1








This question already has an answer here:



  • How to output JSON data correctly using PHP

    7 answers



  • Display JSON as HTML [closed]

    12 answers



This question have been asked many times before but none of the solution seems to works. I want to return JSON data from this url using file_get_contents() but it returning JSON data in raw format (string).



Here is my code:



$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);


I have tried using curl but result is same it also return JSON data in raw format (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);


Output I want
Produced output



Output it gives
Required output










share|improve this question
















This question already has an answer here:



  • How to output JSON data correctly using PHP

    7 answers



  • Display JSON as HTML [closed]

    12 answers



This question have been asked many times before but none of the solution seems to works. I want to return JSON data from this url using file_get_contents() but it returning JSON data in raw format (string).



Here is my code:



$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);


I have tried using curl but result is same it also return JSON data in raw format (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);


Output I want
Produced output



Output it gives
Required output





This question already has an answer here:



  • How to output JSON data correctly using PHP

    7 answers



  • Display JSON as HTML [closed]

    12 answers







php laravel-5.6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:48

























asked Nov 12 at 14:12









Milan Tarami

316




316




marked as duplicate by mario php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 14:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by mario php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 14:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    The question linked as dupicate is not what he was asking for I guess. You're rather looking for the application/json header. See: stackoverflow.com/questions/4064444/… - also, you should remove your API keys.
    – maio290
    Nov 12 at 14:17











  • dd() is Laravel's debug output function, right? Then yes, it'll just show the literal string as is. Usually you'd decode the JSON, and do something with it. If this script is just about playback, then an echo and the right MIME type would make your browser JSON prettifier engage (if this is what you want, then it's perhaps worth mentioning).
    – mario
    Nov 12 at 14:20










  • i will disable this key after a solution
    – Milan Tarami
    Nov 12 at 14:23











  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28













  • 1




    The question linked as dupicate is not what he was asking for I guess. You're rather looking for the application/json header. See: stackoverflow.com/questions/4064444/… - also, you should remove your API keys.
    – maio290
    Nov 12 at 14:17











  • dd() is Laravel's debug output function, right? Then yes, it'll just show the literal string as is. Usually you'd decode the JSON, and do something with it. If this script is just about playback, then an echo and the right MIME type would make your browser JSON prettifier engage (if this is what you want, then it's perhaps worth mentioning).
    – mario
    Nov 12 at 14:20










  • i will disable this key after a solution
    – Milan Tarami
    Nov 12 at 14:23











  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28








1




1




The question linked as dupicate is not what he was asking for I guess. You're rather looking for the application/json header. See: stackoverflow.com/questions/4064444/… - also, you should remove your API keys.
– maio290
Nov 12 at 14:17





The question linked as dupicate is not what he was asking for I guess. You're rather looking for the application/json header. See: stackoverflow.com/questions/4064444/… - also, you should remove your API keys.
– maio290
Nov 12 at 14:17













dd() is Laravel's debug output function, right? Then yes, it'll just show the literal string as is. Usually you'd decode the JSON, and do something with it. If this script is just about playback, then an echo and the right MIME type would make your browser JSON prettifier engage (if this is what you want, then it's perhaps worth mentioning).
– mario
Nov 12 at 14:20




dd() is Laravel's debug output function, right? Then yes, it'll just show the literal string as is. Usually you'd decode the JSON, and do something with it. If this script is just about playback, then an echo and the right MIME type would make your browser JSON prettifier engage (if this is what you want, then it's perhaps worth mentioning).
– mario
Nov 12 at 14:20












i will disable this key after a solution
– Milan Tarami
Nov 12 at 14:23





i will disable this key after a solution
– Milan Tarami
Nov 12 at 14:23













when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
– Milan Tarami
Nov 12 at 14:28





when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
– Milan Tarami
Nov 12 at 14:28













1 Answer
1






active

oldest

votes


















0














JSON is nothing but a string. file_get_contents or cURL will always return string, you have to decode the string using json_decode method which should give you the JSON object.






share|improve this answer




















  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28











  • Looks like you don't have json extension enabled in your php configuration. you should install ext-json and enable it.
    – AppDevD
    Nov 12 at 14:30


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














JSON is nothing but a string. file_get_contents or cURL will always return string, you have to decode the string using json_decode method which should give you the JSON object.






share|improve this answer




















  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28











  • Looks like you don't have json extension enabled in your php configuration. you should install ext-json and enable it.
    – AppDevD
    Nov 12 at 14:30
















0














JSON is nothing but a string. file_get_contents or cURL will always return string, you have to decode the string using json_decode method which should give you the JSON object.






share|improve this answer




















  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28











  • Looks like you don't have json extension enabled in your php configuration. you should install ext-json and enable it.
    – AppDevD
    Nov 12 at 14:30














0












0








0






JSON is nothing but a string. file_get_contents or cURL will always return string, you have to decode the string using json_decode method which should give you the JSON object.






share|improve this answer












JSON is nothing but a string. file_get_contents or cURL will always return string, you have to decode the string using json_decode method which should give you the JSON object.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 14:23









AppDevD

813




813











  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28











  • Looks like you don't have json extension enabled in your php configuration. you should install ext-json and enable it.
    – AppDevD
    Nov 12 at 14:30

















  • when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
    – Milan Tarami
    Nov 12 at 14:28











  • Looks like you don't have json extension enabled in your php configuration. you should install ext-json and enable it.
    – AppDevD
    Nov 12 at 14:30
















when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
– Milan Tarami
Nov 12 at 14:28





when i do json_decode() it gives me error Call to undefined function GuzzleHttpjson_decode()
– Milan Tarami
Nov 12 at 14:28













Looks like you don't have json extension enabled in your php configuration. you should install ext-json and enable it.
– AppDevD
Nov 12 at 14:30





Looks like you don't have json extension enabled in your php configuration. you should install ext-json and enable it.
– AppDevD
Nov 12 at 14:30




這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Museum of Modern and Contemporary Art of Trento and Rovereto

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully