English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP curl_setopt_array() Function Usage and Example

PHP CURL Reference Manual

(PHP 5 >= 5.1.3)

curl_setopt_array — Batch sets options for CURL transmission sessions.

Syntax

bool curl_setopt_array ( resource $ch , array $options )

Batch sets options for CURL transmission sessions. This function is very useful for setting a large number of CURL options, without needing to call curl_setopt() repeatedly.

parameters

ch

The CURL handle returned by curl_init().

options

An array used to determine the options to be set and their values. The key value must be a valid curl_setopt() constant or their equivalent integer values.

Return Value

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

Online Example

Initialize a new CURL session and fetch a web page.

<?php
// Create a new CURL resource
$ch = curl_init();
 
// Set the URL and corresponding options
$options = array(CURLOPT_URL => 'https://www.oldtoolbag.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);
?>

Before PHP 5.1.3This function can simulate 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:for curl_setopt() means passing an array to CURLOPT_POST will encode the data in multipart/form-data format, however, passing a URL-The encoded string will be encoded in the application/x-www-form-encode data in the urlencoded format.

PHP CURL Reference Manual