Unset curl CURLOPT_POSTFIELDS

I have a PHP class that is used to POST some data on the server and return the data back using the same open connection. The problem is that this code will try to get the POST data from the first request in the second 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 an unset method CURLOPT_POSTFIELDS. I tried using curl_setopt(self::$ecurl, CURLOPT_POSTFIELDS, null);, but curl will send the Posting 0 bytes...request header in any case .

Also note that I need to use the exact same connection, so I cannot create another connection through curl_init.

+4
source share
2 answers

Set CURLOPT_HTTPGETto trueuntil the last query.

PHP.net:

CURLOPT_HTTPGET

reset HTTP- GET. GET , , .

+4

curl curl_setopt_array

( ) .

$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);

, , , .

0

Source: https://habr.com/ru/post/1544809/


All Articles