Thursday, 12 October 2017

PHP Pinterest RSS Feed PARSING

class XmlToJson {
  public function Parse( $url ) {
    $fileContents = file_get_contents( $url );
    $fileContents = str_replace( array(
      "\n",
      "\r",
      "\t"
    ), '', $fileContents );
    $fileContents = trim( str_replace( '"', "'", $fileContents ) );
    $simpleXml = simplexml_load_string( $fileContents );
    $json = json_encode( $simpleXml );
    return $json;
  }
}

function viral_pinterest() {
  $xml   = new XmlToJson;
  $feed  = json_decode( $xml->Parse( "https://www.pinterest.com/codifyclub/feed.rss/" ) );
  $items = $feed->channel->item;
  foreach( $items as $item ) :
 
   // print_r($item);
   if(is_string($item->title)) echo $item->title;
    echo '<br/>';
   
    echo $item->link;
    echo '<br/>';
 
    $doc = new DOMDocument();
    @$doc->loadHTML($item->description);
    $img_tags = $doc->getElementsByTagName('img');
    foreach ($img_tags as $img_tag) {
       echo $img_path = $img_tag->getAttribute('src');
    }
   

  endforeach;
}

viral_pinterest();

No comments:

Post a Comment