doumeng4400
2013-11-21 12:10
浏览 738
已采纳

如何通过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

图片转代码服务由CSDN问答提供 功能建议

我无法通过curl发布包含特殊字符的数据,任何解决方案? \ n

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

  $ communication_data_string ='token ='。json_encode(array(
“)  activityTypeId“=> 12,
”activityId“=> 5,
”userID“=> 10,
”partyID“=> 20,
”message_area_name“=>(”这是我的 用特殊的扯缩器测试数据&&!@ ## $%$ %% *)++“)
)
);  
 
echo(datPostingCURL($ url,$ communication_data_string)); 
   
 
 

?>

这似乎不是 如果messageData包含特殊字符 ,则返回任何内容

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doudou6719 2013-11-21 13:09
    已采纳

    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.

    已采纳该答案
    打赏 评论
  • douzhi1924 2013-11-22 08:26

    Try urlencode

    or

    How about using the entity codes...

    @ = %40
    
    & = %26
    
    打赏 评论
  • dtmm0148603 2013-11-22 09:13

    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'));
    
    打赏 评论

相关推荐 更多相似问题