如何通过curl传递包含特殊字符的数据


How to pass data containing special characters through curl

我无法通过curl发布包含特殊字符的数据,有解决方案吗

第一个代码是我的curl函数,第二个代码是需要传递的数据

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

?>

如果messageData包含特殊字符的

,则这似乎不会返回任何内容

尝试urlencode

使用实体代码怎么样。。。

@ = %40
& = %26

更改行

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

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

以以下方式接收,因为您没有使用任何特定的后变量

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

检查以下代码。我在本地系统中进行了测试。在我的www director中,我创建了一个名为"test"的文件夹来保存代码。然后我在"测试"文件夹中创建了两个文件。一个用于发送请求的文件和一个用于处理请求的文件。

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

注意:使用特殊字符时,应该对该字符串进行编码。我试着用htmlspecialchars()转换它。但这并没有奏效。所以我在这里使用了base64编码方法。