Source of: /manual/en/function.curl-setopt-array.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.curl.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.curl-setopt-array.php',
1 => 'curl_setopt_array',
),
'up' =>
array (
0 => 'ref.curl.php',
1 => 'cURL Functions',
),
'prev' =>
array (
0 => 'function.curl-multi-select.php',
1 => 'curl_multi_select',
),
'next' =>
array (
0 => 'function.curl-setopt.php',
1 => 'curl_setopt',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.curl-setopt-array" class="refentry">
<div class="refnamediv">
<h1 class="refname">curl_setopt_array</h1>
<p class="verinfo">(PHP 5 >= 5.1.3)</p><p class="refpurpose"><span class="refname">curl_setopt_array</span> — <span class="dc-title">Set multiple options for a cURL transfer</span></p>
</div>
<a name="function.curl-setopt-array.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">bool</span> <span class="methodname"><b>curl_setopt_array</b></span>
( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$ch</tt></span>
, <span class="methodparam"><span class="type">array</span> <tt class="parameter">$options</tt></span>
)</div>
<p class="para rdfs-comment">
Sets multiple options for a cURL session. This function is
useful for setting a large amount of cURL options without repetitively
calling <a href="function.curl-setopt.php" class="function">curl_setopt()</a>.
</p>
</div>
<a name="function.curl-setopt-array.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">ch</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">A cURL handle returned by
<a href="function.curl-init.php" class="function">curl_init()</a>.</p></dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">options</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
An <a href="language.types.array.php" class="type array">array</a> specifying which options to set and their values.
The keys should be valid <a href="function.curl-setopt.php" class="function">curl_setopt()</a> constants or
their integer equivalents.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.curl-setopt-array.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns <b><tt class="constant">TRUE</tt></b> if all options were successfully set. If an option could
not be successfully set, <b><tt class="constant">FALSE</tt></b> is immediately returned, ignoring any
future options in the <i><tt class="parameter">options</tt></i>
array.
</p>
</div>
<a name="function.curl-setopt-array.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1
Initializing a new cURL session and fetching a web page
</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// create a new cURL resource<br /></span><span style="color: #0000BB">$ch </span><span style="color: #007700">= </span><span style="color: #0000BB">curl_init</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// set URL and other appropriate options<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= array(</span><span style="color: #0000BB">CURLOPT_URL </span><span style="color: #007700">=> </span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">CURLOPT_HEADER </span><span style="color: #007700">=> </span><span style="color: #0000BB">false<br /> </span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">curl_setopt_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// grab URL and pass it to the browser<br /></span><span style="color: #0000BB">curl_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// close cURL resource, and free up system resources<br /></span><span style="color: #0000BB">curl_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
Prior to PHP 5.1.4 this function can be simulated with:
</p>
<p class="para">
</p><div class="example">
<p><b>Example #2 Our own implementation of <b>curl_setopt_array()</b></b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (!</span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'curl_setopt_array'</span><span style="color: #007700">)) {<br /> function </span><span style="color: #0000BB">curl_setopt_array</span><span style="color: #007700">(&</span><span style="color: #0000BB">$ch</span><span style="color: #007700">, </span><span style="color: #0000BB">$curl_options</span><span style="color: #007700">)<br /> {<br /> foreach (</span><span style="color: #0000BB">$curl_options </span><span style="color: #007700">as </span><span style="color: #0000BB">$option </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> if (!</span><span style="color: #0000BB">curl_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">, </span><span style="color: #0000BB">$option</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">)) {<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /> } <br /> }<br /> return </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div>
<a name="function.curl-setopt-array.notes"></a><div class="refsect1 notes">
<h3 class="title">Notes</h3>
<blockquote><p><b class="note">Note</b>:
As with <a href="function.curl-setopt.php" class="function">curl_setopt()</a>, passing an array to
<b><tt class="constant">CURLOPT_POST</tt></b> will encode the data as
<em class="emphasis">multipart/form-data</em>, while passing a
URL-encoded string will encode the data as
<em class="emphasis">application/x-www-form-urlencoded</em>.
<br />
</p></blockquote>
</div>
<a name="function.curl-setopt-array.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.curl-setopt.php" class="function" rel="rdfs-seeAlso">curl_setopt()</a> - Set an option for a cURL transfer</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>