tags:

views:

261

answers:

3

Hi all,

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.xxx.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.xxx.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: xxx.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.xxx.com/proddetail.php?prod=lpd026n&cat=43

Not

http://www.facebook.com/share.php?u=http://www.xxx.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.

A: 

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.xxx.com/proddetail.php?<?php print $_SERVER['QUERY_STRING']?>"
Dror
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.
FB_noob
Have you tried just leaving out the urlencode() function?
Kaivosukeltaja
you mean leave the () empty?
FB_noob
i tried urlencode() with the () empty but the new link have only the page without any query behind proddetail.php
FB_noob
+3  A: 

This URL:

http://www.facebook.com/share.php?u=http://www.xxx.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.xxx.com/proddetail.php?prod=lpd026n&cat=43');

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

Update: build your dynamic query

// Original URL
$url = 'http://www.xxx.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);
K Prime
FB_noob
is it possible to do so?
FB_noob
@FB_noob - Of course, just pass the final string (built from `QUERY_STRING`) to `urlencode`. See my edited post above
K Prime
A: 

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.xxx.com/proddetail.php?prod=lpd026n&cat=43');

should be fine for your purposes

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