Задача: создать php/curl скипт для афтоматической заливки файлов на multiupload.com.
Обычно юзаю firefox + LiveHTTP Add-on для того что бы посмотреть что передаётся через POST и делаю аналогично. На этот раз возник вопрос: что значит когда в POST методе при Content-type: multipart/form-data используется конструкция типа `Content-Disposition: form-data; name=«file_0»; filename=«test»`? Т.е. непонятно, у этого поля 2 имени что ли? Т.е., имеем HTTP заголовок такого вида (некоторые поля удалены для краткости):
Т.е. вопрос касается 3-его сверху поля (после полей name=«UPLOAD_IDENTIFIER» и name=«u»). Обратите внимание: name=«file_0»; filename=«test». Что это значит и как закодить на php/curl? Пока что написал вот что, но оно не работает (файл не заливается):
<?php
class Curl {
public $cookies = array();
public $ch;
public function __construct() {
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_HEADER, 1);
curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
}
public function close() {
curl_close($this->ch);
}
public function cookie_string() {
$cookies_r = array();
foreach ($this->cookies as $cookie_name => $cookie_val) {
$cookies_r[] = urlencode($cookie_name)."=".urlencode($cookie_val);
}
$cookies_r = join('; ', $cookies_r);
return $cookies_r;
}
public function request($url, $post = false, $postvars = array()) {
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_COOKIE, Curl::cookie_string());
if ($post) {
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postvars);
}
$serv_resp = curl_exec($this->ch);
$h_end = strpos($serv_resp, "\r\n\r\n");
$header = substr($serv_resp, 0, $h_end);
$content = substr($serv_resp, $h_end + 4);
if (preg_match_all('/Set-Cookie:\s*([^=]+)=([^;]*)/is', $header, $matches)) {
foreach ($matches[1] as $n => $cookie_name) {
$cookie_value = $matches[2][$n];
$this->cookies[$cookie_name] = $cookie_value;
}
}
return $content;
}
}
$ch = new Curl();
$html = $ch->request("http://multiupload.com/login");
$html = $ch->request(
"http://multiupload.com/login",
true,
"username=".urlencode($config['multiupload.com_login'])."&password=".urlencode($config['multiupload.com_password'])
);
$html = $ch->request("http://multiupload.com");
if (!preg_match_all('/(<form[^>]+>)/is', $html, $matches)) die("No forms on multiupload.com page\n");
$upload_identifier = '';
foreach ($matches[1] as $form_tag) {
if (preg_match('/UPLOAD_IDENTIFIER=(\d+)/is', $form_tag, $matches)) {
$upload_identifier = $matches[1];
break;
}
}
if ($upload_identifier == '') die("No UPLOAD_IDENTIFIER\n");
$html = $ch->request(
"http://multiupload.com/upload/?UPLOAD_IDENTIFIER=$upload_identifier",
true,
array(
"UPLOAD_IDENTIFIER" => $upload_identifier,
"u" => urlencode($ch->cookies['u']),
// "file_0;filename=\"test\"" => "@/home/par/projects/new_nzbs/uploads/test",
"file_0" => "@/home/par/projects/new_nzbs/uploads/test",
"description_0" => "",
"fetchfield0" => "",
"fetchdesc0" => "",
"file_1" => "",
"description_1" => "",
"file_2" => "",
"description_2" => "",
"file_3" => "",
"description_3" => "",
"file_4" => "",
"description_4" => "",
"file_5" => "",
"description_5" => "",
"file_6" => "",
"description_6" => "",
"file_7" => "",
"description_7" => "",
"file_8" => "",
"description_8" => "",
"file_9" => "",
"description_9" => "",
"fetchfield1" => "http://",
"fetchdesc1" => "",
"fetchfield2" => "http://",
"fetchdesc2" => "",
"fetchfield3" => "http://",
"fetchdesc3" => "",
"fetchfield4" => "http://",
"fetchdesc4" => "",
"fetchfield5" => "http://",
"fetchdesc5" => "",
"fetchfield6" => "http://",
"fetchdesc6" => "",
"fetchfield7" => "http://",
"fetchdesc7" => "",
"fetchfield8" => "http://",
"fetchdesc8" => "",
"fetchfield9" => "http://",
"fetchdesc9" => "",
"service_1" => "1",
"service_7" => "1",
"service_9" => "1",
"fromemail" => "",
"toemail" => "",
"rsaccount" => "C",
"username_5" => "",
"password_5" => "",
"remember_5" => "1",
"username_1" => "megaupload.com username",
"password_1" => "megaupload.com password",
"remember_1" => "1",
"username_7" => "depositfiles username",
"password_7" => "depositfiles password",
"remember_7" => "1",
"username_9" => "hotfile username",
"password_9" => "hotfile password",
"remember_9" => "1",
"username_6" => "",
"password_6" => "",
"remember_6" => "1",
"username_10" => "",
"password_10" => "",
"remember_10" => "1",
)
);
echo "|$html|\n";
$ch->close();
?>
Если в массив полей написать что то типа «file_0; filename=\„test\“» => «@/path/to/file/for/upload», то curl при curl_setopt($ch, CURLOPT_VERBOSE, 1) вообще пишет что то типа что не смог скомпоновать post request и естественно так же ничего не заливается.
PS. Искал ответ в RFC, но не нашёл. Прошу знающих людей подсказать.