dsfdfdfd6576578 2016-04-13 22:10
浏览 36
已采纳

Tumblr共享按钮不能与PHP Codeigniter一起使用

I am trying to implment the share button on my website, but it does not work when I use php. However it does work when I input the data manunaly.

for example this works:

<a href="http://www.tumblr.com/share/link?url=https%3A%2F%2Fdomain.com&amp;name=test&amp;description=test">Share on Tumblr</a>

but this does not:

<a href="http://www.tumblr.com/share/link?url=<?=urlencode(current_url())?>&amp;name=test&amp;description=test">Share on Tumblr</a>

When I click on the one using PHP I get redirected to:

https://www.tumblr.com/widgets/share/tool/banned?

EDIT: I even tried doing it this way:

var tumblr_link_url = '<?=current_url()?>';
var tumblr_link_name = 'test';
var tumblr_link_description = 'test';
var url = "http://www.tumblr.com/share/link?url=" + encodeURIComponent(tumblr_link_url) + "&amp;name=" + encodeURIComponent(tumblr_link_name) + "&amp;description=" + encodeURIComponent(tumblr_link_description);
var tumblr_button = document.createElement("a");
tumblr_button.setAttribute("href", url);
tumblr_button.setAttribute("title", "Share on Tumblr");
tumblr_button.innerHTML = "Share on Tumblr";
document.getElementById("tumblr_button").appendChild(tumblr_button);

but it still does not work. It definitely has something to do with using PHP. My other share buttons work, it's only the Tumblr share button that has this problem when using PHP

Any reason why this does not work?

  • 写回答

1条回答 默认 最新

  • douzhenzu0247 2016-04-14 18:16
    关注

    tumblr forbids you to post invalid urls such as domain, which I assume you've obfuscated for this forum. in your working example, you actually use domain.com as an example, which is in fact a real site which is why it works on tumblr. notice the absence of the .com in your broken example.

    side note: unless you're trying to advertise for domain.com, I would encourage you to use a non-commercial entity like example.com

    I may be wrong, but I'm guessing that you're testing this on a local development machine whose current url would be localhost or 127.0.0.1, or an otherwise firewall protected host, which are also invalid URLs to tumblr because they are not world accessible. Your code should work just fine on a world accessible server.

    It's also worth noting in my tests - which I'm no code igniter fan, so perhaps I skipped a configuration setting - that the current_url() function returns my IP, and not the hostname. This actually isn't a problem, because tumblr will accept a valid accessible IP address also, but it may be undesirable. So if you're experiencing this, I simply rebuilt the whole URL following this guide with a little help from this post to get the protocol right as such:

    <?php 
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http";
    $host     = $_SERVER['HTTP_HOST'];
    $script   = $_SERVER['SCRIPT_NAME'];
    $params   = $_SERVER['QUERY_STRING'];
    $currentUrl = $protocol . '://' . $host . $script . '?' . $params;
    ?>
    <a href="http://www.tumblr.com/share/link?url=<?=urlencode($currentUrl)?>&amp;name=test&amp;description=test">Share on Tumblr</a>
    

    You can see the working example here, notice the small "share on tumblr" link at the bottom. The full source code for that is here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分