PHP curl上传图像返回bool(false)

时间:2018-12-23 19:15:23

标签: php curl

我使用以下代码将图像上传到另一台服务器

$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完全是空的,我会得到空的响应或错误的

0 个答案:

没有答案