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
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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.