我还想构建一个动态填充的Pinterest按钮,所以我把它放在一起.它利用simpleHTMLDom解析器来抓取页面上的第一个图像(对于图像)和页面上的第一个段落(对于描述).可以修改这些选择器以获取正确的图像/文本.有关详细信息,请参阅simpleHTMLDom文档.
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
现在我们只需在按钮链接中回显该新URL.
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10166 次 |
| 最近记录: |