簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM