How to send array with CURL? Should I `urlencode` it?

前端 未结 5 1694
攒了一身酷
攒了一身酷 2021-01-03 23:12

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?

5条回答
  •  甜味超标
    2021-01-03 23:52

    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.

    Example Usage

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

提交回复
热议问题