[英]what is the php curl equalent to commandline curl?
我已经在命令提示符下使用curl测试了此命令,它可以工作并且可以执行我想要的操作。
curl -T filetoupload.tmp http://example.com -H "Accept: text/html" -H "Content-type: appliction/pdf" > filename.htm
然后,我试图用php(我运行PHP v5.5)表示这一点,并编写了这段代码,但是远程服务器不喜欢它,因此显然它没有做同样的事情。
$ch = curl_init("http://example.com");
$cfile = curl_file_create('filetoupload.tmp', 'application/pdf', 'filename');
$data['file'] = $cfile;
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/pdf',
'Accept: text/html'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
我究竟做错了什么?
curl -T filetoupload.tmp
将文件上传为RAW帖子正文。 您的PHP代码正在发送带有标头的文件,就像您发布带有multipart/form-data
。
您需要在PHP代码中设置原始发布主体。 同样,看起来curl -T
使用PUT
而不是POST
。
$ch = curl_init("http://example.com");
$file = fopen('filetoupload.tmp', 'r');
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize('filetoupload.tmp'));
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/pdf',
'Accept: text/html'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
fclose($file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.