简体   繁体   中英

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

Try urlencode

or

How about using the entity codes...

@ = %40

& = %26

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'));

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);
?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM