How to Auto Post on Pinterest with PHP?

Auto post on Pinterest using php Script Code Download: You can implement PHP Pinterest Auto Post with images feature in your web application using its API. In this tutorial I will discuss its PHP Pinterest Auto Post with images implementation.

Free Live Chat for Any Issue

PHP Pinterest Auto Post with images

In this tutorial we will show you how to post automatically on Pinterest using PHP and Pinterest API. You just have to create a Pinterest App account and get your Username and need a Pinterest Password to help to post on Pinterest.You may also like login with facebook using PHP.

Automate posting to Pinterest with PHP

To Post On Pinterest Automatically It Takes Only Two Steps:-

Pinterest.class.php

<?php
class Pinterest {


const SUCCESS = 0;
 const INVALID_LOGIN = -1;
 const NOT_LOGGED_IN = -2;
 const MISSING_PIN_URL = -10;
 const MISSING_PIN_DESCRIPTION = -11;
 const MISSING_PIN_IMAGE_PREVIEW = -12;
 const UNABLE_TO_PIN = -13;
 const BAD_REPIN_URL = -14;
 const REPIN_URL_NOT_FOUND = -15;
 const UNABLE_TO_REPIN = -16;
 const IMAGE_DOESNT_EXIST = -20;
 const UNABLE_TO_CREATE_IMAGE_PREVIEW = -21;
 const UNABLE_TO_GET_BOARDS = -30;
 const UNABLE_TO_GET_ACCOUNT_NAME = -31;
 const UNABLE_TO_DELETE = -40;
 const NO_CURL = -100;


 protected $cookie_jar = "";
 protected $csrf_token = "";
 protected $is_logged_in = false;
 public $pin_url = "";
 public $pin_description = "";
 public $pin_image_preview = "";

 public $boards = array();
 public $last_pin_id = 0;
 function __construct($cookie_path = null) {

 if( !in_array('curl', get_loaded_extensions()) )
 return Pinterest::NO_CURL;
 if( $cookie_path ) {
 $this->cookie_jar = $cookie_path;
 if( file_exists($this->cookie_jar) ) {
 $this->csrf_token = Pinterest::get_csrf_token($this->cookie_jar);
 $this->is_logged_in = true;
 $this->get_boards();


}


} else
 $this->cookie_jar = tempnam(sys_get_temp_dir(), "cookies");  good


}


 


 private $login_url = "https:www.pinterest.com/resource/UserSessionResource/create/";


function login($username, $password) {

 $data_json = array(
 "options" => array(
 "username_or_email" => $username,
 "password" => $password
 ),
 "context" => array()
 );

 $post = array(
 "source_url" => "/login/",
 "data" => json_encode($data_json, JSON_FORCE_OBJECT),
 "module_path" => "App()>LoginPage()>Login()>Button(class_name=primary, text=Log In, type=submit, size=large)"
 );


 $data_info = array();
 foreach( $post as $k => $v )
 $data_info[] = "{$k}=" . urlencode($v);


$post_string = join("&", $data_info);



 $post_string = Pinterest::fix_encoding($post_string);




 $my_curl = \curl_init($this->login_url);
 \curl_setopt_array($my_curl, array(
 CURLOPT_COOKIEJAR => $this->cookie_jar,
 CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0",
 CURLOPT_REFERER => "https:www.pinterest.com/login/",
 CURLOPT_HTTPHEADER => array(
 'Host: www.pinterest.com',
 'Accept: application/json, text/javascript, */*; q=0.01',
 'Accept-Language: en-US,en;q=0.5',
 'DNT: 1',
 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
 'X-Pinterest-AppState: active',
 'X-CSRFToken: 1234',
 'X-NEW-APP: 1',
 'X-APP-VERSION: 04cf8cc',
 'X-Requested-With: XMLHttpRequest',
 'Cookie: csrftoken=1234;'
 ),
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => $post_string,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_SSL_VERIFYPEER => false  remove me later
 ));



 $res = \curl_exec($my_curl);
 \curl_close($my_curl);



 if( json_decode($res) === null ) {
 $this->is_logged_in = false;
 return Pinterest::INVALID_LOGIN;
 } else {
 $this->csrf_token = Pinterest::get_csrf_token($this->cookie_jar);
 $this->is_logged_in = true;
 $this->get_boards();
 return Pinterest::SUCCESS;
 }


}



 function is_logged_in() {
 return $this->is_logged_in;
 }



 private $get_boards_url = "https:www.pinterest.com/resource/BoardPickerBoardsResource/get/";


public function get_boards() {


 Can't do anything if we're not logged in
 if( !$this->is_logged_in )
 return Pinterest::NOT_LOGGED_IN;



 $data_json = array(
 "options" => array(
 "filter" => "all",
 "field_set_key" => "board_picker"
 ),
 "context" => array()
 );


 $get = array(
 "source_url" => "/pin/create/bookmarklet/?url=",
 "pinFave" => "1",
 "description" => "",
 "data" => json_encode($data_json, JSON_FORCE_OBJECT)
 );


 $get_arr = array();
 foreach( $get as $k => $v )
 $get_arr[] = "{$k}=" . urlencode($v);


$get_string = join("&", $get_arr);


 $get_string = Pinterest::fix_encoding($get_string);


 $my_curl = \curl_init($this->get_boards_url . "?{$get_string}");
 \curl_setopt_array($my_curl, array(
 CURLOPT_COOKIEFILE => $this->cookie_jar,
 CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0",
 CURLOPT_REFERER => "https:www.pinterest.com/pin/create/bookmarklet/?url=&pinFave=1&description=",
 CURLOPT_HTTPHEADER => array(
 'Host: www.pinterest.com',
 'Accept: application/json, text/javascript, */*; q=0.01',
 'Accept-Language: en-US,en;q=0.5',
 'DNT: 1',
 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
 'X-Pinterest-AppState: active',
 'X-NEW-APP: 1',
 'X-APP-VERSION: 04cf8cc',
 'X-Requested-With: XMLHttpRequest'
 ),
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_SSL_VERIFYPEER => false  remove me later
 ));



 $res = \curl_exec($my_curl);
 $json = json_decode($res, TRUE);
 \curl_close($my_curl);



 if( $json === null ) {
 return Pinterest::UNABLE_TO_GET_BOARDS;
 } else {



 if( isset($json['resource_response']['data']['all_boards']) ) {



 foreach( $json['resource_response']['data']['all_boards'] as $board )
 $this->boards[$board['name']] = $board['id'];


return Pinterest::SUCCESS;


} else
 return Pinterest::UNABLE_TO_GET_BOARDS;


}


}


 private $get_account_name_url = "https:www.pinterest.com/";
 public function get_account_name() {


 Can't do anything if we're not logged in
 if( !$this->is_logged_in )
 return Pinterest::NOT_LOGGED_IN;


 $my_curl = \curl_init($this->get_account_name_url);
 \curl_setopt_array($my_curl, array(
 CURLOPT_COOKIEFILE => $this->cookie_jar,
 CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0",
 CURLOPT_REFERER => "",
 CURLOPT_HTTPHEADER => array(
 'Host: www.pinterest.com',
 'Accept: application/json, text/javascript, */*; q=0.01',
 'Accept-Language: en-US,en;q=0.5',
 'DNT: 1',
 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
 'X-Pinterest-AppState: active',
 'X-NEW-APP: 1',
 'X-APP-VERSION: 04cf8cc',
 'X-Requested-With: XMLHttpRequest'
 ),
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_SSL_VERIFYPEER => false  remove me later
 ));

 $res = \curl_exec($my_curl);
 $json = json_decode($res, TRUE);
 \curl_close($my_curl);


 if( $json === null ) {
 return Pinterest::UNABLE_TO_GET_ACCOUNT_NAME;
 } else {

 if( isset($json['resource_data_cache'][1]['resource']['options']['username']) )
 return $json['resource_data_cache'][1]['resource']['options']['username'];
 else
 return Pinterest::UNABLE_TO_GET_ACCOUNT_NAME;


}


}


 private $image_preview_url = "https:www.pinterest.com/resource/ImagePreviewResource/create/";


public function generate_image_preview($path) {


 if( !$this->is_logged_in )
 return Pinterest::NOT_LOGGED_IN;
 
 if( !$this->pin_url )
 return Pinterest::MISSING_PIN_URL;


if( !$this->pin_description )
 return Pinterest::MISSING_PIN_DESCRIPTION;

 if( !$path )
 return Pinterest::IMAGE_DOESNT_EXIST;


$image = file_get_contents($path);
 if( !$image )
 return Pinterest::IMAGE_DOESNT_EXIST;



 $image_tmpfile = tempnam(sys_get_temp_dir(), "image");
 file_put_contents($image_tmpfile, $image);




 $data_json = array(
 "options" => array(
 "content_type" => mime_content_type($image_tmpfile),
 "base64_payload" => base64_encode($image)
 ),
 "context" => array()
 );



 $post = array(
 "source_url" => "/pin/create/bookmarklet/?url=" . urlencode($this->pin_url),
 "pinFave" => "1",
 "description" => urlencode($this->pin_description),
 "data" => json_encode($data_json, JSON_FORCE_OBJECT)
 );



 $data_info = array();
 foreach( $post as $k => $v )
 $data_info[] = "{$k}=" . urlencode($v);


$post_string = join("&", $data_info);



 $post_string = Pinterest::fix_encoding($post_string);




 $my_curl = \curl_init($this->image_preview_url);
 \curl_setopt_array($my_curl, array(
 CURLOPT_COOKIEFILE => $this->cookie_jar,
 CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0",
 CURLOPT_REFERER => "https:www.pinterest.com/pin/create/bookmarklet/?url=" . urlencode($this->pin_url) . "&pinFave=1&description=" . urlencode($this->pin_url),
 CURLOPT_HTTPHEADER => array(
 'Host: www.pinterest.com',
 'Accept: application/json, text/javascript, */*; q=0.01',
 'Accept-Language: en-US,en;q=0.5',
 'DNT: 1',
 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
 'X-Pinterest-AppState: active',
 'X-CSRFToken: ' . $this->csrf_token,
 'X-NEW-APP: 1',
 'X-APP-VERSION: 04cf8cc',
 'X-Requested-With: XMLHttpRequest'
 ),
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => $post_string,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_SSL_VERIFYPEER => false  remove me later
 ));



 $res = \curl_exec($my_curl);
 $json = json_decode($res, TRUE);
 \curl_close($my_curl);



 if( $json === null ) {
 return Pinterest::UNABLE_TO_CREATE_IMAGE_PREVIEW;
 } else {



 if( isset($json['resource_response']['data']['image_url']) )
 return $json['resource_response']['data']['image_url'];
 else
 return Pinterest::UNABLE_TO_CREATE_IMAGE_PREVIEW;


}


}


 


 private $create_pin_url = "https:www.pinterest.com/resource/PinResource/create/";


public function pin($board_id) {


 Can't do anything if we're not logged in
 if( !$this->is_logged_in )
 return Pinterest::NOT_LOGGED_IN;


 Need a pin URL and description, too
 if( !$this->pin_url )
 return Pinterest::MISSING_PIN_URL;


 Is it a repin?
 if( Pinterest::is_repin_url($this->pin_url) )
 return $this->repin($board_id);
 else {


 Not repinning? We need description and image preview
 if( !$this->pin_image_preview )
 return Pinterest::MISSING_PIN_IMAGE_PREVIEW;
 if( !$this->pin_description )
 return Pinterest::MISSING_PIN_DESCRIPTION;


}


 



 $data_json = array(
 "options" => array(
 "board_id" => $board_id,
 "description" => $this->pin_description,
 "link" => $this->pin_url,
 "image_url" => $this->pin_image_preview,
 "method" => "bookmarklet",
 "is_video" => null
 ),
 "context" => array()
 );



 $module_path = "module_path=App()>PinBookmarklet()>PinCreate()>PinForm(description=, default_board_id=null, show_cancel_button=true, cancel_text=Close, link=, show_uploader=false, image_url=, is_video=null, heading=Pick a board, pin_it_script_button=true)";



 $post = array(
 "source_url" => "/pin/create/bookmarklet/?url=" . urlencode($this->pin_url),
 "pinFave" => "1",
 "description" => urlencode($this->pin_description),
 "data" => json_encode($data_json, JSON_FORCE_OBJECT),
 "module_path" => urlencode($module_path)
 );



 $data_info = array();
 foreach( $post as $k => $v )
 $data_info[] = "{$k}=" . urlencode($v);


$post_string = join("&", $data_info);



 $post_string = Pinterest::fix_encoding($post_string);




 $my_curl = \curl_init($this->create_pin_url);
 \curl_setopt_array($my_curl, array(
 CURLOPT_COOKIEFILE => $this->cookie_jar,
 CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0",
 CURLOPT_REFERER => "https:www.pinterest.com/pin/create/bookmarklet/?url=&pinFave=1&description=",
 CURLOPT_HTTPHEADER => array(
 'Host: www.pinterest.com',
 'Accept: application/json, text/javascript, */*; q=0.01',
 'Accept-Language: en-US,en;q=0.5',
 'DNT: 1',
 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
 'X-Pinterest-AppState: active',
 'X-CSRFToken: ' . $this->csrf_token,
 'X-NEW-APP: 1',
 'X-APP-VERSION: 04cf8cc',
 'X-Requested-With: XMLHttpRequest'
 ),
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => $post_string,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_SSL_VERIFYPEER => false  remove me later
 ));



 $res = \curl_exec($my_curl);
 \curl_close($my_curl);
 $json = json_decode($res, TRUE);



 if( $json === null ) {
 $this->last_pin_id = 0;
 return Pinterest::UNABLE_TO_PIN;
 } else {
 $this->last_pin_id = $json['resource_response']['data']['id'];
 return Pinterest::SUCCESS;
 }



 }



 private $repin_url = "https:www.pinterest.com/resource/RepinResource/create/";


public function repin($board_id) {


 Can't do anything if we're not logged in
 if( !$this->is_logged_in )
 return Pinterest::NOT_LOGGED_IN;



 if( !preg_match("/https?:\/\/(?:www\.|)pinterest\.com\/pin\/(\d+)/", $this->pin_url, $matches) )
 return Pinterest::BAD_REPIN_URL;



 $pin_id = $matches[1];



 $repin_source = file_get_contents(trim($this->pin_url));
 if( !$repin_source )
 return Pinterest::REPIN_URL_NOT_FOUND;



 preg_match("<meta property=\"og:see_also\" name=\"og:see_also\" content=\"(.*?)\" data-app>", $repin_source, $matches);
 $pin_url = $matches[1];



 if( $this->pin_description )
 $pin_description = $this->pin_description;
 else {
 preg_match("<meta property=\"og:description\" name=\"og:description\" content=\"(.*?)\" data-app>", $repin_source, $matches);
 $pin_description = html_entity_decode($matches[1]);
 $pin_description = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $pin_description);
 }



 
 $data_json = array(
 "options" => array(
 "board_id" => $board_id,
 "description" => stripslashes($pin_description),
 "link" => stripslashes($pin_url),
 "is_video" => null,
 "pin_id" => $pin_id,
 ),
 "context" => array()
 );



 $module_path = "App()>Closeup(resource=PinResource(link_selection=true, fetch_visual_search_objects=true, id=))>PinActionBar(resource=PinResource(link_selection=true, fetch_visual_search_objects=true, id=))>ShowModalButton(module=PinCreate)#Modal(module=PinCreate(resource=PinResource(id=)))";



 $post = array(
 "source_url" => "/pin/{$pin_id}/",
 "data" => json_encode($data_json, JSON_FORCE_OBJECT),
 "module_path" => urlencode($module_path)
 );



 $data_info = array();
 foreach( $post as $k => $v )
 $data_info[] = "{$k}=" . urlencode($v);


$post_string = join("&", $data_info);



 $post_string = Pinterest::fix_encoding($post_string);




 $my_curl = \curl_init($this->repin_url);
 \curl_setopt_array($my_curl, array(
 CURLOPT_COOKIEFILE => $this->cookie_jar,
 CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0",
 CURLOPT_REFERER => $this->pin_url,
 CURLOPT_HTTPHEADER => array(
 'Host: www.pinterest.com',
 'Accept: application/json, text/javascript, */*; q=0.01',
 'Accept-Language: en-US,en;q=0.5',
 'DNT: 1',
 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
 'X-Pinterest-AppState: active',
 'X-CSRFToken: ' . $this->csrf_token,
 'X-NEW-APP: 1',
 'X-APP-VERSION: 04cf8cc',
 'X-Requested-With: XMLHttpRequest'
 ),
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => $post_string,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_SSL_VERIFYPEER => false  remove me later
 ));



 $res = \curl_exec($my_curl);
 \curl_close($my_curl);
 $json = json_decode($res, TRUE);



 if( $json === null ) {
 $this->last_pin_id = 0;
 return Pinterest::UNABLE_TO_REPIN;
 } else {
 $this->last_pin_id = $json['resource_response']['data']['id'];
 return Pinterest::SUCCESS;
 }



 }

 private $delete_pin_url = "https:www.pinterest.com/resource/PinResource/delete/";


public function delete_pin($pin_id) {


 Can't do anything if we're not logged in
 if( !$this->is_logged_in )
 return Pinterest::NOT_LOGGED_IN;



 $data_json = array(
 "options" => array(
 "id" => $pin_id
 ),
 "context" => array()
 );



 $module_path = "Modal()>ConfirmDialog(template=delete_pin, ga_category=pin_delete)";



 $post = array(
 "source_url" => "/pin/{$pin_id}/",
 "data" => json_encode($data_json, JSON_FORCE_OBJECT),
 "module_path" => urlencode($module_path)
 );



 $data_info = array();
 foreach( $post as $k => $v )
 $data_info[] = "{$k}=" . urlencode($v);


$post_string = join("&", $data_info);



 $post_string = Pinterest::fix_encoding($post_string);




 $my_curl = \curl_init($this->delete_pin_url);
 \curl_setopt_array($my_curl, array(
 CURLOPT_COOKIEFILE => $this->cookie_jar,
 CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0",
 CURLOPT_REFERER => "https:www.pinterest.com/pin/{$pin_id}/",
 CURLOPT_HTTPHEADER => array(
 'Host: www.pinterest.com',
 'Accept: application/json, text/javascript, */*; q=0.01',
 'Accept-Language: en-US,en;q=0.5',
 'DNT: 1',
 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
 'X-Pinterest-AppState: active',
 'X-CSRFToken: ' . $this->csrf_token,
 'X-NEW-APP: 1',
 'X-APP-VERSION: 04cf8cc',
 'X-Requested-With: XMLHttpRequest'
 ),
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => $post_string,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_SSL_VERIFYPEER => false  remove me later
 ));



 $res = \curl_exec($my_curl);
 \curl_close($my_curl);
 $json = json_decode($res, TRUE);
 print_r($json);



 if( $json === null ) {
 return Pinterest::UNABLE_TO_DELETE;
 } else {
 return Pinterest::SUCCESS;
 }



 }


 


 
 public static function fix_encoding($query_string) {
 return str_replace(
 array("%28", "%29", "%7E"),
 array("(", ")", "~"),
 $query_string
 );
 }


 public static function is_repin_url($query_string) {
 return preg_match("/https?:\/\/(?:www\.|)pinterest\.com\/pin\/(\d+)/", $query_string, $matches);
 }


 public static function get_pin_preview_url($pin_url) {



 if( !preg_match("/https?:\/\/(?:www\.|)pinterest\.com\/pin\/(\d+)/", $pin_url, $matches) )
 return Pinterest::BAD_REPIN_URL;



 $pin_id = $matches[1];



 $repin_source = file_get_contents(trim($pin_url));



 preg_match("<meta property=\"og:image\" name=\"og:image\" content=\"(.*?)\" data-app>", $repin_source, $matches);
 $image_preview_url = $matches[1];


return $image_preview_url;


}


 

 public static function get_pin_description($pin_url) {



 if( !preg_match("/https?:\/\/(?:www\.|)pinterest\.com\/pin\/(\d+)/", $pin_url, $matches) )
 return Pinterest::BAD_REPIN_URL;



 $pin_id = $matches[1];



 $repin_source = file_get_contents(trim($pin_url));



 preg_match("<meta property=\"og:description\" name=\"og:description\" content=\"(.*?)\" data-app>", $repin_source, $matches);
 $description = html_entity_decode($matches[1]);
 $description = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $description);


return $description;


}


 public static function get_pin_image_url($pin_url) {



 if( !preg_match("/https?:\/\/(?:www\.|)pinterest\.com\/pin\/(\d+)/", $pin_url, $matches) )
 return Pinterest::BAD_REPIN_URL;



 $pin_id = $matches[1];



 $repin_source = file_get_contents(trim($pin_url));



 preg_match("<meta property=\"twitter:image:src\" name=\"twitter:image:src\" content=\"(.*?)\" data-app>", $repin_source, $matches);
 $image_url = $matches[1];


return $image_url;


}

 public static function get_pin_pinner($pin_url) {



 if( !preg_match("/https?:\/\/(?:www\.|)pinterest\.com\/pin\/(\d+)/", $pin_url, $matches) )
 return Pinterest::BAD_REPIN_URL;



 $pin_id = $matches[1];



 $repin_source = file_get_contents(trim($pin_url));

 preg_match("<meta property=\"pinterestapp:pinner\" name=\"pinterestapp:pinner\" content=\"(.*?)\" data-app>", $repin_source, $matches);
 $pinner = $matches[1];
 $pinner = str_replace("https:www.pinterest.com/", "", $pinner);
 $pinner = rtrim($pinner, "/");

return $pinner;


}

public static function get_csrf_token($file) {


if( !file_exists($file) )
return null;


foreach( file($file) as $line ) {


$line = trim($line);



if( $line == "" or substr($line, 0, 2) == "# " )
continue;

list($domain, $tailmatch, $path, $secure, $expires, $name, $value) = explode("\t", $line);
if( $name == "csrftoken" )
return $value;


}


 Couldn't find it..
 return null;


}


}
?>

php-pinterest-auto-post-with-images.php

Also Read This πŸ‘‰   How to check array is empty or not in php?

<?php
header("Pragma: no-cache");
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");


function removeaccents($query_string, $my_curlarset='utf-8'){
$query_string = htmlentities($query_string, ENT_NOQUOTES, $my_curlarset);
$query_string = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $query_string);
$query_string = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $query_string); 
$query_string = preg_replace('#&[^;]+;#', '', $query_string); 
return $query_string;
}


require_once "Pinterest.class.php";


$pages = array(
'http:www.your-website.com/feed.rss',
'http:www.your-website-2.com/feed.rss'
);


$page = array_rand($pages);
$fichier = $pages[$page];


$dom = new DOMDocument();
if (!$dom->load($fichier)) {die('Error XML');}



$itemList = $dom->getElementsByTagName("item");
$nbitem = $itemList ->length;
echo "<span style=\"color:green;\">il y a ".$nbitem." produits</span><hr />";


$nb_min = 1;
$nb_max = $nbitem;
$nombre = mt_rand($nb_min,$nb_max);


echo $nombre.'<br>';
$countos=0;
foreach ($itemList as $item) {


$countos++;
if($countos==$nombre){
$titos = $item->getElementsByTagName('title');
if ($titos->length > 0) {$biztitos = $titos->item(0)->nodeValue;
$biztitos = preg_replace("/€/","€",$biztitos);



}
$link = $item->getElementsByTagName('link');
if ($link->length > 0) {$bizlink = $link->item(0)->nodeValue;
echo $bizlink;
}
 
$pic = $item->getElementsByTagName('description');
if ($pic->length > 0) {$bizpic = $pic->item(0)->nodeValue;


$html = $bizpic;


$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(img/@src)");


$src = preg_replace("/small/","large",$src);


$pakainfo_irl=removeaccents($biztitos);
$pakainfo_irl = substr($pakainfo_irl, 0, -1);
$pakainfo_irl=strtolower($pakainfo_irl);
$pakainfo_irl=preg_replace("/ /","-",$pakainfo_irl);
$pakainfo_irl=preg_replace("/\"/","",$pakainfo_irl);
$pakainfo_irl=preg_replace("/,/","",$pakainfo_irl);
$pakainfo_irl=preg_replace("/'/","",$pakainfo_irl);
$pakainfo_irl=preg_replace("/:/","",$pakainfo_irl);
$pakainfo_irl=preg_replace("/\","-",$pakainfo_irl);
$pakainfo_irl=preg_replace("/Β°/","-",$pakainfo_irl);
$pakainfo_irl=preg_replace("/%/","-",$pakainfo_irl);
$pakainfo_irl=preg_replace("/&/","\|",$pakainfo_irl);
$pakainfo_irl=preg_replace("/---/","-",$pakainfo_irl);
$pakainfo_irl=preg_replace("/--/","-",$pakainfo_irl);


if (!file_exists('img/'.$pakainfo_irl.'_458x458.jpg')){
copy($src,'img/'.$pakainfo_irl.'_458x458.jpg');
}


}


}
}


$p = new Pinterest();
$p->login("[email protected]", "pinterest-password");
if( $p->is_logged_in() )
 echo "Success, we're logged in\n";


$p->pin_url = $bizlink;
$p->pin_description = $biztitos;
$p->pin_image_preview = $p->generate_image_preview('./img/'.$pakainfo_irl.'_458x458.jpg');


$p->get_boards();


$p->pin($p->boards['Items']);
 And we're done

?>

Thats all, this is how to Post On Pinterest Automatically using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

5/5 - (6 votes)