Share link to facebook with PHP and Javascript

3.1k Views Asked by At

I'm new to PHP and I've this code I'am not sure why it redirect me to http://www.facebook....php?s=100&p[url]= instead I want http://www.facebook....php?s=100&p[url]=test.

<?php

$url = 'test' ;

echo "

<a target='_blank' href='http://www.facebook.com/sharer/sharer.php?s=100&amp;p[url]=' " . $url . "  '&amp;p[images][0]=https://scontent-vie1-1.xx.fbcdn.net/hphotos-xta1/v/t1.0-9/11102691_483697748448554_6954591283482418788_n.png?oh=78cfe0fb891a30cf56d78df08e65e70f&oe=55F12850;p[title]=Desgin Football online and win&amp;p[summary]=Vote and Win '>

<img style='width: 50px;

    height: 50px;  margin-left: 35px;

    margin-right: auto;'  src='2.png'></a> 


    " ;
?>

Any ideas?

2

There are 2 best solutions below

0
On BEST ANSWER

Don´t copy old code, the sharer.php only takes the URL as parameter since many years:

http://www.facebook.com/sharer/sharer.php?u=<your-urlencoded-url>

For example:

echo '<a href="http://www.facebook.com/sharer/sharer.php?u=' . urlencode($url) . '">test</a>';
0
On

your code needs to be updated as @luschn said

<a target="_blank" href="http://www.facebook.com/sharer.php?u=<?=rawurlencode("http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]);?>">
                        <img src="images/facebook.png" alt="facebook"/>
                    </a>

the u parameter needs urlencoded value (enter your own URL, if not current page is shared)

for differences between rawurlencode and urlencode please read this thread urlencode vs rawurlencode?