PHP CURL POST通过CURLOPT_FOLLOWLOCATION重定向后获得身份验证并发送POST请求

时间:2017-07-28 18:47:32

标签: php authentication redirect curl

要在网站上创建新用户,我需要向此网址发送POST请求:

https://wifinext.internavigare.com/prepagataAnagrafica/creaUtente/

发送字段“Prepagata_codice = [selected username]”和“Prepagata_password = [selected password]”

我需要发送POST请求的页面需要身份验证并重定向到登录页面。

我使用以下代码,但我无法进行身份验证,而是将其重定向到登录页面。

我的问题是如何在重定向和发送POST请求后处理身份验证?

我已尝试过所有内容,但我无法找到解决方案,请非常感谢任何帮助。

// Set the URL for page to send the post request for create a new user
$url = "https://wifinext.internavigare.com/prepagataAnagrafica/creaUtente/";

// Set username and password for authentication
$username = 'myuser';
$password = 'mypassword';

// create a new cURL resource
$ch = curl_init($url);

# Set the cURL options.
$options = [
    CURLOPT_COOKIEFILE      => 'cookies.txt',
    CURLOPT_COOKIEJAR       => 'cookies.txt',
    CURLOPT_RETURNTRANSFER  => 1,
    CURLOPT_FOLLOWLOCATION  => 1,
    CURLOPT_SSL_VERIFYPEER  => 0,
    CURLOPT_SSL_VERIFYHOST  => 0,
    CURLOPT_USERPWD         => $username . ":" . $password,
    CURLOPT_POST            => 1,
    CURLOPT_POSTFIELDS      => "Prepagata_codice=" . $_GET[user] . "&Prepagata_password=" . $_GET[pass]
];
curl_setopt_array($ch, $options);

// Do request
$response = curl_exec($ch);
// Get status code
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Show statusCode and response
echo $statusCode;
echo $response;

// close cURL
curl_close($ch);

非常感谢你的回答。我现在能够登录发送字段而不使用CURLOPT_USERPWD。不过也许我的问题并不清楚。我在我的问题中发布的URL不是用于登录,而是用于在其系统上添加新的WIFi用户。 Prepagata [code]和Prepagata [password]是指定URL的页面中预期的字段,以便添加新用户。现在我可以使用以下代码登录:

$url = "https://wifinext.internavigare.com/userGroups/";

// Set username and password for authentication
$username = 'myuser';
$password = 'mypassword';

// create a new cURL resource
$ch = curl_init($url);

# Set the cURL options.
$options = [
    CURLOPT_COOKIEFILE      => 'cookies.txt',
    CURLOPT_COOKIEJAR       => 'cookies.txt',
    CURLOPT_RETURNTRANSFER  => 1,
    CURLOPT_FOLLOWLOCATION  => 1,
    CURLOPT_SSL_VERIFYPEER  => 0,
    CURLOPT_SSL_VERIFYHOST  => 0,
    CURLOPT_USERPWD         => $username . ":" . $password,
    CURLOPT_POST            => 1,
    CURLOPT_POSTFIELDS      => "UserGroupsUser[username]=" . $username . "&UserGroupsUser[password]=" . $password
];

但是当我将下一个POST请求发送到URL $ url =“https://wifinext.internavigare.com/prepagataAnagrafica/creaUtente/”时;

使用此代码:

$user = $_GET[user];
$pass = $_GET[pass];
$url = "https://wifinext.internavigare.com/prepagataAnagrafica/creaUtente/";
$ch = curl_init($url);
$options = [
    CURLOPT_COOKIEFILE      => 'cookies.txt',
    CURLOPT_COOKIEJAR       => 'cookies.txt',
    CURLOPT_RETURNTRANSFER  => 1,
    CURLOPT_FOLLOWLOCATION  => 1,
    CURLOPT_SSL_VERIFYPEER  => 0,
    CURLOPT_SSL_VERIFYHOST  => 0,
    CURLOPT_USERPWD         => $username . ":" . $password,
    CURLOPT_POST            => 1,
    CURLOPT_POSTFIELDS      => "Prepagata[codice]=" . $user . "&Prepagata[password]=" . $pass
];
curl_setopt_array($ch, $options);

我刚刚被重定向到页面以添加新用户,但未添加用户。

1 个答案:

答案 0 :(得分:0)

他们不使用curl原生支持的登录方案,因此您可以放弃此部分CURLOPT_USERPWD => $username . ":" . $password - 充其量,浪费带宽,最坏的情况是,它会混淆Web服务器并拒绝登录。

引用:sending the fields "Prepagata_codice=[chosen username]" and "Prepagata_password=[chosen password]" - 不,这根本不是我得到的。在我的测试中,字段是

$params = array (
        'UserGroupsUser' => array (
                'username' => $username,
                'password' => $password,
                'rememberMe' => 0 
        ),
        'yt0' => 'Login' 
);

以及处理http重定向的最简单方法是启用CURLOPT_FOLLOWLOCATION,因此,curl会自动为您处理http重定向。同样在我的测试中,登录POST网址实际上是https://wifinext.internavigare.com/userGroups,而不是https://wifinext.internavigare.com/prepagataAnagrafica/creaUtente/

还有可能的是,他们试图通过验证Referrer标头来防止CSRF攻击,如果错误,最简单的方法是启用CURLOPT_AUTOREFERER并使其正常,则拒绝登录首先获取登录页面的请求。

另外,你 PROBABLY 在尝试登录之前需要一个cookie会话,最简单的方法就是设置CURLOPT_COOKIEFILE,现在curl会自动为你处理cookie。

并且您确定需要处理Cookie才能保持登录状态 - 此cookie与您的登录信息相关联。如果您在登录后未向下一个请求发送cookie,则服务器将不记得您的登录信息,并会再次向您发送登录页面。所以启用cookie。

和protip,无论何时调试curl代码,启用CURLOPT_VERBOSE,它都会打印许多有用的调试信息,比如http状态代码和收到的任何重定向。

另外,检查curl_setopt_array的返回类型!如果设置任何设置时出现问题,则返回bool(false),您应该使用curl_error()来查找错误。

另外,在你的代码中,你没有urlencode $ _GET [user] / $ _GET [password],所以如果它们包含urlencoded格式中具有特殊含义的任何字符,你将发送错误的凭据而不会登录(包括&=@,空格和许多其他字符),因此请对其进行编码。

这是一个登录示例,使用来自https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php的hhb_curl,一个围绕curl_的便利包装器,处理cookie,响应正文文件,详细的调试信息文件,引用标头,检查curl_setopt_array和amp的返回类型。如果无法设置选项,则抛出异常,etcetc:

<?php
declare(strict_types = 1);
require_once ('hhb_.inc.php');
$username = '???';
$password = '???';
$url = 'https://wifinext.internavigare.com/userGroups';
$hc = new hhb_curl ( $url, true );
$hc->exec (); // getting a referer, and a cookie session
$hc->setopt_array ( array (
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query ( array (
                'UserGroupsUser' => array (
                        'username' => $username,
                        'password' => $password,
                        'rememberMe' => 0 
                ),
                'yt0' => 'Login' 
        ) ) 
) );
$hc->exec ( $url );//actually logging in.
hhb_var_dump ( $hc->getStdErr (), $hc->getResponseBody () );

(使用真实的$ username / $密码,它可能会登录)

相关问题