(PHP 5 >= 5.1.3)
curl_setopt_array — Set options in batches for cURL transfer sessions.
bool curl_setopt_array ( resource $ch , array $options )
Set options for cURL transfer sessions in bulk. This function is useful for setting a large number of cURL options without having to call curl_setopt() repeatedly.
ch
The cURL handle returned by curl_init().
options
An array identifying the options to be set and their values. The array key must be a valid curl_setopt() constant or its integer equivalent.
Returns TRUE if all options are successfully set. If an option cannot be successfully set, FALSE is returned immediately, ignoring any subsequent options in the options array.
Initialize a new cURL brilliance and crawl a web page.
<?php// Create a new cURL resource $ch = curl_init(); // Set the URL and corresponding options $options = array(CURLOPT_URL => 'http://www.w3cschool.cc/', CURLOPT_HEADER => false );curl_setopt_array($ch, $options);// Grab the URL and pass it to the browser curl_exec($ch); // Close the cURL resource and release the system resource curl_close($ch);?>
Earlier than PHP 5.1.3 this function could be simulated as follows:
Our equivalent implementation of curl_setopt_array()
<?phpif (!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: In the case of curl_setopt(), passing an array to CURLOPT_POST will encode the data as multipart/form-data, whereas passing a URL-encoded string will encode it as application/x-www-form-urlencoded Encode the data.