当使用cURL发布关联数组时,我需要先对内容进行urlencode编码吗? [英] When POSTing an associative array with cURL, do I need to urlencode the contents first?

查看:136
本文介绍了当使用cURL发布关联数组时,我需要先对内容进行urlencode编码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我分配一个数据数组作为一个cURL选项(通过CURLOPT_POSTFIELDS),我需要首先urlencode数据或将被照顾?

When I assign an array of data to be POSTed as a cURL option (via CURLOPT_POSTFIELDS), do I need to urlencode that data first or will that be taken care of?

推荐答案

curl_setopt 似乎没有对文本进行网址编码。但是,在PHP5中, http_build_query 函数会返回

The C implementation of curl_setopt doesn't seem to URL-encode the text. However, in PHP5, the http_build_query function returns a query string representation of the array that is URL-encoded.

  $curl_parameters = array(
    'param1' => $param1,
    'param2' => $param2
  );

  $curl_options = array(
    CURLOPT_URL => "http://localhost/service",
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query( $curl_parameters ),
    CURLOPT_HTTP_VERSION => 1.0,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false
  );

  $curl = curl_init();
  curl_setopt_array( $curl, $curl_options );
  $result = curl_exec( $curl );

  curl_close( $curl );

这篇关于当使用cURL发布关联数组时,我需要先对内容进行urlencode编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆