Escaping CURL @ symbol with PHP
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.
How to pass data containing special characters through curl
Check the following code. I tested it in my local system. In my www director I created a folder named "test" for keeping the code. Then I created 2 files in that "test" folder. One file for sending the request and one for processing the request.
1. curl.php
<?php
function datPostingCURL($url, $data_string)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}
$url = "http://localhost/test/curl_post.php";
$communication_data_string = 'token='.json_encode(array(
"activityTypeId"=>12,
"activityId"=>5,
"userID"=> 10,
"partyID"=>20,
"message_area_name"=>base64_encode("hai ..this is my test data .!@##$%$%%*)++")
)
);
echo(datPostingCURL($url, $communication_data_string));
?>
2. curl_post.php
<?php
print_r(json_decode($_POST['token']));
$details = json_decode($_POST['token']); var_dump($details);
echo base64_decode($details->message_area_name);
?>
Note : While using special characters you should encode that string. I tried to convert it with htmlspecialchars()
. But it didn't worked. So I used base64
encoding method here.
How to include an '&' character in a bash curl statement
Putting single quotes around the &
symbol seems to work. That is, using a URL like http://www.example.com/page.asp?arg1=${i}'&'arg2=${j}
with curl returns the requested webpage.
CURL escape single quote
I had the same problem. The simplest solution is to escape the apostrophe with a backslash in addition to wrapping it in a set of single quotes. '\''
For your use case, change Mary's
to Mary'\''s
and it should work.
curl -XPOST 'http://localhost:9290/location/place' -d '{"geoloc": {"lat": "38.1899", "lon": "-76.5087"}, "longitude": "-76.5087", "admin_name1": "Maryland", "admin_name2": "St. Mary'\''s", "admin_name3": "", "postal_code": "20692", "admin_code3": "", "country_code": "US", "admin_code1": "MD", "latitude": "38.1899", "admin_code2": "037", "accuracy": null, "place_name": "Valley Lee"}'
An alternate approach is to wrap the POST data (
-d
) in double quotes while escaping all nested occurrences of double quotes in the JSON string with a backslash.
curl -XPOST 'http://localhost:9290/location/place' -d "{\"geoloc\": {\"lat\": \"38.1899\", \"lon\": \"-76.5087\"}, \"longitude\": \"-76.5087\", \"admin_name1\": \"Maryland\", \"admin_name2\": \"St. Mary's\", \"admin_name3\": \"\", \"postal_code\": \"20692\", \"admin_code3\": \"\", \"country_code\": \"US\", \"admin_code1\": \"MD\", \"latitude\": \"38.1899\", \"admin_code2\": \"037\", \"accuracy\": null, \"place_name\": \"Valley Lee\"}"
Strange â character in curl dom utf8 htmlspecialchars not working
The “
character detects as UTF-8 and seems fine, I'm seeing similar issues to what you describe though.
While this is not a perfect answer it is a work-around.
You can simply replace those characters (and other specific characters) before using the string.
str_replace(
[
"\xe2\x80\x98",
"\xe2\x80\x99",
"\xe2\x80\x9c",
"\xe2\x80\x9d",
"\xe2\x80\x93",
"\xe2\x80\x94",
"\xe2\x80\xa6"
],
[
"'",
"'",
'"',
'"',
'-',
'--',
'...'
],
$text
);
This would replace the left and right quote with just a quote, the left and right double quote with just a double quote, and the hyphen, dash and ellipse characters with matching symbols.
Special characters like @ and & in cURL POST data
cURL > 7.18.0 has an option --data-urlencode
which solves this problem. Using this, I can simply send a POST request as
curl -d name=john --data-urlencode passwd=@31&3*J https://www.example.com
Summarizing the comments, in case of mixed "good" and "bad" data and exclamation marks inside we can use on Windows:
curl -d "grant_type=client_credentials&client_id=super-client&acr_values=tenant:TNT123" --data-urlencode "client_secret=XxYyZ21D8E&%fhB6kq^mXQDovSZ%Q*!ipINme" https://login.example.com/connect/token
Using CURL in PHP to post text data starting with @
Could you use percent encoding instead? That way your '@' symbols would be %40 and CURL won't mess with them. We usually percent encode all our post fields.
http://en.wikipedia.org/wiki/Percent-encoding
Related Topics
Find All Hrefs in Page and Replace with Link Maintaining Previous Link - PHP
Run PHP Function Inside Jquery Click
Doctrine: Update Discriminator for Single_Table Inheritance
How to Recursively Obtain the "Parent Id" of Rows in This MySQL Table
Tinymce & Fancybox - Editor Won't Work on Second View
Class Firebase\Jwt\Jwt Not Found
PHP Readfile() Adding Extra Bytes to Downloaded File
Do PHP Opcode Cache Work with _Autoload
Why Does an Infinitely Recursive Function in PHP Cause a Segfault
Simulate PHP Array Language Construct or Parse with Regexp
How to Download Blob Based File from MySQL Database in PHP
How to Parse HTML Table Using PHP
How to Disable PHP Magic Quotes at Runtime
Htaccess Rule to Parse PHP in HTML Files Not Working
Properly Escaping with MySQLi | Query Over Prepared Statements
Laravel Form Methods VS Traditional Coding
Best Method for Converting a PHP Array to JavaScript
Warning: MySQLi_Query() Expects at Least 2 Parameters, 1 Given. What