PHP curl_setopt_array Function

PHP Calendar 参考手册PHP cURL Reference Manual

(PHP 5 >= 5.1.3)

curl_setopt_array — Set multiple options for a cURL transfer session.


Description

bool curl_setopt_array ( resource $ch , array $options )

Sets multiple options for a cURL transfer session. This function is very useful when you need to set a large number of cURL options, avoiding the need to call curl_setopt() repeatedly.


Parameters

ch

A cURL handle returned by curl_init().

options

An array used to determine the options to be set and their values. The array keys must be valid curl_setopt() constants or their integer equivalents.


Return Values

Returns TRUE if all options are successfully set. If an option cannot be successfully set, it immediately returns FALSE, ignoring any subsequent options in the options array.


Example

Initialize a new cURL session and fetch a web page.

Example

<?php //Create a new cURL resource $ch = curl_init(); //Set the URL and corresponding options $options = array(CURLOPT_URL => 'https://www.example.com', CURLOPT_HEADER => false ); curl_setopt_array($ch, $options); //Fetch the URL and pass it to the browser curl_exec($ch); //Close the cURL resource and release system resources curl_close($ch); ?>

Prior to PHP 5.1.3, this function could be simulated as follows:

Our equivalent implementation of curl_setopt_array()

<?php
if (!function_exists('curl_setopt_array')) {
   function curl_setopt_array(&$ch, $curl_options)
   {
       foreach ($curl_options as $option => $value) {
           if (!curl_setopt($ch, $option, $value)) {
               return false;
           } 
       }
       return true;
   }
}
?>

Note:As with curl_setopt(), passing an array to CURLOPT_POST will encode the data as multipart/form-data, whereas passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.


PHP Calendar 参考手册PHP cURL Reference Manual

Other Extensions