Pinterest RSS feed
generate a Pinterest RSS feed with PHP

Can I generate a Pinterest RSS feed with PHP?

Yes, you can easily generate a Pinterest RSS feed with PHP. Even if you want to build in another language you can easily do it. Because Important thing is the structure that converts RSS to Pinterest Pins. Below you can see a static RSS example for one pin.

IMPORTANT!

If you are only inerested in php source code then scroll down and go to Combine code for Pinterest RSS feed:
<rss version='2.0'>
<channel>
<title>Your desired title</title>
<link>Your rss file link</link>
<description>Your desired description</description>
<language>Your desired language </language>
   //Pin start from here
    <item xmlns:dc='ns:1'>
      <title>Pin Title</title>
      <link>Destination Link</link>
      <guid>Unique Id</guid>
      <pubDate>Wed, 29 Dec 2021 12:00:00 GMT</pubDate>
      <dc:creator>Pin Creater Name</dc:creator>
      <description>Pin Description</description>
      <enclosure url='pin_image_path'  type='image/png' />
      <category>Category Name</category>
   </item>
   //Pin end here
</channel>
</rss>

Now as we want a Pinterest RSS feed with PHP it means we need dynamically. So we can create new pins daily automatically from Rss to Pinterest. Let’s start step by step.

In the beginning, we need to make a database connection, then make one query, and fetch a result using any loop in my case I am using While Loop.

<?php
$server       = "localhost";
$user          = "root";
$password = "";
$db             = "blog";
$connection = new mysqli($server, $user, $password, $db);
	if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
	}
$query = "SELECT * FROM post ORDER BY created DESC";
$results = $connection->query($query);
   if ($results->num_rows > 0) {
     // main tags here
       while($result = $results->fetch_assoc()){
       //item tags here
      }
   }

In addition, we need to set the header as XML so Pinterest can identify our file format, XML version, XML encoding type, RSS version which is the main tag of our Pinterest RSS feed. And we need to add a Channel tag as per Pinterest requirements before While Loop.

<?php
header("Content-Type: text/xml;charset=iso-8859-1");
echo "<?xml version='1.0' encoding='UTF-8' ?>" . PHP_EOL;
echo "<rss version='2.0'>".PHP_EOL;
echo "<channel>".PHP_EOL;
   echo "<title> Any title</title>".PHP_EOL;
   echo "<link>Your file link</link>".PHP_EOL;
   echo "<description>Any description</description>".PHP_EOL;
   echo "<language>Language code</language>".PHP_EOL;
      while($result = $results->fetch_assoc()){
       // some more tag for Pins i will explain below
      }
echo '</channel>'.PHP_EOL;
echo '</rss>'.PHP_EOL;
?>

The above code contains almost all main tags. Let’s discuss other tags. Now our last part is left which is for our pins. This part will be under the while loop.

Combine code for Pinterest RSS feed:

<?php
$server      = "localhost";
$user          = "root";
$password      = "";
$db            = "blog";
$connection = new mysqli($server, $user, $password, $db);
	if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
	}
$query = "SELECT * FROM post ORDER BY created DESC";
$results = $connection->query($query);
   if ($results->num_rows > 0) {
     header("Content-Type: text/xml;charset=iso-8859-1");
     echo "<?xml version='1.0' encoding='UTF-8' ?>" . PHP_EOL;
     echo "<rss version='2.0'>".PHP_EOL;
        echo "<channel>".PHP_EOL;
        echo "<title> Any title</title>".PHP_EOL;
        echo "<link>Your file link</link>".PHP_EOL;
        echo "<description>Any description</description>".PHP_EOL;
        echo "<language>Language code</language>".PHP_EOL;
        while($result = $results->fetch_assoc()){
           $publish_Date = date("D, d M Y H:i:s T", strtotime($result["created"]));
           echo "<item xmlns:dc='ns:1'>".PHP_EOL;
				echo "<title>".$result['title']."</title>".PHP_EOL;
				echo "<link>".$result['link']."</link>".PHP_EOL;
				echo "<guid>".md5($result['id'])."</guid>".PHP_EOL;
				echo "<pubDate>".$publish_Date."</pubDate>".PHP_EOL;
				echo "<dc:creator>Tutorials Bites</dc:creator>".PHP_EOL;
				echo "<description><![CDATA[".substr($result["result"], 0, 300) ."]]></description>".PHP_EOL;
				echo "<enclosure url='image_path'  type='image/jpg' />".PHP_EOL;
		   echo "</item>".PHP_EOL;

        }
echo '</channel>'.PHP_EOL;
echo '</rss>'.PHP_EOL;
   }

TIPS:

We used PHP_EOL in each line because helps to indicate the endline character for the system.

We used [CDATA[ ]] too it means that the data in between these brackets only interpret as characters.

Read more about JQuery Multiselect Dropdown with custom icon

Read more about How to convert PHP Array to JavaScript Array

Leave a Reply