Start Pinterest Pin It Button with PHP

I try to use the Pin It button, but does not automatically get the URL and image from the page. How can I dynamically populate a button using PHP?

An example of such an example , but without , using the Genesis framework.

+4
source share
4 answers

I also wanted to create a dynamically populated Pinterest button, so I put it together. It uses simpleHTMLDom parser to capture the first image on a page (for an image) and the first paragraph on a page (for a description). These selectors can be modified to obtain the correct image / text. For more information, see the simpleHTMLDom Documentation.

<?php // Include our dom parser include('simple_html_dom.php'); // Get the current page URL function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } $thispage = curPageURL(); // Grab our data $html = file_get_html($thispage); $imgtag = $html->find('img', 0)->src; $desc = $html->find('p', 0)->plaintext; // Build the URL $pinURL = 'http://pinterest.com/pin/create/button/?url=' . $thispage . '&media=' . $imgtag . '&description=' . $desc . ''; $html->clear(); unset($html); ?> 

Now we just repeat the new URL in the button link.

 <a href="<?php echo $pinURL ?>" class="pin-it-button" count-layout="horizontal">Pin It</a> <script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script> 
+5
source

This is the code generated by Pinterest for the Pin It button.

Pay attention to the inclusion of php tags. Put your own php code to populate the url, media and description parameters. Done.

<a href="http://pinterest.com/pin/create/button/?url=<?php echo url_php_function(); ?>&media=<?php echo media_php_function(); ?>&description=<?php echo description_php_function(); ?>" class="pin-it-button" count-layout="horizontal">Pin It</a>

+1
source

Looking at http://pinterest.com/about/goodies/ , it looks like they are encoding a URL or at least replacing something like ': //' '% 3A% 2F% 2F', but neither one example above does not follow this pattern or uses urlencode.

I assume for me a trial version and an error.

+1
source

Here is a code snippet from a function method in a news aggregator. The messages that are available get the Pin It button if I can find the image with simpleHTMLDom.

  // Pinterest /* Pinterest requires two bits of code, something like this: <a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.muschamp.ca%2F&media=http%3A%2F%2Fwww.muschamp.ca%2Fimage.jpg&description=whatever" class="pin-it-button" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a> Where you want the button ie here, and <script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script> directly after the body tag just like Facebook new style button */ // I definitely miss how I did things in my previous code base, if there is no image should not be able to pin... if(($image != NULL) && ( ! strpos($image->src, '+')) && ( ! strpos($image->src, '%')) && (preg_match('|^http?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $image->src))) { // If I don't display a valid image, no sense in showing a Pin It button $html .= '<li>'; // Opening <li> // May want a more elaborate description... $html .= '<a href="http://pinterest.com/pin/create/button/?url=' . $item->get_permalink() . '&media=' . $image->src . '&description=' . $item->get_title() . '" class="pin-it-button" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></li>'; } 

Designed, if proven, necessary for testing, as some valid URLs may not be accepted as valid on Pinterest Pinterest, at least that was what I discovered when testing. The "%" and "+" symbols seem to be especially unpleasant. Pinterest will not allow you to print small images or even very large images, but since I also used PHP to resize images in my news aggregator, I do not test this here, but this is what I need when adding Pinterest to your application.

0
source

Source: https://habr.com/ru/post/1399111/


All Articles