PHP Curl无法重现XHR上传

时间:2017-03-21 16:16:18

标签: php ajax curl upload xmlhttprequest

我想将视频文件上传到网站。默认情况下在浏览器中 我可以上传视频,但是当我尝试通过curl在php中执行此操作时出现问题,因为我没有收到视频代码(vcode)作为回应。

这是XHR请求,这种方式正常,原始上传请求:

  var formData = new FormData();
  formData.append("sid", "MYSID");
  formData.append("cmd", "uploadVideoFile");
  formData.append("uploadfile", uFile, uFile.name);

  var oXHR = new XMLHttpRequest();
  oXHR.open("POST", upload_url);
  oXHR.send(formData);

请求标题:

Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Content-Length  123456
Content-Type multipart/form-data; boundary=---------------------------26463276212588
Host    upload.somehost.com
Origin  http://somehost.com
Referer http://somehost.com/upload
User-Agent  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0

这是POST:

    -----------------------------26463276212588
Content-Disposition: form-data; name="sid"

MYSID
-----------------------------26463276212588
Content-Disposition: form-data; name="cmd"

uploadVideoFile
-----------------------------26463276212588
Content-Disposition: form-data; name="uploadfile"; filename="test.mp4"
Content-Type: video/mp4

发布后的回复(成功时):

{"response":{"code":0,"vcode":"dBG5XL2VdibeKAG4"}}

此代码为:dBG5XL2VdibeKAG4如果我通过curl上传视频,则无法收到该内容。

CURL PHP代码:

$file = "/var/www/html/dl/big.mp4";
$data = array('sid' => 'MYSID', 'cmd' => 'uploadVideoFiles', 'uploadfile' => '@' . realpath($file));
$lenght = sizeof($data);

$request = curl_init('http://uploadurl/uploadpage');
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($request, CURLOPT_POSTFIELDS, $data);
curl_setopt($request, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type:multipart/form-data',                                                                                
    'Content-Length: ' . strlen($lenght))                                                                       
);                       
echo curl_exec($request);
curl_close($request);

我没有收到vcode时的响应:

{"response":{"code":0,"vcode":""}} 

所以我认为CURL代码在某处不正确,或者我没有像XHR发送请求那样发送请求。

0 个答案:

没有答案