Posting data in PHP Curl
-
28-02-2021 - |
Question
I am trying to post the content in a file as a param using PHP CURL.
The data gets posted but Zoho opens a blank document instead of the one I'm trying to open.
I believe its because I am providing invalid input the $post_data['content']
variable.
$post_data['content'] = "@/c:/xampp/htdocs/site/a.doc";
Zoho opens a blank document when the content received is empty according to what it says in the ZOHO API.
My code is given below for reference. <
?php
$post_data['content'] = "@/c:/xampp/htdocs/site/a.doc";
$post_data['apikey'] = '[MY API KEY IS HERE]';
$post_data['output'] = 'url';
$post_data['filename'] = "a.doc";
$post_data['id'] = '12345678';
$post_data['format'] = "doc";
$post_data['saveurl'] = 'https://localhost/researchPortal/tmp/save.php';
$post_data['agentname'] = 'ZRemoteAgent';
$post_data['mode'] = "normaledit";
foreach ( $post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection = curl_init('https://exportwriter.zoho.com/remotedoc.im');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
echo $result;
//close the connection
curl_close($curl_connection);
?>
Solution
My guess would be change
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
to
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);
Im guessing converting it into a string is making curl not upload the file with the magic @ http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html