0

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?

iamthestreets
  • 733
  • 1
  • 15
  • 38

1 Answers1

1

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.

Community
  • 1
  • 1
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • Thanks. The `current_url()` returns the current page url for me. after seeing your answer and some more research I think it comes down to it not working in local development. I will try in production when I'm ready. – iamthestreets Apr 14 '16 at 18:22