Reputation: 1196
I'm trying to share a link on facebook using its simple Sharer.
I'm passing some parameters in this way:
title="Share this article/post/whatever on Facebook"
href="http://www.facebook.com/sharer.php?
s=100
&p[url]=http://www.mypage.com/index.php?firstID=1&secondID=2
//etc.
but it's only partially working because it only take the firstID but not the second.
My guess is that Facebook thinks that secondID is its own, but it can't use it and it discards the param.
Any guess on how I can escape them?
Upvotes: 0
Views: 1730
Reputation: 481
Found a solution that works. Facebook does not directly accept URL with parameters within your local page. It strips them off in it's process. If you are writing your own code in PHP, you can get around this problem by encoding the parameters and then attaching this encoded string to the end of the page URL.
This works in a WordPress environment as well, if you are using shortcodes to populate the page.
The code snippet I came up with is this:
// When generating a URL for display, encode the parameters within the standard URL
$PageURL = "https://yourdomain/page";
$EncodedParams = urlencode('param1='.<YOUR VALUE> . '¶m1='.<YOUR VALUE>);
$URLPath = $PageURL . '?' . $EncodedParams . '#';
Now on the page in question
// This find the page you are currently displaying
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||
$_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// Retrieve the parameters from the URL
$EncodedParams = substr($url, strrpos($url, '?' )+1);
$DecodeParams = urldecode ($EncodedParams);
$Params = explode('&', $DecodeParams);
$temp = explode('param1=', $Params[0]);
$Param1 = $temp[1];
$temp = explode('param2=', $Params[1]);
$Param2 = $temp[1];
Param1 and Param2 can now be used in the code for further definition. The trick is all about encoding.
Upvotes: 0
Reputation: 1178
Use PHP's rawurlencode()
on the shared url:
title="Share this article/post/whatever on Facebook"
href="http://www.facebook.com/sharer.php?
s=100
&p[url]=<?=rawurlencode('http://www.mypage.com/index.php?firstID=1&secondID=2')?>
Upvotes: 1
Reputation: 4466
When using the sharer.php
to share the page you should encode the URL so that it is used by it in appropriate manner or else it might take the parameters like your secondID
as its own parameter and would render the URL incorrectly.
Upvotes: 1