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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line