📄 function.curl-setopt-array.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Set multiple options for a cURL transfer</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.curl-multi-select.html">curl_multi_select</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.curl-setopt.html">curl_setopt</a></div> <div class="up"><a href="ref.curl.html">cURL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><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> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>curl_setopt_array</b></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.html" class="function">curl_setopt()</a>. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt><span class="term"><i><tt class="parameter">ch</tt></i></span><dd><p class="para">A cURL handle returned by <a href="function.curl-init.html" class="function">curl_init()</a>.</p></dd></dt> <dt> <span class="term"><i><tt class="parameter">options</tt></i></span> <dd> <p class="para"> An <a href="language.types.array.html" class="type array">array</a> specifying which options to set and their values. The keys should be valid <a href="function.curl-setopt.html" class="function">curl_setopt()</a> constants or their integer equivalents. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> if all options were successfully set. If an option could not be successfully set, <b><tt>FALSE</tt></b> is immediately returned, ignoring any future options in the <i><tt class="parameter">options</tt></i> array. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Initializing a new cURL session and fetching a web page </b></p> <div class="example-contents"><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 class="para"> Prior to PHP 5.1.4 this function can be simulated with: </p> <p class="para"> <div class="example"> <p><b>Example #2 Our own implementation of <b>curl_setopt_array()</b></b></p> <div class="example-contents"><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> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.curl-setopt.html" class="function" rel="rdfs-seeAlso">curl_setopt()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.curl-multi-select.html">curl_multi_select</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.curl-setopt.html">curl_setopt</a></div> <div class="up"><a href="ref.curl.html">cURL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -