简体   繁体   English

什么是PHP curl等同于命令行curl?

[英]what is the php curl equalent to commandline curl?

I've tested this command with curl in the command prompt and it works and does what I want it to do. 我已经在命令提示符下使用curl测试了此命令,它可以工作并且可以执行我想要的操作。

curl -T filetoupload.tmp http://example.com -H "Accept: text/html" -H "Content-type: appliction/pdf" > filename.htm

I've then tried to express this in php(I run PHP v5.5) and wrote this code, but the remote server doesn't like it so it obviously isn't doing the same thing. 然后,我试图用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);

what am I doing wrong? 我究竟做错了什么?

curl -T filetoupload.tmp uploads the file as the RAW post body. curl -T filetoupload.tmp将文件上传为RAW帖子正文。 Your PHP code is sending the file with headers, like you were posting a form with multipart/form-data . 您的PHP代码正在发送带有标头的文件,就像您发布带有multipart/form-data

You need to set the raw post body in your PHP code. 您需要在PHP代码中设置原始发布主体。 Also, it looks like curl -T uses PUT instead of POST . 同样,看起来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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM