在 PHP Curl 中发布数据
我正在尝试使用 PHP CURL 将文件中的内容作为参数发布。
数据已发布,但 Zoho 打开一个空白文档,而不是我尝试打开的文档。
我相信这是因为我向 $post_data['content']
变量提供了无效输入。
$post_data['content'] = "@/c:/xampp/htdocs/site/a.doc";
当收到的内容为空时,Zoho 将根据其内容显示打开一个空白文档佐霍API。
下面给出我的代码供参考。 <
?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);
?>
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);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是将
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
更改为
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);
我猜将其转换为字符串是使curl不使用magic@上传文件
http://dtbaker.com.au /random-bits/uploading-a-file-using-curl-in-php.html
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