note 117009 modified in curl.examples-basic by cmb

From: Date: Thu, 11 Nov 2021 10:19:01 +0000
Subject: note 117009 modified in curl.examples-basic by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-217183@lists.php.net to get a copy of this message
It is important to notice that when using curl to post form data and you use an array for CURLOPT_POSTFIELDS option, the post will be in multipart format <?php $params=['name'=>'John', 'surname'=>'Doe', 'age'=>36]; $defaults = array( CURLOPT_URL => 'http://myremoteservice/', CURLOPT_POST => true, CURLOPT_POSTFIELDS => $params, ); $ch = curl_init(); curl_setopt_array($ch, ($options + $defaults)); ?> This produce the following post header: --------------------------fd1c4191862e3566 Content-Disposition: form-data; name="name" Jhon --------------------------fd1c4191862e3566 Content-Disposition: form-data; name="surnname" Doe --------------------------fd1c4191862e3566 Content-Disposition: form-data; name="age" 36 --------------------------fd1c4191862e3566-- Setting CURLOPT_POSTFIELDS as follow produce a standard post header CURLOPT_POSTFIELDS => http_build_query($params), Which is: name=John&surname=Doe&age=36 This caused me 2 days of debug while interacting with a java service which was sensible to this difference, while the equivalent one in php got both format without problem. --was-- It is important to notice that when using curl to post form data and you use an array for CURLOPT_POSTFIELDS option, the post will be in multipart format <?php $params=['name'=>'John', 'surname'=>'Doe', 'age'=>36) $defaults = array( CURLOPT_URL => 'http://myremoteservice/', CURLOPT_POST => true, CURLOPT_POSTFIELDS => $params, ); $ch = curl_init(); curl_setopt_array($ch, ($options + $defaults)); ?> This produce the following post header: --------------------------fd1c4191862e3566 Content-Disposition: form-data; name="name" Jhon --------------------------fd1c4191862e3566 Content-Disposition: form-data; name="surnname" Doe --------------------------fd1c4191862e3566 Content-Disposition: form-data; name="age" 36 --------------------------fd1c4191862e3566-- Setting CURLOPT_POSTFIELDS as follow produce a standard post header CURLOPT_POSTFIELDS => http_build_query($params), Which is: name=John&surname=Doe&age=36 This caused me 2 days of debug while interacting with a java service which was sensible to this difference, while the equivalent one in php got both format without problem. https://php.net/manual/en/curl.examples-basic.php

« previous php.notes (#217183) next »