PHP Doku:: Mehrere Optionen für einen cURL-Transfer setzen - function.curl-setopt-array.html

Verlauf / Chronik / History: (29) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteCURLcURL Funktionencurl_setopt_array

Ein Service von Reinhard Neidl - Webprogrammierung.

cURL Funktionen

<<curl_multi_select

curl_setopt>>

curl_setopt_array

(PHP 5 >= 5.1.3)

curl_setopt_arrayMehrere Optionen für einen cURL-Transfer setzen

Beschreibung

bool curl_setopt_array ( resource $ch , array $options )

Setzt mehrere Optionen für eine cURL-Session. Diese Funktion ist nützlich, um eine große Anzahl an Optionen zu setzen, ohne wiederholt curl_setopt() aufrufen zu müssen.

Parameter-Liste

ch

Ein von curl_init() zurückgegebenes cURL-Handle.

options

Ein array das die zu setzenden Optionen und die entsprechenden Werte enthält. Die Array-Schlüssel sollten gültige curl_setopt()-Konstanten resp. deren Integer- Equivalent sein.

Rückgabewerte

Gibt TRUE zurück, wenn alle Optionen erfolgreich gesetzt werden konnten. Konnte eine Option nicht gesetzt werden wird unmittelbar FALSE zurückgegeben und alle folgenden Optionen im Parameter options werden ignoriert.

Beispiele

Beispiel #1 Initialisierung einer cURL-Session und holen einer Webseite

<?php
// neues cURL-Handle erstellen
$ch curl_init();

// URL und andere Optionen setzen
$options = array(CURLOPT_URL => 'http://www.example.com/',
                 
CURLOPT_HEADER => false
                
);

curl_setopt_array($ch$options);

// URL holen und an der Browser weitergeben
curl_exec($ch);

// cURL-Handle schließen und Systemresourcen freigeben
curl_close($ch);
?>

Für PHP-Versionen vor PHP 5.1.3 kann diese Funktion simuliert werden durch:

Beispiel #2 Eine eigene Implementierung von 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;
   }
}
?>

Anmerkungen

Hinweis:

Analog zu curl_setopt() wird ein für CURLOPT_POST übergebenes Array als multipart/form-data, ein URL-kodierter String als application/x-www-form-urlencoded kodiert.

Siehe auch


4 BenutzerBeiträge:
- Beiträge aktualisieren...
anthon at piwik dot org
1.06.2010 15:52
Starting in PHP 5.2.0, CURLOPT_FOLLOWLOCATION can't be set via curl_setopt_array() (or curl_setopt()) when either safe_mode is enabled or open_basedir is set.  In these cases, the order of CURLOPT_* settings in the array can be important.
maran dot emil at gmail dot com
25.03.2009 20:54
In case that you need to read SSL page content from https with curl, this function can help you:

<?php

function get_web_page( $url,$curl_data )
{
   
$options = array(
       
CURLOPT_RETURNTRANSFER => true,         // return web page
       
CURLOPT_HEADER         => false,        // don't return headers
       
CURLOPT_FOLLOWLOCATION => true,         // follow redirects
       
CURLOPT_ENCODING       => "",           // handle all encodings
       
CURLOPT_USERAGENT      => "spider",     // who am i
       
CURLOPT_AUTOREFERER    => true,         // set referer on redirect
       
CURLOPT_CONNECTTIMEOUT => 120,          // timeout on connect
       
CURLOPT_TIMEOUT        => 120,          // timeout on response
       
CURLOPT_MAXREDIRS      => 10,           // stop after 10 redirects
       
CURLOPT_POST            => 1,            // i am sending post data
          
CURLOPT_POSTFIELDS     => $curl_data,    // this are my post vars
       
CURLOPT_SSL_VERIFYHOST => 0,            // don't verify ssl
       
CURLOPT_SSL_VERIFYPEER => false,        //
       
CURLOPT_VERBOSE        => 1                //
   
);

   
$ch      = curl_init($url);
   
curl_setopt_array($ch,$options);
   
$content = curl_exec($ch);
   
$err     = curl_errno($ch);
   
$errmsg  = curl_error($ch) ;
   
$header  = curl_getinfo($ch);
   
curl_close($ch);

 
//  $header['errno']   = $err;
  //  $header['errmsg']  = $errmsg;
  //  $header['content'] = $content;
   
return $header;
}

$curl_data = "var1=60&var2=test";
$url = "https://www.example.com";
$response = get_web_page($url,$curl_data);

print
'<pre>';
print_r($response);

?>
fnjordy at gmail dot com
1.12.2008 8:48
There is no CURLOPT_MAXFILESIZE in the PHP module but it's function only works with Content-Length headers anyway.  There are two ways of checking download sizes, one is after the download is complete using filesize(), the other is as the download is running allowing you to terminate before wasting time and disk space.

<?php
$GLOBALS
['file_size'] = 0;
$GLOBALS['max_file_size'] = 1024 * 1024;
function
on_curl_header($ch, $header)
{
   
$trimmed = rtrim($header);   
    if (
preg_match('/^Content-Length: (\d+)$/', $trimmed, $matches))
    {
       
$file_size = $matches[1];
        if (
$file_size > $GLOBALS['max_file_size']) {
           
// handle error here.
       
}
    }
    return
strlen($header);
}

function
on_curl_write($ch, $data)
{
   
$bytes = strlen($data);
   
$GLOBALS['file_size'] += $bytes;
    if (
$GLOBALS['file_size'] > $GLOBALS['max_file_size']) {
       
// handle error here.
   
}
    return
$bytes;
}

$ch = curl_init();
$options = array(CURLOPT_URL        => 'http://www.php.net/',
        
CURLOPT_HEADER        => false,
        
CURLOPT_HEADERFUNCTION    => 'on_curl_header',
        
CURLOPT_WRITEFUNCTION    => 'on_curl_write');
curl_setopt_array($ch, $options);
curl_exec($ch);
// ...
?>
bendavis78 at gmail dot com
14.07.2006 0:58
You can use CURLOPT_HEADERFUNCTION  with a callback inside an object.  This makes is it easy to capture the headers for later use.  For example:

<?php
class Test
{
    public
$headers;

   
//...

   
public function exec($opts)
    {
       
$this->headers = array();
       
$opts[CURLOPT_HEADERFUNCTION] = array($this, '_setHeader');
       
$ch = curl_init();
       
curl_setopt_array($ch, $opts);
        return
curl_exec($ch);
    }

    private function
_setHeader($ch, $header)
    {
       
$this->headers[] = $header;
        return
strlen($header);
    }

   
}

$test = new Test();
$opts = array(
  
//... your curl opts here
);
$data = $test->exec($opts);
print_r($test->headers);
?>

...something like that

(This works in php v. 5.1.4)



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)