【问题标题】:How to save multiple images using 'file_get_contents' at once using xml tag如何使用 xml 标签一次使用“file_get_contents”保存多个图像
【发布时间】:2016-08-17 00:38:36
【问题描述】:

我将不胜感激。我正在尝试使用“file_get_contents”从 xml 提要中保存图像。但问题是,它现在只需要一张图片,而且在输入 URL 时也是如此。我正在寻找一个代码,它会在每次刷新提要时自动保存提要中的图像,但只保存新图像,而不是已经存储的图像。另外,问题是,该链接位于“ContentItem”标签内,不容易提取

这是我的 XML 提要代码:

      <xml>
     <channel><title></title>
     <link>http://www.yournewssite.com</link>
     <description>gossip</description>
     <item>
     <title>title of article</title>
     <link>http://yourwebsite.com/</link>
     <description><![CDATA[<img src=http://yourwebsite.com/i.php?k=d88d4e2b336966b5389837832 width=100 height=100>
    <BR>article content<BR>]]></description>
    <ContentItem Href="http://yourwebiste.com/i.php?k=d88d4e2b336966b538983783230051c7">
    <MediaType FormalName="Picture" />
    <MimeType FormalName="image/jpg" />
    <Property FormalName="caption" value="Nick Carter" />
    </ContentItem>
    </xml>

PHP 代码:

   <?php       
   $doc = new DOMDocument();
   $doc->load('http://yourwebsite.com/clients/d51b83e5/index.xml');

   $xpath = new DOMXpath($doc);
   $nodeLists = $xpath->query ('//ContentItem[@Href]');

   $arrFeeds = array();
   foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array (
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
    'ContentItem'=>$nodeLists->item(0)->getAttribute('Href'),
    );
    array_push($arrFeeds, $itemRSS);
    }


    echo file_get_contents("http://yourwebsite.com/clients/d51b83e5/index.xml");
    $url = '';
    $img = 'C:\xampp\htdocs\trial\images\image.jpg';
    file_put_contents($img, file_get_contents($url));
  ?>

非常感谢您对此提供的帮助。提前致谢。

【问题讨论】:

    标签: php mysql xml image url


    【解决方案1】:

    您检索&lt;ContentItem Href&gt; 的代码有点扭曲,但对我来说似乎可行。

    你可以这样简化:

    $nodeLists = $xpath->query ('//ContentItem[@Href]');
    foreach( $nodeLists as $node )
    {
        $itemRSS = array (
            'title'       => $node->parentNode->getElementsByTagName('title')->item(0)->nodeValue,
            'ContentItem' => $node->getAttribute('Href'),
        );
        /* Following syntax is equivalent to array_push() when you add only one item to array: */
        $arrFeeds[] = $itemRSS;
    }
    

    要仅检索新图像,我们只能帮助您猜测。如果 URL 中的图片名称是唯一的,您可以使用它来保存图片并检查已保存的文件。

    在您的示例中,链接是:

    http://yourwebiste.com/i.php?k=d88d4e2b336966b538983783230051c7
    

    因此,使用以下代码:

    $url = parse_url( 'http://yourwebiste.com/i.php?k=d88d4e2b336966b538983783230051c7' );
    parse_str( $url['query'], $query );
    

    $query['k'] 你有:

    d88d4e2b336966b538983783230051c7
    

    如果你想用它作为唯一的文件名,你可以这样写代码来保存图像:

    $localPath = '/Your/Path/To/Image/Directory';
    foreach( $arrFeeds as $feed )
    {
        $url = parse_url( $feed['ContentItem'] );
        parse_str( $url['query'], $query );
        $localName = "$localPath/{$query['k']}.jpg";
        if( file_exists( $localName ) )
        {
            echo "Already downloaded: '$localName'".PHP_EOL;
        }
        else
        {
            $data = file_get_contents( $feed['ContentItem'] );
            if( ! $data )
            {
                echo "Error downloading '{$feed['ContentItem']}'".PHP_EOL;
            }
            else
            {
                if( ! file_put_contents( $localName, $data ) )
                {
                    echo "Error saving '$localName'".PHP_EOL;
                }
                else
                {
                    echo "'$localName' successful saved".PHP_EOL;
                }
            }
        }
    }
    

    【讨论】:

    • 非常感谢!这完美地工作。我真的很感谢你。 :)
    猜你喜欢
    • 1970-01-01
    • 2022-08-16
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多