I am sending an image and a tweet (text) to Twitter API as multipart/form-data. The image starts with '#someimagefile' and works as expected. The tweet text is something like, '#username hi.' but fails because cURL tries to interpret #username as a file path and errors out.
See answer from Wang Bin: https://stackoverflow.com/a/9137917/922522
While a work-around is to prefix the tweet text with a space so the # isn't the first char, I was wondering if there was a better way to do this? Twitter does not allow the # sign to be encoded as anything else, https://dev.twitter.com/discussions/6663.
Is there a way to make cURL for PHP ignore the # sign in the text (and not for the image)?
Have you tried encoding the "#" in the tweet?
str_replace('#', urlencode('#'), $tweet);
edit: I saw your the forum post you linked and I believe he's saying it needs to be percent encoded which is what urlencode does. Have you tried what I posted above? If not give it a shot.
Related
I am sending the below url with query string. In the query string one parameter
"approverCmt" has value with hash(#).
"/abc/efd/xyz.jas?approverCmt=Transaction Log #459505&batchNm=XS_10APR2015_082224&mfrNm=Timberland"
In server side when I tried to retrieve it from the request I get
approverCmt = Transaction Log -----> "#459505" is missing
batchNm = null
mfrNm = null
And If I remove hash(#) from query string or If I replace # with %23 every thing works fine
I don't understand why I am getting null for one parameter if another parameter contains a hash(#) symbol.
Appreciate if any one can explain.
This is known as the "fragment identifier".
As mentioned in wikipedia:
The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document.
The part after the # is info for the client. It is not sent to the server. Put everything only the browser needs here.
You can use the encodeURIComponent() function in JavaScript to encode special characters in a URL, so that # characters are converted to other characters that way you can be sure your whole URL will be sent to the server.
The Hash value is for the anchor, so it is only client-side, it is often used in client-side framework like angular for client-side routing.
The anchor is NOT available server-side.
In your case you don't need an anchor, but a parameter value with a # break the query string the value is "Transaction Log #459505".
EDIT Naive solution that doesn't work, just let it ther for history, See Real solution below
The solution is to encode client-side and decode serveur-side
Encoding in javascript
encodeURI("Transaction Log #459505")
//result value "Transaction%20Log%20#459505"
Decode in Java
java.net.URLDecoder.decode("Transaction%20Log%20#459505");
//result "Transaction Log #459505"
EDIT: But: Javascript doesn't encode in the same way than Java
So the correct answer (I hope) is to manually replace all your # with %23, then Java will decode it normally, or to use encodeURIComponent as suggested in comments. For your need the replace solution seem to be enough.
Encode in Javascript:
encodeURI("yourUrl/Transaction Log #459505").replace(/#/,"%23")
//result: yourUrl/Transaction%20Log%20%23459505
The decode in Java doesn't change
java.net.URLDecoder.decode("Transaction%20Log%20#459505")
// result (java.lang.String) Transaction Log #459505
Sorry for long post, I didn't see the difference bettween Java and the JavaScrip Url encoding
the hash is an anchor:
see wikipedia for more information
Im using Twitter's RESTful API 1.1 and on odd occations usually when there is a URL embedded in the tweet it pulls through in funny charcters e.g.
#MyHandle_123 RT #ThinkAfricaFeed: Controversy & acrimony may surround Nigeria's country's federalist system but it may be the country's best option: htt…
I tried to call the function utf8_decode but its still renders funny characters in my browser.
Any idea's on how I can get the returned values to show correctly?
I was running into a similar problem, since you tried the utf8 decode and it didn't work, try this:
htmlentities($td->text, ENT_NOQUOTES, 'UTF-8');
where td is the object whose text or item is being referenced.
Hope that helps
I am using smarty as a template engine. I have to escape an image file path {$filepath|urlencode}, the problem is that the white space are converted into a '+', which prevent the image to be reached on the server : %20 would work, how to escape correctly my path ?
Edit : more precisely, I use the facebook share link
I use a facebook share as so and it doesn't display the image when shared :
``
The final code looks like for my specific usage :
<a href="http://www.facebook.com/dialog/feed?app_id=...&link=http%3A%2F%2Fmysite.org%2Findex.php%3Fpage%3Dcampaign%26campaign_id%3D18&picture=http%3A%2F%2Fmysite.org%2Ffiles%2Fcampaign%2Fimage%2Foriginals%2F18%2FSans+titre-3.jpg&name=Some text "Text d'Text", Text&description=Rejoignez%20la%20campagne%21&redirect_uri=http%3A%2F%2Fmysite.org%2Findex.php%3Fpage%3Dcampaign%26campaign_id%3D18"onclick="window.open(this.href);return false;">
on the same site, all the facebook share link works perfectly and the image displays well ! Reason why I thought it was the link of that specific image that is not working
escape is what you're searching for. Take a look at:
http://www.smarty.net/docsv2/en/language.modifier.escape.tpl
{$filepath|escape:"url"}
urlencode is used to encode (not escape!) a string to be used as a query part inside an URL passed as GET var: http://php.net/manual/en/function.urlencode.php
URL encoded space is either a plus sign or %20. They are equivalent, and are both interpreted as a space on the server.
If you see either in the URL, then the server will see a space.
You say that the plus sign is preventing the image from being loaded. This sounds like a deeper problem than simply using the wrong encoding. Possibly it's being double-encoded?
What is the actual URL being requested in the browser? Open the dev tools/Firebug, and look at the requests to find out. If the URL includes %2B then the plus sign is being double-encoded. This is the problem you need to solve.
The other solution, of course, is not to use spaces in filenames on the web. The only reason one would want spaces in filenames is for readability, but since the web requires spaces to be urlencoded, it removes that readability anyway. Take away the spaces, and the problem will go away by itself.
Here's the thing. I have friendly urls like
http://site.com/blog/read/мъдростта-на-вековете
http://site.com/blog/read/green-apple
The last segment is actually the friendly title of the blog article. The problem is when I try to pass that segment to the database, the cyrillic fonts turn into something like %D1%8A%D0%B4%D1%80%D0%BE%D1%81%D1%8 and couldn't match the database record. In the address bar in my browser it looks normal (мъдростта-на-вековете) but if I choose 'copy url location' the last segment again turns into these strange characters. I'm using CodeIgniter and everything is set to UTF-8.
Please help! :(
The text is just being encoded to fit the specification for URLs.
Echo out the data to a log to see what you are actually trying to pass to the database.
You should be able to decode it with urldecode.
The above answers are ok, but if you want to use routing with cyrillic it isn't enough. For example if you have http://site.com/блог/статия/мъдростта-на-вековете you will have to do something like this:
In config/routes.php: $route['блог/статия/(:any)'] = "blog/article/$1";
In system/core/URI.php , in the function _explode_segments(), you can change
$val = trim($this->_filter_uri($val));
to
$val = urldecode(trim($this->_filter_uri($val)));
This will solve the above problem plus controllers and functions.
Actually, Firefox is cheating you here: the URL actually is url-encoded, but is shown as if it wasn't. So copy-pasting and retrieving it on the server will have the URL encoded.
(Not sure if other browsers behave in the same way.)
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.
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....
SHARE ON TWITTER you can also put a twitter icon here...
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