Home  >  Article  >  Backend Development  >  How to use php curl_setopt_array function

How to use php curl_setopt_array function

藏色散人
藏色散人Original
2019-05-27 10:42:392300browse

php curl_setopt_array function is used to set options in batches for cURL transfer sessions, that is, to set options in batches for cURL transfer sessions. This function is useful for setting a large number of cURL options without having to call curl_setopt() repeatedly.

How to use php curl_setopt_array function

php How to use curl_setopt_array function?

curl_setopt_array — Set options in batches for cURL transfer sessions.

Description

bool curl_setopt_array ( resource $ch , array $options )

Set options for cURL transfer sessions in batches. This function is useful for setting a large number of cURL options without having 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 array key must be a valid curl_setopt() constant or its integer equivalent.

Return value

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

Example

Initialize a new cURL brilliance and crawl a web page.

<?php
// 创建一个新cURL资源
$ch = curl_init();
// 设置URL和相应的选项
$options = array(CURLOPT_URL => &#39;http://www.php.cn/&#39;,
                 CURLOPT_HEADER => false
                );
curl_setopt_array($ch, $options);
// 抓取URL并把它传递给浏览器
curl_exec($ch);
// 关闭cURL资源,并且释放系统资源
curl_close($ch);
?>

Earlier than PHP 5.1.3, this function can be simulated as follows:

Our equivalent implementation of curl_setopt_array()

<?php
if (!function_exists(&#39;curl_setopt_array&#39;)) {
   function curl_setopt_array(&$ch, $curl_options)
   {
       foreach ($curl_options as $option => $value) {
           if (!curl_setopt($ch, $option, $value)) {
               return false;
           } 
       }
       return true;
   }
}
?>

Note: In the case of curl_setopt(), passing an array to CURLOPT_POST will encode the data as multipart/form-data, whereas passing a URL-encoded string will encode the data as application/x-www-form-urlencoded. Encode.

The above is the detailed content of How to use php curl_setopt_array function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn