2016-08-29 15 views
0

Wordpress 피드를 내 외부 마젠타 사이트에 직접 표시하려고합니다. 이 결과를 얻기 위해 피드 버너를 사용하고 있습니다. 지금까지 나는 블로그 제목 & 링크를 표시 할 수있었습니다. 모양을 개선하기 위해 제목 옆에 블로그를 표시하고 싶습니다. 여기 XML로 RSS 피드에서 이미지 추출

는 하나의 피드 항목에 대한 XML 코드 내가 링크 한 다음

<?php $channel = new Zend_Feed_Rss('http://feeds.feedburner.com/golfoy/pxce'); ?> 
     <div class="block block-rss"> 

      <div class="block-content"> 
       <ol id="graybox-latest-news"> 

        <?php foreach ($channel as $item): ?> 
        <li><a href="<?php echo $item->link; ?>"><?php echo $item->description; ?></a></li> 
        <?php endforeach; ?> 
       </ol> 
      </div> 
     </div> 

와 블로그 제목을 표시하기위한 XML 피드를 구문 분석하는 데 사용하고있는 PHP 코드이다. 당신은 내가 전체 img 태그 또는 피드 항목의 설명 태그 안에 바로 "SRC"를 잡으려고 노력하고 this URL

<item> 
    <title>The grey-haired major winners.</title> 
    <link>http://blog.golfoy.com/the-grey-haired-major-winners/</link> 
    <pubDate>Mon, 29 Aug 2016 08:24:53 +0000</pubDate> 
    <guid isPermaLink="false">http://blog.golfoy.com/?p=980</guid> 

    <description> 
     <![CDATA[<img width="300" height="169" src="http://blog.golfoy.com/wp-content/uploads/2016/08/17_Jack_Nicklaus_20130805165206236_2560_1706.vresize.1200.675.high_.43-300x169.jpg" /> 
You are never too old to hit the greens and play a round of golf. In the history of men&#8217;s major championships, no golfer older than 48 (and just one golfer older than 46) has won. Have a look at the list of oldest men to have won a major: 1.Ben Hogan: The legend won [&#8230;]]]> 
    </description> 
    <content:encoded> 
    .... 
    </content:encoded> 
    </item> 

에서 전체 피드를 찾을 수 있습니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

+0

[PHP를 사용하여 HTML에서 img src, title 및 alt를 추출하는 방법은 무엇입니까?] (http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt- from-html-using-php) – revo

답변

0

xml로 설명을 선택하면 을 보내고 get_images로 보냅니다.

1 단계

download

변화는 함수 (URL_IMAGE, PATH_UPLOAD)를 정의 simple_html_dom.php 포함한다.

2 단계

함수 get_images ($ HTML)

require_once('simple_html_dom.php'); 

function get_images($html) 
    { 
     if (!empty($html)) 
     { 

      $post_dom = str_get_html($html); 
      $first_img = $post_dom->find('img'); 

      $image_list = array(); 
      foreach ($first_img as $image) 
      { 
       if(isset($image->src) AND $image->src != '') 
       { 
        $image = $image->src; 

        $filetype = strtolower(substr(strrchr($image,'.'),1)); 
        if (in_array($filetype,array('jpg','jpeg','png','gif'))) 
        { 
         $filename = 'image_'.time().'_'.rand(0000000,99999999).'.'.$filetype; 
         $up = file_put_contents(PATH_UPLOAD.$filename,file_get_contents($image)); 
         $size = filesize(PATH_UPLOAD.$filename); 
         list($width) = getimagesize(PATH_UPLOAD.$filename); 

         if(intval($size) >= 1024 AND $width >= 40) 
         { 
          $filename = URL_IMAGE.$filename; 
         } 
         else 
         { 
          unlink(PATH_UPLOAD.$filename); 
          $filename = ''; 
         } 

         array_push($image_list, "$image|$filename"); 
        } 
       } 
      } 

      return $image_list; 
     } 
     else 
     { 
      return array(); 
     } 
    } 

3 단계

선택 설명

$html = $feed->item->description; 
print_r(get_images($html)); 
+0

'simple_html_dom'과 같은 여분의 라이브러리를 사용하는 것은 성능이 현저하지 않습니다. – revo