Setting Multiple Options

If you’re working with PHP 5.1.3+ you have access to the curl_setopt_array function, which allows you to pass in an associative array of setting-value pairs to set with a single function call. If you’re working with an older version of PHP, the function is relatively easy to write.

Using this function results in not only less and cleaner code, but in the case of the native C function it also results in fewer function calls and by proxy improved performance.

<?php
if (!function_exists('curl_setopt_array')) { function curl_setopt_array($ch, $options) { foreach ($options as $setting => $value) { curl_setopt($ch, $setting, $value);
}
}
}
$ch = curl_init();
$opts = array(
CURLOPT_URL => 'http://localhost.example', CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $opts);
$response = curl_exec($ch); curl_close($ch);
?>

© cURL Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (30.08.2014)
Views: 322 | Rating: 0.0/0
Total comments: 0
avatar