php - How I have to encode url for Facebook sharing

8

I am using a JScript code like this:

shareLink = function (link) {

    FB.ui({
        method: 'share',
        display: 'popup',
        href: link,
    }, function(response){});

}

and it is called in a line like this:

<?php
$sharedata = " ... parameters ... "
?>

<a href="javascript:shareLink('http://<?php echo $site; ?>/?v=<?php echo rawurlencode($sharelink); ?>');">

so, the destination script trasform parameter by using:

$str = rawurldecode($_GET["v"]);

the problem is when the url has a "+" character
using rawurlencode it its converted to "%2B"

zcu0xci%2FFMH2%2B7cLDPVP%2BgD7%2FwQJ%2FFT2Bw%3D%3D

but facebook change only "%2B" into "+" sign again:

zcu0xci%2FFMH2+7cLDPVP+gD7%2FwQJ%2FFT2Bw%3D%3D

and my script does not recognize it

EDIT:

if I echo the "v" parameter I get

zcu0xci/FMH2 7cLDPVP gD7/wQJ/FT2Bw==

instead of

zcu0xci/FMH2+7cLDPVP+gD7/wQJ/FT2Bw==

SOLUTION:

the solution I've found is to replace space by "+" before decode

$str = str_replace(" ","+",$str);
938

Answer

Solution:

You don't need to userawurldecode() on elements of the$_GET super-global variable. PHP decodes URL data for you.

In addition, you should generally useurlencode() to encode query string data. If you investigate the difference between these two functions, you'll see that urlencode() will translate any space character to the "+" character. This could possibly be the problem you're experiencing.

I'm having difficulty understanding the remainder of your question. You may need to work on your phrasing.

People are also looking for solutions to the problem: php - Composer create project issue

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.