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

时间:2013-11-21 12:10:30

标签: php 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包含特殊字符的

,这似乎没有返回任何内容

3 个答案:

答案 0 :(得分:2)

尝试urlencode

如何使用实体代码......

@ = %40

& = %26

答案 1 :(得分:1)

更改行

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

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

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

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

答案 2 :(得分:0)

检查以下代码。我在我的本地系统中测试过它。在我的www导演中,我创建了一个名为“test”的文件夹来保存代码。然后我在“test”文件夹中创建了2个文件。一个用于发送请求的文件和一个用于处理请求的文件。

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编码方法。

相关问题