Darsana.K
Darsana.K

Reputation: 67

How to pass data containing special characters through curl

I cannot able to post data its contain the special characters through curl ,Any solution ?

first code is my curl function ,second code is my data need to pass

$communication_data_string = 'token='.json_encode(array(
    "activityTypeId"=>12,
    "activityId"=>5,
    "userID"=> 10,
    "partyID"=>20,
    "message_area_name"=>("this is my test data with special cheractors&&!@##$%$%%*)++")
    )
); 

echo(datPostingCURL($url, $communication_data_string));

?>

this seems to not returning anything if the messageData contains the special character's

Upvotes: 3

Views: 12539

Answers (3)

user3011768
user3011768

Reputation: 191

change the line

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

to

curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($data_string));

the receive it in following way as you are not using any specific post variable

$data = urldecode(file_get_contents('php://input'));

Upvotes: 2

Sajith
Sajith

Reputation: 2852

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.

Upvotes: 0

vijaykumar
vijaykumar

Reputation: 4806

Try urlencode

or

How about using the entity codes...

@ = %40

& = %26

Upvotes: 2

Related Questions