dongyou4411 2017-05-18 23:06
浏览 64
已采纳

PHP-如何从提取的URL IMGS重新显示网站上的图像

So I'm a bit stuck, and I've been given various solutions, none of which work. Any hotshot PHP folks out there? Here's the deal, I'm trying to get an image to display on my website, from another website, that has a randomly generated IMG. Though I'm actually trying to do this off a personal art site of mine, this example will serve perfectly. http://commons.wikimedia.org/wiki/Special:Random/File

A random image page with an image on it pops up with that link. Now, I'd like to display THAT random image, or whatever image comes up, on another site. The two possible solutions I have encountered is gathering an array of URL LINKS from a given link. And then re displaying that array as images on another site, like a: < a href="https

The code I get back from what I'm talking about looks like this:

Array
(
[0] => https ://kfjhiakwhefkiujahefawef/awoefjoiwejfowe.jpg
[1] => https ://oawiejfoiaewjfoajfeaweoif/awoeifjao;iwejfoawiefj.png

)

Instead of the print out however, I'd like the actual images displayed, well specifically array [0], but one thing at a time. The code that's actually doing this is:

<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/

$url = 'http://commons.wikimedia.org/wiki/Special:Random/File';

// Fetch page
$string = FetchPage($url);

// Regex that extracts the images (full tag)
$image_regex_src_url = '/<img[^>]*'.

'src=[\"|\'](.*)[\"|\']/Ui';

preg_match_all($image_regex, $string, $out, PREG_PATTERN_ORDER);

$img_tag_array = $out[0];

echo "<pre>"; print_r($img_tag_array); echo "</pre>";

// Regex for SRC Value
$image_regex_src_url = '/<img[^>]*'.

'src=[\"|\'](.*)[\"|\']/Ui';

preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);

$images_url_array = $out[1];

echo "<pre>"; print_r($images_url_array); echo "</pre>";

// Fetch Page Function

function FetchPage($path)
{
$file = fopen($path, "r"); 

if (!$file)
{
 exit("The was a connection error!");
} 

$data = '';

 while (!feof($file))
{
// Extract the data from the file / url

$data .= fgets($file, 1024);
}
return $data; 
}




for($i=0; $i<count($arr1); $i++) {
echo '<img src="'.$arr1[$i].'">';
}

?>

Solution two, Use a file_get_contents command. Which is this:

<?php

$html = 
file_get_contents("http://commons.wikimedia.org/wiki/Special:Random/File");

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$image_src =  $xpath->query('//div[contains(@class,"fullImageLink")]/a/img')
[0]->getAttribute('src') ;
echo "<img src='$image_src'><br>";


?>

However, there's unfortunately an error message I get: Fatal error: Cannot use object of type DOMNodeList as array in /home/wilsons888/public_html/wiki.php on line 11. Or, if I remove a "}" at the end, I just get a blank page.

I have been told that the above code will work, but with openssl extension included. Problem is, I have no idea how to do this. (I'm very new to PHP). Anyone know how to plug it in, so to speak? Thank you so much! I feel like I'm close, just missing the last element.

  • 写回答

1条回答 默认 最新

  • douyong1885 2017-05-18 23:57
    关注

    I was able to load the random image, and "print it" as an image directly (so you can embed the php file directly on the IMG tag) using this code:

    <?php
        $html = file_get_contents("http://commons.wikimedia.org/wiki/Special:Random/File");
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        $remoteImage = $dom->getElementById("file")->firstChild->attributes[0]->textContent;
        header("Content-type: image/png");
        header('Content-Length: ' . filesize($remoteImage));
        echo file_get_contents($remoteImage);
    ?>
    

    Get a new file called showImage.php and put this code in it:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <img src="test.php">
    </body>
    </html>
    

    Next, go to your browser and get the showImage.php path, and will show a random image fromt he site you asked...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c