我已經從API獲取Json數據。請告訴我如何調用API並打印特定的現場數據。我的API是如何從json獲取特定的字段數據
https://api.themoviedb.org/3/movie/popular?api_key=52385ff92cb9105e52d86c4786293ce8&page=1
我已經從API獲取Json數據。請告訴我如何調用API並打印特定的現場數據。我的API是如何從json獲取特定的字段數據
https://api.themoviedb.org/3/movie/popular?api_key=52385ff92cb9105e52d86c4786293ce8&page=1
$url="https://api.themoviedb.org/3/movie/popular?api_key=52385ff92cb9105e52d86c4786293ce8&page=1";
使用cURL
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump json data
var_dump(json_decode($result, true));
$result = file_get_contents($url);
// Will dump json data
var_dump(json_decode($result, true));
關於使用什麼代碼如下?
$url = "https://api.themoviedb.org/3/movie/popular?api_key=52385ff92cb9105e52d86c4786293ce8&page=1";
$json = file_get_contents($url);
$obj = json_decode($json);
var_dump($obj);
使用Ajax後,您可以調用URL來獲取josn數據如下。從jquery.com下載最新的jquery。
您可以使用http://jsonlint.com/網站來驗證和格式化json數據,以便您輕鬆理解json數據的結構。
$.ajax({
url: 'https://api.themoviedb.org/3/movie/popular?api_key=52385ff92cb9105e52d86c4786293ce8&page=1',
type: 'POST',
success: function(resp)
{
var jsonObj=$.parseJSON(resp); //converting json string to jsonobject
var results=jsonObj.results; //to gest result array from json code
var page_number=jsonObj.page; // to get page number from json data
}
})