Tags: PHP
Here's a method I wrote to extract Pinterest photos by the RSS feed. It takes a string with the API URL, and returns an array of image sources.
/**
* Get all image URLs from the Pinterest Feed by URL
*
* @param $url, string
* @return $all_images, array of strings
*/
public function getPinterestFeed($url){
$doc = new DOMDocument();
$doc->load($url);
$nodes = $doc->getElementsByTagName('title');
$nodes2 = $doc->getElementsByTagName('link');
$title = $nodes->item(0)->nodeValue;
$link = $nodes2->item(0)->nodeValue;
$Rss = new Rss;
try {
$feed = $Rss->getFeed($url, Rss::XML);
//$items = $Parser->getItems();
$i=0;
$all_images = [];
foreach ($feed as $item) {
$item_desc = new domDocument;
$item_desc->loadHTML($item['description']);
$item_desc->preserveWhiteSpace = false;
$images = $item_desc->getElementsByTagName('img');
foreach ($images as $image) {
$all_images[] = $image->getAttribute('src');
}
}
} catch (Exception $e) {
return $e->getMessage();
}
return [
'all_images' => $all_images
];
}
©2023, kirillsimin.com