Forum und email

curl_setopt_array

(PHP 5 >= 5.1.3)

curl_setopt_array — Set multiple options for a cURL transfer

Descrição

bool curl_setopt_array ( resource $ch , array $options )

Define múltiplas opções para um sessão cURL. Esta função é útil para definir diversas opções cURL sem repetir a chamada da função curl_setopt().

Parâmetros

ch

Um manipulador cURL retornado por curl_init().

options

Um array especificando quais opções serão definida e seus respectivos valores. As chaves devem ser válidas constantes curl_setopt() ou seu inteiros equivalentes.

Valor Retornado

Retorna TRUE se todas opções foram definididas com sucesso. Se uma opção não foi definida com sucesso, FALSE é imediatamente retornado, ignorando qualquer outra opção da array options .

Exemplos

Example#1 Inicializando uma nova sessão cURL e obtendo o código-fonte de uma página web

<?php
// create a new cURL resource
$ch curl_init();

// set URL and other appropriate options
$options = array(CURLOPT_URL => 'https://www.example.com/',
                 
CURLOPT_HEADER => false
                
);

curl_setopt_array($ch$options);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Antes do PHP 5.1.4 esta função pode ser simulada com:

Example#2 Nossa implementação da 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;
   }
}
?>

Veja também