Twitter URL encoding

2019-03-21 07:44发布

问题:

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 etc if possible.

Any help will be greatly appreciated.

回答1:

Encode the spaces as + and it works: http://twitter.com/home?status=%40user+blah%2Cblah%2Cblah+%23hashtag



回答2:

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.



回答3:

You could try just posting right to Twitter:

<form action="http://www.twitter.com/home" method="GET">
    <textarea name="status">

...



回答4:

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.



回答5:

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>



标签: php url twitter