我沒有在控制檯中發現任何錯誤,但我想要的只是thumbnail_url,它是php文件中的json。在控制檯中顯示它是一個數組字符串,我該如何訪問它?使用ajax從API獲取圖像
EDITS: 我能夠執行我想要的輸出,但問題是最後一個對象是唯一一個在瀏覽器中輸出。
<?php
//Set API user and password
$API_USER = 'username';
$API_PASS = 'password';
//Set parameters to make cURL call to Duda
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'sampleurl');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $API_USER.':'.$API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//execute cURL call and get template data
$output = curl_exec($ch);
//check for errors in cURL
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
//echo $output;
print_r($output);
?>
這裏是我們的HTML和Ajax請求:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Ajax Preview</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
$(function() {
var $data = $('#content');
$.ajax({
method:'GET',
dataType:'json',
url:'https://syncslider-ehnoxx07.c9users.io/temp.json',
success: function (data) {
$.each(data, function(i,data){
$data.html('<img src=' + data.thumbnail_url + '>');
});
}
});
});
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>
你也可以在這裏顯示數組輸出嗎? – vher2
你已經發布了你的API和API憑證..我不會在公開帖子中推薦它。 –
這些Api憑據是虛擬的,但感謝您對其進行編輯。 下面是php截圖: https://postimg.org/image/4svg44ubn/ 和網絡中的html輸出。 https://postimg.org/image/b5vobivxr/ – camdev