1

我正在使用 php 和 curl,我在使用enctype=multipart/form-data.

远程网页中的表单:

<form method="post" enctype="multipart/form-data" action="/account/login" id="login">
<label for="username">Username</label><input name="username" id="username"><br>
<label for="password">Password</label><input name="password" id="password" type="password"><br>
<input type="checkbox" name="keeplogged" id="keeplogged" value="on"><label for="keeplogged">Remember me</label><br>
<input type="submit" name="submit" value="Submit" />
</form>

数据(来自postFirefox):

POSTDATA =-----------------------------24464570528145
Content-Disposition: form-data; name="username"

fakeuser
-----------------------------24464570528145
Content-Disposition: form-data; name="password"

fakepass
-----------------------------24464570528145
Content-Disposition: form-data; name="keeplogged"

on
-----------------------------24464570528145
Content-Disposition: form-data; name="submit"

Submit
-----------------------------24464570528145--

我正在尝试使用 curl 登录:

function login($ref)
{
// Identify the URL:
$url = 'http://www.fakeSite.com/account/login';

$dt = array();
$dt['username'] = 'fakeUser';
$dt['password'] = 'fakePass';
$dt['keeplogged'] = 'on';
$dt['submit'] = 'Submit';

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dt);

$result = curl_exec($curl);

if(curl_errno($curl))//If there is any error
{
    echo 'Curl error: ' . curl_error($curl);
    exit();
}

curl_close($curl);

return $result;
}

但结果什么都没有!没有结果,没有错误,什么都没有!有人知道吗?

PS:我试图传递格式类似于 Firefox 的 postfields,但这并没有解决问题。

4

1 回答 1

0

我在使用enctype=multipart/form-data(Proxmox VE)的 API 方面遇到了一些问题。

尝试POST使用字符串而不是数组(使用http_build_query)添加数据:

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($dt));
于 2012-08-03T18:27:17.883 回答