note 24709 deleted from function.curl-setopt by cmb
| From: | cmb@php.net | Date: | Sun, 29 Dec 2019 10:20:46 +0000 |
| Subject: | note 24709 deleted from function.curl-setopt by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213674@lists.php.net to get a copy of this message | ||
Note Submitter: paul at zgtec dot com
----
To make a POST in multipart/form-data mode
this worked for me, the " \n" at the end of the variables was very important on my OS X
server.
<?php
$file = "file_to_upload.txt";
$submit_url = "http://www.example.com/upload_page.php";
$formvars = array("cc"=>"us \n");
$formvars[variable_1] = "bla bla \n";
$formvars[variable_2] = "bla bla \n";
$formvars[variable_3] = "bla bla \n";
$formvars[variable_4] = "bla bla \n";
$formvars[upfile] = "@$file"; // "@" causes cURL to send as file and not string
(I believe)
// init curl handle
$ch = curl_init($submit_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); //initiates cookie file if
needed
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); // Uses cookies from previous
session if exist
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.net"); //if server needs to think
this post came from elsewhere
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);
// perform post
echo $pnp_result_page = curl_exec($ch);
curl_close ($ch);
?>