我有一个非常复杂的任务,我需要帮助。也许有人能帮上忙,而且找到了丢失的线索。
我需要请求一个具有curl的php表单页面(并发送cookie)。所有这些都很好,也很有效。但是表单页搜索一个$_FILE'picture'却找不到它。
我以前试过用一个简单的空的“图片”名和“图片”=> @。
我可以发送一个“真实”的文件名从我的办公桌和创建一个卷曲文件对象,但我不想添加一个文件。
如果我有一个空的上传表格,应该是一样的。
我想要:
$_FILE = Array
(
[bild] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
如果我请求PHP页面,我如何使用curl强制这样做?
我用这样的方法尝试过:PHP Send local file by cURL和它的工作。但是我不想发送文件,我只会强制文件数组传递表单。
有什么想法吗?提前感谢您的帮助!
这是当前/最后一个状态:
<?php
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/testpage.php');
curl_setopt ($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: multipart/form-data'
)
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
curl_close($ch);
?>
并仅取得成果:
$_POST: Array
(
[name] => Foo
[file] => @
)
$_FILES: Array
(
)
发布于 2015-09-10 14:17:05
您可以手动构建原始的post数据。下面是一个例子
$file = 'empty.txt';
$ending = "\r\n";
$boundary = md5(microtime());
$fullBoundary = sprintf("--%s%s", $boundary, $ending);
$body = '';
$body .= $fullBoundary;
$body .= "Content-Disposition: form-data; name=\"name\"" . $ending . $ending;
$body .= "Foo" . $ending;
$body .= $fullBoundary;
$body .= "Content-Disposition: form-data; name=\"file\"; filename=\"$file\"" . $ending;
$body .= "Content-Type: text/plain; charset=utf8" . $ending . $ending;
$body .= chunk_split("contents here") . $ending;
$body .= "--" . $boundary . "--" . $ending . $ending;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data; boundary=".$boundary)
);
curl_setopt($ch, CURLOPT_URL, "http://localhost/testpage.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
这导致了一个
--f47db631bcd2c6ca1de88f95da0e132a
Content-Disposition: form-data; name="name"
Foo
--f47db631bcd2c6ca1de88f95da0e132a
Content-Disposition: form-data; name="file"; filename="empty.txt"
Content-Type: text/plain; charset=utf8
contents here
--f47db631bcd2c6ca1de88f95da0e132a--
然后是服务器端,这是输出
$_POST Array
(
[name] => Foo
)
$_FILES Array
(
[file] => Array
(
[name] => empty.txt
[type] => text/plain
[tmp_name] => /tmp/phpdTOn9n
[error] => 0
[size] => 13
)
)
https://stackoverflow.com/questions/32511502
复制