I'm using the twitter custom share button 'Build your own tweet button' (https://dev.twitter.com/docs/tweet-button). The documentation says that I have to use the query parameters to pass on params.
PROBLEM: Twitter is encoding the text param wrong when I pass it a URL encoded string. A comma (,) is displayed as %252C in the tweet message. Other chars are also wrong encoded.
I use PHP url encode (http://php.net/manual/en/function.urlencode.php) to prepare the string for the call.
$text = urlencode("I just backed ". $project->getTitle().", an amazing new mobile app, on appbackr, where anyone can back mobile apps");
Then I build the twitter link:
'http://www.twitter.com/share?url='.urlencode($projectUrl).'&via='.$via.'&text='.$text.'&related='.$user->getTwitterProfileName()
The final twitter url call is:
http://www.twitter.com/share?url=http%3A%2F%2Flocalhost%2Fapp%2Fbig-top-ballet&via=appbackr&text=I+just+backed+Big+Top+Ballet%2C+an+amazing+new+mobile+app%2C+on+appbackr%2C+where+anyone+can+back+mobile+apps&related=philippberner
As soon as the page opens in the browser (Chrome and Firefox) twitter redirects the URL to:
https://twitter.com/intent/tweet?related=philippberner&text=I+just+backed+Big+Top+Ballet%252C+an+amazing+new+mobile+app%252C+on+appbackr%252C+where+anyone+can+back+mobile+apps&url=http%253A%252F%252Flocalhost%252Fapp%252Fbig-top-ballet&via=appbackr
This displays the following message in the tweet box:
I just backed Big Top Ballet%2C an amazing new mobile app%2C on appbackr%2C where anyone can back mobile apps via @appbackr
It converts Top+Ballet%2C+an+amazing to Top+Ballet%252C+an+amazing. The comma is displayed properly when I manually change %252C to %2C in the twitter URL.
Actually, it has nothing much to do with url encoding.
It actually works with urlencode, with rawurlencode, or even without url encoding Try the following URLs on opening a new tab.
With urlEncode: http://twitter.com/share?url=http%3A%2F%2Fwww.appbackr.com%2Fapp%2Fglass-ceiling&via=appbackr&text=I+just+backed+Glass+Ceiling%2C+an+amazing+new+mobile+app%2C+on+appbackr%2C+where+anyone+can+back+mobile+apps&related=
With rawurlEncode: http://twitter.com/share?url=http%3A%2F%2Fwww.appbackr.com%2Fapp%2Fglass-ceiling&via=appbackr&text=I%20just%20backed%20Glass%20Ceiling%2C%20an%20amazing%20new%20mobile%20app%2C%20on%20appbackr%2C%20where%20anyone%20can%20back%20mobile%20apps&related=
Without urlEncode: http://twitter.com/share?url=http%3A%2F%2Fwww.appbackr.com%2Fapp%2Fglass-ceiling&via=appbackr&text=I just backed Glass Ceiling, an amazing new mobile app, on appbackr, where anyone can back mobile apps&related=
The trick actually lies in using twitter.com instead of www.twitter.com. Not sure why there is a difference and it does not seem to be documented anywhere in twitter's documentation nor in google's search results. Although to be fair, Twitter's documentation did point to twitter.com and not www.twitter.com.
As always, it is definitely best practice to always urlencode the text even though it works without url encoding.