CURLOPT_POSTFIELDS编码

时间:2011-06-10 16:09:23

标签: php encoding curl

我不确定使用cURL时要使用的编码:

  

GET:

     

(网址=)http://www.example.com/form.php?test=test+1

     

(URL =)   http://www.example.com/form.php?test=test%201

  

POST:

     

(POSTFIELDS =)test = test 1

     

(POSTFIELDS =)test = test + 1

     

(POSTFIELDS =)test = test%201

1 个答案:

答案 0 :(得分:4)

CURL可以接受post的参数数组,它会为你处理编码:

$array = (
    'test' => 'test 1',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);

但是,根据curl文档(http://php.net/curl_setopt,搜索CURLOPT_POST_FIELDS),对于PHP,这些对应该是urlencode()格式:

$post_args = urlencode('test=test 1');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_args);