我使用以下代码将图像上传到另一台服务器
$data = array('type'=>'new', 'file'=>'@'.realpath('../upload/test.jpg'));
$data = http_build_query($data);
$size = filesize('../upload/test.jpg');
$boundary = uniqid();
$delimiter = '----WebKitFormBoundary'.$boundary;
$header = array("X-Requested-With: XMLHttpRequest",
"x-csrf-token: $token",
"accept: application/json",
"Content-Type: multipart/form-data; boundary=$delimiter",
"Content-Length: $size");
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, "https://aaa.com/upload");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_REFERER, "https://aaa.com/new");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$html = curl_exec($ch);
var_dump($html);
运行此代码后,在25秒后出现错误bool(false)
如果我删除Content-Length: $size
,则运行2秒钟后,我从服务器获取错误文件为空。
但是,如果我的$header
完全是空的,我会得到空的响应或错误的