问题描述
我用在我的博客冰图片搜索的API。
我的请求的响应:
I'm using Bing image search API in my blog.The response of my request:
stdClass Object
(
[d] => stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[__metadata] => stdClass Object
(
[uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='Kitchen'&Market='en-us'&$skip=0&$top=1
[type] => ImageResult
)
[ID] => a40b8c85-8a6b-45a8-bce2-c07b16a942e6
[Title] => Our Kitchen Remodel is Complete!!! @ A Well Dressed Home
[MediaUrl] => http://awelldressedhome.com/wp-content/uploads/2010/10/Kitchen-31.jpg
[SourceUrl] => http://awelldressedhome.com/496-our-kitchen-remodel-is-complete/
[DisplayUrl] => awelldressedhome.com/496-our-kitchen-remodel-is-complete
[Width] => 4000
[Height] => 3000
[FileSize] => 5062458
[ContentType] => image/jpeg
和我得到这个答复50个结果(50种不同的图片和网址)。
我有一个PHP code这给我的第一张图像的链接在我的回应:
And I get 50 results(50 different images and URLs) in this response.I have a php code which give me the link of first image in my response:
<?php
$context = stream_context_create($data);
$response = file_get_contents($requestUri, 0, $context);
$response=json_decode($response);
$response = $response->d->results[0]->MediaUrl;
echo "<pre>";
print_r($response);
echo "</pre>";
echo '
<td>
<img src="'.$response.'" height="100" weight="100">
</td>
';
echo("</ul>");
?>
此外,我做了一个简单的按钮,这给了我这是我从我的反应更早得到这个第一个图像:
Also I made a simple button, which gives me this first image which I got earlier from my response:
<div class="form-group">
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML ="<?php echo "<img src=".$response." height='100' width='100'/>"?>"
}
</script>
</div>
所以现在的问题是:如何才能得到新的图像,当我点击我的按钮,每一次?
对不起,我的英语和感谢帮助。
So the question is: How can I get new image each time when I click my button?Sorry for my English and thanks for help.
推荐答案
您首先需要确保编写的URL转换为JavaScript:
You will first need to make sure to write your url's into javascript:
<?php
$urlList = array();
foreach($response->d->results as $r){
$urlList[] = $r->MediaUrl;
}
?>
<script>
var urls = <?php echo json_encode($urlList); ?>;
</script>
然后就可以使用这些链接在你的函数:
and then you can use those url's in your function:
<div class="form-group">
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
var currentIndex = 0;
function myFunction(){
document.getElementById("demo").innerHTML = "<img src='" + urls[currentIndex++ % urls.length] + "' height='100' width='100'/>";
}
</script>
</div>
这篇关于更改按钮后点击图片链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!