PHP CURL 登录时出现问题


PHP CURL Problems with login

我在使用 cURL 进行日志记录时遇到问题。这是我的代码:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', '1');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://mysite.net/post/login.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/test.cookie'); // replace this with /tmp or something like that
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
    'accountName' => 'name',
    'accountPassword' => 'password'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $output; 
?> 

我的代码有什么问题?我无法登录。对不起我的英语。

有表单 mysite.net

<form class="form-horizontal" method="post" action="post/login.php">
<input type="hidden" name="securitytoken" value="672385374d4ed88e5ac0d6aaf29ae5f25280ed549a01f1.65943444">
<fieldset>
<h2>Account Login</h2>
<div class="control-group">
<label class="control-label" for="accountName">Account name</label>
<div class="controls">
<input type="text" class="input-large" id="accountName" name="accountName">
</div>
</div>
<div class="control-group">
<label class="control-label" for="accountPassword">Account password</label>
<div class="controls">
<input type="password" class="input-large" id="accountPassword" name="accountPassword">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Submit</button>&nbsp;
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>

有什么想法吗?我做错了什么?我尝试更改代码,使用示例代码。还是没有。

有几个问题。

  1. 您需要提供securitytoken以及accountNameaccountPassword。为此,您需要抓取页面,提取令牌并使用凭据发布
  2. 表单通常使用什么 enctype(通过浏览器)?您可以通过任何浏览器流量插件查找它。如果提供$data数组,cURL 将使用multipart/form-data 。如果是字符串 - 内容类型将为application/x-www-form-urlencoded