<p>How do you pass <code>$_POST</code> values to a page using <code>cURL</code>?</p>

[{'answer_id': 28411, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 8, 'selected': True, 'text': '<p>Should work fine.</p>\n\n<pre><code>$data = array(\'name\' =&gt; \'Ross\', \'php_master\' =&gt; true);\n\n// You can POST a file by prefixing with an @ (for &lt;input type="file"&gt; fields)\n$data[\'file\'] = \'@/home/user/world.jpg\';\n\n$handle = curl_init($url);\ncurl_setopt($handle, CURLOPT_POST, true);\ncurl_setopt($handle, CURLOPT_POSTFIELDS, $data);\ncurl_exec($handle);\ncurl_close($handle)\n</code></pre>\n\n<p>We have two options here, <code>CURLOPT_POST</code> which turns HTTP POST on, and <code>CURLOPT_POSTFIELDS</code> which contains an array of our post data to submit. This can be used to submit data to <code>POST</code> <code>&lt;form&gt;</code>s.</p>\n\n<hr>\n\n<p>It is important to note that <code>curl_setopt($handle, CURLOPT_POSTFIELDS, $data);</code> takes the $data in two formats, and that this determines how the post data will be encoded.</p>\n\n<ol>\n<li><p><code>$data</code> as an <code>array()</code>: The data will be sent as <code>multipart/form-data</code> which is not always accepted by the server.</p>\n\n<pre><code>$data = array(\'name\' =&gt; \'Ross\', \'php_master\' =&gt; true);\ncurl_setopt($handle, CURLOPT_POSTFIELDS, $data);\n</code></pre></li>\n<li><p><code>$data</code> as url encoded string: The data will be sent as <code>application/x-www-form-urlencoded</code>, which is the default encoding for submitted html form data.</p>\n\n<pre><code>$data = array(\'name\' =&gt; \'Ross\', \'php_master\' =&gt; true);\ncurl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));\n</code></pre></li>\n</ol>\n\n<p>I hope this will help others save their time.</p>\n\n<p>See:  </p>\n\n<ul>\n<li><a href="http://www.php.net/manual/en/function.curl-init.php" rel="noreferrer"><code>curl_init</code></a></li>\n<li><a href="http://www.php.net/manual/en/function.curl-setopt.php" rel="noreferrer"><code>curl_setopt</code></a></li>\n</ul>\n'}, {'answer_id': 28438, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 5, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/28395/passing-post-values-with-curl#28411">Ross has the right idea</a> for POSTing the usual parameter/value format to a url.</p>\n\n<p>I recently ran into a situation where I needed to POST some XML as Content-Type "text/xml" without any parameter pairs so here\'s how you do that:</p>\n\n<pre><code>$xml = \'&lt;?xml version="1.0"?&gt;&lt;stuff&gt;&lt;child&gt;foo&lt;/child&gt;&lt;child&gt;bar&lt;/child&gt;&lt;/stuff&gt;\';\n$httpRequest = curl_init();\n\ncurl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, 1);\ncurl_setopt($httpRequest, CURLOPT_HTTPHEADER, array("Content-Type:  text/xml"));\ncurl_setopt($httpRequest, CURLOPT_POST, 1);\ncurl_setopt($httpRequest, CURLOPT_HEADER, 1);\n\ncurl_setopt($httpRequest, CURLOPT_URL, $url);\ncurl_setopt($httpRequest, CURLOPT_POSTFIELDS, $xml);\n\n$returnHeader = curl_exec($httpRequest);\ncurl_close($httpRequest);\n</code></pre>\n\n<p>In my case, I needed to parse some values out of the HTTP response header so you may not necessarily need to set <code>CURLOPT_RETURNTRANSFER</code> or <code>CURLOPT_HEADER</code>.</p>\n'}, {'answer_id': 17662491, 'author': 'Sapnandu', 'author_id': 746270, 'author_profile': 'https://Stackoverflow.com/users/746270', 'pm_score': 2, 'selected': False, 'text': '<pre><code>$query_string = "";\n\nif ($_POST) {\n    $kv = array();\n    foreach ($_POST as $key =&gt; $value) {\n        $kv[] = stripslashes($key) . "=" . stripslashes($value);\n    }\n    $query_string = join("&amp;", $kv);\n}\n\nif (!function_exists(\'curl_init\')){\n    die(\'Sorry cURL is not installed!\');\n}\n\n$url = \'https://www.abcd.com/servlet/\';\n\n$ch = curl_init();\n\ncurl_setopt($ch, CURLOPT_URL, $url);\ncurl_setopt($ch, CURLOPT_POST, count($kv));\ncurl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);\n\ncurl_setopt($ch, CURLOPT_HEADER, FALSE);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);\ncurl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);\n\n$result = curl_exec($ch);\n\ncurl_close($ch);\n</code></pre>\n'}, {'answer_id': 20651956, 'author': 'Mohammad Faisal Islam', 'author_id': 1615812, 'author_profile': 'https://Stackoverflow.com/users/1615812', 'pm_score': 0, 'selected': False, 'text': '<pre><code>&lt;?php\n    function executeCurl($arrOptions) {\n\n        $mixCH = curl_init();\n\n        foreach ($arrOptions as $strCurlOpt =&gt; $mixCurlOptValue) {\n            curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue);\n        }\n\n        $mixResponse = curl_exec($mixCH);\n        curl_close($mixCH);\n        return $mixResponse;\n    }\n\n    // If any HTTP authentication is needed.\n    $username = \'http-auth-username\';\n    $password = \'http-auth-password\';\n\n    $requestType = \'POST\'; // This can be PUT or POST\n\n    // This is a sample array. You can use $arrPostData = $_POST\n    $arrPostData = array(\n        \'key1\'  =&gt; \'value-1-for-k1y-1\',\n        \'key2\'  =&gt; \'value-2-for-key-2\',\n        \'key3\'  =&gt; array(\n                \'key31\'   =&gt; \'value-for-key-3-1\',\n                \'key32\'   =&gt; array(\n                    \'key321\' =&gt; \'value-for-key321\'\n                )\n        ),\n        \'key4\'  =&gt; array(\n            \'key\'   =&gt; \'value\'\n        )\n    );\n\n    // You can set your post data\n    $postData = http_build_query($arrPostData); // Raw PHP array\n\n    $postData = json_encode($arrPostData); // Only USE this when request JSON data.\n\n    $mixResponse = executeCurl(array(\n        CURLOPT_URL =&gt; \'http://whatever-your-request-url.com/xyz/yii\',\n        CURLOPT_RETURNTRANSFER =&gt; true,\n        CURLOPT_HTTPGET =&gt; true,\n        CURLOPT_VERBOSE =&gt; true,\n        CURLOPT_AUTOREFERER =&gt; true,\n        CURLOPT_CUSTOMREQUEST =&gt; $requestType,\n        CURLOPT_POSTFIELDS  =&gt; $postData,\n        CURLOPT_HTTPHEADER  =&gt; array(\n            "X-HTTP-Method-Override: " . $requestType,\n            \'Content-Type: application/json\', // Only USE this when requesting JSON data\n        ),\n\n        // If HTTP authentication is required, use the below lines.\n        CURLOPT_HTTPAUTH =&gt; CURLAUTH_BASIC,\n        CURLOPT_USERPWD  =&gt; $username. \':\' . $password\n    ));\n\n    // $mixResponse contains your server response.\n</code></pre>\n'}, {'answer_id': 22474056, 'author': 'Julian', 'author_id': 1280520, 'author_profile': 'https://Stackoverflow.com/users/1280520', 'pm_score': 2, 'selected': False, 'text': '<p>Another simple PHP example of using cURL:</p>\n\n<pre><code>&lt;?php\n    $ch = curl_init();                    // Initiate cURL\n    $url = "http://www.somesite.com/curl_example.php"; // Where you want to post data\n    curl_setopt($ch, CURLOPT_URL,$url);\n    curl_setopt($ch, CURLOPT_POST, true);  // Tell cURL you want to post something\n    curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=value1&amp;var2=value2&amp;var_n=value_n"); // Define what you want to post\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the output in string format\n    $output = curl_exec ($ch); // Execute\n\n    curl_close ($ch); // Close cURL handle\n\n    var_dump($output); // Show output\n?&gt;\n</code></pre>\n\n<p>Example found here: <a href="http://devzone.co.in/post-data-using-curl-in-php-a-simple-example/" rel="nofollow">http://devzone.co.in/post-data-using-curl-in-php-a-simple-example/</a></p>\n\n<p>Instead of using <code>curl_setopt</code> you can use <code>curl_setopt_array</code>. </p>\n\n<p><a href="http://php.net/manual/en/function.curl-setopt-array.php" rel="nofollow">http://php.net/manual/en/function.curl-setopt-array.php</a></p>\n'}, {'answer_id': 37318643, 'author': 'Aniket B', 'author_id': 6355133, 'author_profile': 'https://Stackoverflow.com/users/6355133', 'pm_score': 2, 'selected': False, 'text': "<pre><code>$url='Your url'; // Specify your url\n$data= array('parameterkey1'=&gt;value,'parameterkey2'=&gt;value); // Add parameters in key value\n$ch = curl_init(); // Initialize cURL\ncurl_setopt($ch, CURLOPT_URL,$url);\ncurl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_exec($ch);\ncurl_close($ch);\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/28395', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2863/']