Reputation: 435
I have an PHP class which is used to POST some data to a server, and GET some data back using the same open connection. The problem is that this code will try to POST data from 1st request, in the 2nd request...
curl_setopt(self::$ecurl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt(self::$ecurl, CURLOPT_POSTFIELDS, $data);
$request=curl_exec(self::$ecurl);
curl_setopt(self::$ecurl, CURLOPT_CUSTOMREQUEST, "GET");
$request=curl_exec(self::$ecurl);
So i need the way to unset CURLOPT_POSTFIELDS
. I tried to use curl_setopt(self::$ecurl, CURLOPT_POSTFIELDS, null);
, but anyway curl send Posting 0 bytes...
in request's header.
Also please note, that i need to use exactly the same connection, so I can't create another connection via curl_init.
Upvotes: 7
Views: 2948
Reputation: 817
You could add your curl options into an array and use curl_setopt_array
This will make it easy to unset (or set) your options array elements.
$options = array(
CURLOPT_URL => 'http://www.example.com/',
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => $post_data
);
// condition on which you want to unset
if ($condition == true) {
unset($options[CURLOPT_POSTFIELDS]);
}
...
curl_setopt_array($ch, $options);
// grab URL and pass it to the browser
curl_exec($ch);
I understand this was an old question but still think this method can be handy.
Upvotes: -1