note 118000 added to function.curl-setopt-array

From: Date: Thu, 17 Sep 2015 06:58:58 +0000
Subject: note 118000 added to function.curl-setopt-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-203795@lists.php.net to get a copy of this message
If you are writing a mini API for your library, and if you are doing merging of options, remember to use the union operator (+) ! So something like this will definitely fail. This is because array_merge effectively resets all the keys in the array into running numbers: <?php function post($url, $options = array) { $ch = curl_init(); curl_setopt_array($ch, array_merge(array( CURLOPT_HEADER => 1, CURLOPT_RETURNTRANSFER => 1, ..... ))); ?> Rather, this is the correct way of doing it: <?php function post($url, $options = array) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_HEADER => 1, CURLOPT_RETURNTRANSFER => 1, ..... ) + (array) $options); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 220.255.1.73 ---- Manual Page -- http://php.net/manual/en/function.curl-setopt-array.php Edit -- https://master.php.net/note/edit/118000 Del: integrated -- https://master.php.net/note/delete/118000/integrated Del: useless -- https://master.php.net/note/delete/118000/useless Del: bad code -- https://master.php.net/note/delete/118000/bad+code Del: spam -- https://master.php.net/note/delete/118000/spam Del: non-english -- https://master.php.net/note/delete/118000/non-english Del: in docs -- https://master.php.net/note/delete/118000/in+docs Del: other reasons-- https://master.php.net/note/delete/118000 Reject -- https://master.php.net/note/reject/118000 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203795) next »