gpt4 book ai didi

php - 使用 PHP 填充 Pinterest Pin It 按钮

转载 作者:可可西里 更新时间:2023-10-31 22:14:25 25 4
gpt4 key购买 nike

我正在尝试使用 Pin It 按钮,但它不会自动从页面获取 URL 和图像。如何使用 PHP 动态填充按钮?

有点像这样example ,但使用 Genesis 框架。

最佳答案

我也想构建一个动态填充的 Pinterest 按钮,所以我把这个放在一起。它利用 simpleHTMLDom parser抓取页面上的第一张图片(用于图片)和页面上的第一段(用于描述)。可以修改这些选择器以获取正确的图像/文本。查看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);
?>

现在我们只需在按钮链接中回显该新 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>

关于php - 使用 PHP 填充 Pinterest Pin It 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com