【问题标题】:how to display images in array using php,if url is provided [closed]如果提供了 url,如何使用 php 在数组中显示图像[关闭]
【发布时间】:2015-02-22 16:10:24
【问题描述】:

,
如果提供了 url,如何使用 php 在数组中显示图像。这是我从 Accessing main picture of wikipedia page by API 中挑选的完整代码

<html>
<head></head><body><?php 
function makeCall($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($curl);
}

function wikipediaImageUrls($url) {
    $imageUrls = array();
    $pathComponents = explode('/', parse_url($url, PHP_URL_PATH));
    $pageTitle = array_pop($pathComponents);
    $imagesQuery = "http://en.wikipedia.org/w/api.php?action=query&titles={$pageTitle}&prop=images&format=json";
    $jsonResponse = makeCall($imagesQuery);
    $response = json_decode($jsonResponse, true);
    $imagesKey = key($response['query']['pages']);
    foreach($response['query']['pages'][$imagesKey]['images'] as $imageArray) {
        if($imageArray['title'] != 'File:Commons-logo.svg' && $imageArray['title'] != 'File:P vip.svg') {
            $title = str_replace('File:', '', $imageArray['title']);
            $title = str_replace(' ', '_', $title);
            $imageUrlQuery = "http://en.wikipedia.org/w/api.php?action=query&titles=Image:{$title}&prop=imageinfo&iiprop=url&format=json";
            $jsonUrlQuery = makeCall($imageUrlQuery);
            $urlResponse = json_decode($jsonUrlQuery, true);
            $imageKey = key($urlResponse['query']['pages']);
            $imageUrls[] = $urlResponse['query']['pages'][$imageKey]['imageinfo'][0]['url'];
        }
    }
    return $imageUrls;
?>
  for($i=0; $i<10; $i++) {
  <img src="<?php echo $imageUrls[$i]; ?>" />
  <?php
}

print_r(wikipediaImageUrls('http://en.wikipedia.org/wiki/Saturn_%28mythology%29'));
print_r(wikipediaImageUrls('http://en.wikipedia.org/wiki/Hans-Ulrich_Rudel'));

?>
</body>
</html>

【问题讨论】:

    标签: php json


    【解决方案1】:

    这是您的代码,经过 2 处修改,使其按您想要的方式工作:

    <html>
    <head></head><body><?php 
    function makeCall($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($curl);
    }
    
    function wikipediaImageUrls($url) {
        $imageUrls = array();
        $pathComponents = explode('/', parse_url($url, PHP_URL_PATH));
        $pageTitle = array_pop($pathComponents);
        $imagesQuery = "http://en.wikipedia.org/w/api.php?action=query&titles={$pageTitle}&prop=images&format=json";
        $jsonResponse = makeCall($imagesQuery);
        $response = json_decode($jsonResponse, true);
        $imagesKey = key($response['query']['pages']);
        foreach($response['query']['pages'][$imagesKey]['images'] as $imageArray) {
            if($imageArray['title'] != 'File:Commons-logo.svg' && $imageArray['title'] != 'File:P vip.svg') {
                $title = str_replace('File:', '', $imageArray['title']);
                $title = str_replace(' ', '_', $title);
                $imageUrlQuery = "http://en.wikipedia.org/w/api.php?action=query&titles=Image:{$title}&prop=imageinfo&iiprop=url&format=json";
                $jsonUrlQuery = makeCall($imageUrlQuery);
                $urlResponse = json_decode($jsonUrlQuery, true);
                $imageKey = key($urlResponse['query']['pages']);
                $imageUrls[] = $urlResponse['query']['pages'][$imageKey]['imageinfo'][0]['url'];
            }
        }
        // return $imageUrls; /* 1st modif : do not return anything here */
        for($i=0; $i<count($imageUrls); $i++) {
            echo '<img src="'.$imageUrls[$i].'" />';
        }
    }
    /* 2nd modif : no print, just a call to your method because you already display stuff from the method */
    wikipediaImageUrls('http://en.wikipedia.org/wiki/Saturn_%28mythology%29');
    wikipediaImageUrls('http://en.wikipedia.org/wiki/Hans-Ulrich_Rudel');
    
    ?>
    </body>
    </html>
    

    【讨论】:

    • 假设这不是 OP 想要的,这个答案将是完全错误的!
    • 可能是真的,那么希望这个问题将通过更多信息来完成
    • 我是 stackoverflow 的新手。它阻止我添加对代码的更改,我不知道如何添加代码。
    • 您不能编辑问题吗?否则请在评论中尝试...
    • @AnilaMalik 也许你想读这个:stackoverflow.com/help/mcve
    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多