Twitter URL encoding
We're about to launch a little twitter Christmas competition, and I've run into a little snag.
To enter, people will need to post a tweet in the following format:
@user blah, blah, blah #hashtag
Currently, I have a form where they enter their answer (the blah, blah, blah) and a PHP script which encodes the entire statement and adds on the twitter url:
http://www.twitter.com/home?status=%40user%20blah%2Cblah%2Cblah%20%23hashtag
Then takes the user to twitter and puts the status in the update field.
However, whilst the spaces (%20) are decoded fine the @ and # characters remain as %40 & %23 respectively, even when the tweet is posted. I cannot put the actual characters in the url as twitter mistakes this for a search.
Is there any way to solve this? I'd like to do it without requiring username & password e开发者_StackOverflow社区tc if possible.
Any help will be greatly appreciated.
I've had the same problem, and the solution was very simple.
Just use
http://twitter.com/home?status=
instead of
http://www.twitter.com/home?status=
and it'll work as expected, even if the text isn't in ASCII.
If you want to know more details about this strange behavior see this blog post: http://www.kilometer0.com/blog/2010/01/21/twitter-status-urls-and-ampersands/
Hope this helps someone.
Encode the spaces as + and it works: http://twitter.com/home?status=%40user+blah%2Cblah%2Cblah+%23hashtag
You could try just posting right to Twitter:
<form action="http://www.twitter.com/home" method="GET">
<textarea name="status">
...
Hmm. At least when using the new Twitter layout ... this:
http://twitter.com/home?status=This+is+a+test+%26+So+is+this
... redirects to this (when logged in):
http://twitter.com/?status=This%20is%20a%20test%20&%20So%20is%20this
(notice the unencoded &) ... and the tweet-in-waiting becomes:
This is a test
:(
Myriad adjustments and variations didn't help. (Sigh.)
Admittedly sketchy workaround: Change & (%26) to + (%2B). It may be advisable do this with plain text, before (re-)introducing entities into the equation (e.g., don't change %26 to %2B). Measure twice, cut once, as they say.
After a wile i got this... You have to send as UTF8 encoded, you can use javascript to do that but I prefere PHP because my text also came from the tatabase....
<a href="http://twitter.com/share?text=<?= urlencode(utf8_encode( 'HERE I USE THE TITLE OF MY ARTICLE FROM DATABASE' )) ?>&lang=pt&url=<?= urlencode('http://jadielalves.com?s=stackoverflow') ;?>" target="_blank">SHARE ON TWITTER you can also put a twitter icon here... </a>
I've done it using this function from MDN
To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
source
精彩评论