0

I have been trying to attempt to use the facebook share function in my website but i cant seems to have the right result.

Say:
i have a page called http://www.example.com/product.php?prod=lpd026n&cat=43 and i am using facebook's share function to have visitors to share the page in the FB wall.

i tried writing the link this way but i doesn't seems to be successful:

href="http://www.facebook.com/share.php?u=www.example.com/proddetail.php?<?php print urlencode(@$_SERVER['QUERY_STRING']!=''?'?'.$_SERVER['QUERY_STRING']:'')?>"

as the result the arguments in the URL came out to be in %26, %3D and etc..
Ie: example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43

as some of you may know that the data after '?' is dynamic and i am planing to use the code above in the frame of the page, so it will have different query passed to the share link in every new item.

The end result that i want got to look like this:

http://www.facebook.com/sharer.php?u=http://www.example.com/proddetail.php?prod=lpd026n&cat=43

Not

http://www.facebook.com/share.php?u=http://www.example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43

can anyone help me to solve this problem?
Thanks in advance!
Ps: if you are unclear, please ask me to further clarify.

2
  • 1
    The URL you get is the correct one. (I'm using the same code to share links on Facebook.) I don't understand what you mean by "the data after '?' is dynamic". It doesn't matter, you need to use urlencode like you do. Commented Jan 12, 2010 at 9:54
  • i mean the arguments after .php? is the date fetched from the database. i need to have the link (href="") to be automated not prefixed so it works for every arguments that passes to the link. thus, placing the same argument the same argument on the url bar in new link.. (i'm doing this because my knowledge of php is shallow, and i have no idea how it is obtained from the database..) Commented Jan 12, 2010 at 14:05

3 Answers 3

3

This URL:

http://www.facebook.com/share.php?u=http://www.example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43

is only partially-encoded. You actually need to fully URL-encode it before passing to FB, so that it won't interfere with FB's URL structure. I'm sure that their script will know how to parse it properly.

The correct method is:

$url = 'http://www.facebook.com/sharer.php?u='.urlencode('http://www.example.com/proddetail.php?prod=lpd026n&cat=43');

// evaluates to:
// http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.example.com%2Fproddetail.php%3Fprod%3Dlpd026n%26cat%3D43

Update: build your dynamic query

// Original URL
$url = 'http://www.example.com/proddetail.php';
if ($_SERVER['QUERY_STRING'])
    $url .= '?'.$_SERVER['QUERY_STRING'];

// Final URL for FB
$fb_url = 'http://www.facebook.com/share.php?u='.urlencode($url);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi K prime, thanks for replying but the problem is that '?prod=lpd026n&cat=43' is dynamic.. i can't afford to encode all products for the share links.. as my plan was to write it once and use on all products, so therefore i want to use php to read the query and pass it the the sharer link..
@FB_noob - Of course, just pass the final string (built from QUERY_STRING) to urlencode. See my edited post above
0

This is what urlencode does, what is the problem with the link this way?

Edit: I do not use PHP, but I think the following will do the trick (omitted the urlencode):

href="http://www.facebook.com/share.php?u=www.example.com/proddetail.php?<?php print $_SERVER['QUERY_STRING']?>"

2 Comments

i have just edited my post. i need to pass my query in to new link without having the arguments changed to %20 or anything else.
i tried urlencode() with the () empty but the new link have only the page without any query behind proddetail.php
0

I guess K Prime is right.

u need to encode the whole url because the slashes and ":" are still causing problems in this link ;)

$url = 'http://www.facebook.com/sharer.php?u='.urlencode('http://www.example.com/proddetail.php?prod=lpd026n&cat=43');

should be fine for your purposes.

1 Comment

hi jaysn, would encode the ulr, means it only works for one page only?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.