Latest web development tutorials

PHP curl_setopt_array function

PHP curl_setopt_array function

PHP Calendar Reference Manual PHP cURL Reference Manual

(PHP 5> = 5.1.3)

curl_setopt_array - a cURL transfer sessions batch options.


Explanation

bool curl_setopt_array ( resource $ch , array $options )

CURL session for the batch transmission setting options. This function needs to be set for a large amount of cURL option is very useful, do not repeat the call curl_setopt ().


parameter

ch

By the curl_init () returns a cURL handle.

options

An array used to determine the options and their values ​​to be set. Keys array must be a valid curl_setopt () constants or their integer values ​​equal.


return value

If all options are set successfully, it returns TRUE. If an option can not be set successfully, immediately returns FALSE, ignores any subsequent options in the options array.


Examples

Initializes a new cURL brilliant and crawl a web page.

<?php
// 创建一个新cURL资源
$ch = curl_init();

// 设置URL和相应的选项
$options = array(CURLOPT_URL => 'http://www.w3cschool.cc/',
                 CURLOPT_HEADER => false
                );

curl_setopt_array($ch, $options);

// 抓取URL并把它传递给浏览器
curl_exec($ch);

// 关闭cURL资源,并且释放系统资源
curl_close($ch);
?>

Earlier than PHP 5.1.3 this function can be simulated as follows:

We equivalence of curl_setopt_array () implementation

<?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: For curl_setopt (), it will pass an array to CURLOPT_POST the data to multipart / form-data is encoded, however, pass a URL-encoded string will be in application / x-www-form- urlencoded manner to encode data.


PHP Calendar Reference Manual PHP cURL Reference Manual