Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer json data to html with php?

Tags:

json

php

How to transfer json data to html with php?

$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey"

when I type the url in browser, it return {"offset" : "0" , "results" : [{"body" : "A guide to cultural and recreational goings-on in and around the Hudson Valley. ...}]} how to put the json body data into html? I mean like this echo '<div class="body"></div>';

like image 990
cj333 Avatar asked Feb 27 '26 10:02

cj333


1 Answers

You first need to get the file. You should use curl for this. In the example below I used the file_get_contents() function, you might want to replace it. Use json_decode() to parse the JSON for you.

$json = file_get_contents($url);
$data = json_decode($json);
echo $data->results[0]->body;

This will echo A guide to cultural....

like image 103
svens Avatar answered Mar 01 '26 01:03

svens