Escaping CURL @ symbol with PHP

I'm writing a php application that submits via curl data to sign up for an iContact email list. However I keep getting an invalid email address error. I think this may be due to the fact that I'm escaping the @ symbol so it looks like %40 instead of @. Also, according to the php documentation for curl_setopt with CURLOPT_POSTFIELDS:

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path.

So, is there anyway to pass the @ symbol as post data through curl in php without running it through urlencode first?


Use http_build_query() on your data-array first before passing it to curl_setopt() , that will lead to it sending the form as application/x-www-form-encoded instead of multipart/form-data (and thus the @ is not interpreted).

Also why do you really care about the @ in an email-address? It only matters if the @ is the first character, not somewhere in the middle.


After search PHP curl manual, I found there is no information to escape the first '@' if the post field is a string instead of a file if post with multipart/form-data encoding.

The way I worked around this problem is prefixing a blank at the beginning of the text. While our backend API will strip blanks so it could remove the blank and restore the original text. I don't know weather Twitter API will trim blanks on user input.

If so, this workaround also works for you.

If any one found the way to escaping the first '@' when using PHP curl with multipart/form-data encoding, please let us know.


I ran into the same issue, though with curl itself and not PHP curl.

When using curl's field option ' -F ' a leading @ symbol will not be sent in the POST but instead will instruct curl to send the file name that immediately succeeds the symbol as part of the POST.

Fortunately, curl offers another option ' --form-string ', which behaves the same way as ' -F ', except that the 'form-string' option is not parsed.

As an example, if you want to use curl to POST field1 with value "@value" and file1 with the file "testfile.txt" you can do so as follows:

curl "http://www.url.com" --form-string "field1=@value" -F "file1=@testfile.txt"
链接地址: http://www.djcxy.com/p/48890.html

上一篇: Libcurl在内容POST上很少崩溃

下一篇: 用PHP转义CURL @符号