duanjingwei7239 2013-06-23 08:08
浏览 55
已采纳

回显数组中的每个图像

im using the Wikipedia API to get images and a description for my site, and I need your help with the images. Currently I'm using this:

$wurl = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info|images&titles=".$wsearch."&format=json&explaintext&redirects&inprop=url&indexpageids&exintro";

$wjson = file_get_contents($wurl);
$wdata = json_decode($wjson);
$wpageid = $wdata->query->pageids['0'];
if ($wpageid == -1)
{
}
else
{
echo"
<div id='images'>
&nbsp;&nbsp;<p style='padding-bottom:8px;border-bottom: 6px solid #333;width:100%;'>Images for <b>$search</b>:</br></p>
<div class='resultbox random-color-".rand(1,3)."'>
";

foreach($wdata->query->pages->$wpageid->images as $iimages)
{
$ititle = $iimages->title;
$iname = $ititle;   
$ifilename = str_replace(" ", "_",$iname);
$idigest = md5($ifilename);
$ifolder = $idigest[0] . '/' . $idigest[0] . $idigest[1] . '/' .  urlencode($iname);
$iurl = 'http://upload.wikimedia.org/wikipedia/commons/' . $ifolder;
echo"
<img style='height:100px;' src='$irurl'>
";
}
echo"
</div>
</div>
";
}  

To echo each individual image from the url:

However, I can't get anything back, or at least nothing is displaying on my page. I'm very new to JSON, so can someone explain to me what I'm doing wrong? I hope you can tell me and future viewers to do it, because its really annoying... lol, anyway thanks in advance.

EDIT:

<div class="resultbox random-color-3">
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" 4="" 4a="" commons-logo.svg'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" f="" fe="" crystal_clear_app_browser.png'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" e="" e5="" eric_schmidt_at_the_37th_g8_summit_in_deauville_037.jpg'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" b="" b6="" factory_1b.svg'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" a="" a4="" flag_of_the_united_states.svg'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" 2="" 20="" google-logo.svg'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" b="" b7="" google1998.png'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" 6="" 69="" google_appliance.jpg'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" 5="" 54="" google_mountain_view_campus_dinosaur_skeleton_%27stan%27.jpg'=""/>
<img src'http:="" upload.wikimedia.org="" wikipedia="" commons="" 8="" 8c="" google_mountain_view_campus_garden.jpg'=""/>
</div>

it seems to be removing all the /'s and not replacing the spaces with underscores... any ideas?

  • 写回答

1条回答 默认 最新

  • dqy012345 2013-06-23 08:33
    关注

    You have an error I can see in your code...

      $iurl = 'http://upload.wikimedia.org/wikipedia/commons/' . $ifolder;
      echo"<img style='height:100px;' src='$irurl'>";
    

    Here you have $iurl but then youre calling $irurl in the src of the iamge...that could just be a typo on StackOverflow though.....

    But thats not your real problem. Your problem is that the wikipedia url takes the image, without the File: prepend, so you have to remove it.

    This should work for you...

    foreach($wdata->query->pages->$wpageid->images as $iimages)
    {
    $name = $iimages->title;
    $filename = str_replace(' ', '_',$name);
    $filename = str_replace("File:","",$filename);
    $digest = md5($filename);
    $folder = $digest[0].'/'.$digest[0].$digest[1].'/'.urlencode($filename);
    $url = 'http://upload.wikimedia.org/wikipedia/commons/'.$folder;
    
    echo "<img src='$url'">;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧