【问题标题】:unable to authenticate with curl无法使用 curl 进行身份验证
【发布时间】:2014-07-09 15:59:57
【问题描述】:

我无法使用 curl 进行身份验证:https://www.eyez-on.com/EZMAIN/login.php 这是我的代码(基于问题Login to remote site with PHP cURL):

<?php


echo date('l jS \of F Y h:i:s A');
echo "<br>";

//username and password of account
$username = trim($values["email@domain.com"]);
$password = trim($values["mypassword"]);



//login form action url
$url="https://www.eyez-on.com/EZMAIN/login.php"; 
$postinfo = "email=".$username."&password=".$password;

$cookie_file_path = "/cookie/cookie.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);

//page with the content I want to grab
curl_setopt($ch, CURLOPT_URL, "https://www.eyez-on.com/EZMAIN/account.php");
//do stuff with the info with DomDocument() etc

$html = curl_exec($ch);

echo $html;

curl_close($ch);

?>

我想知道问题是否在于登录页面表单具有方法 GET。 下面是一段登录页面的源码:

    <form action="" method="get" id="login">
        <fieldset>
            <legend>Login</legend>
            <ul>
                <li>
                    <label for="email"><span class="required">User Account</span></label>
                    <input id="email" name="email" class="text required email" type="text" />
                    <label for="email" class="error">This must be a valid email address</label>
                </li>

                <li>
                    <label for="password"><span class="required">Password</span></label>
                    <input name="password" type="password" class="text required" id="password" minlength="4" maxlength="20" />
                </li>


            </ul>
        </fieldset>

        <fieldset class="submit">
                                <input type="submit" class="button" value="Login..." />
        </fieldset>

        <div class="clear"></div>
    </form>

我的最终目标是登录后,转到我的一个帐户页面,获取一些数据并使用这些数据创建一个 php 变量。 /cookie 目录是每个人都可以读/写的,并且 cookie.txt 文件被写入 - 所以这不是问题。 我已经为此工作了好几天............任何帮助将不胜感激。

【问题讨论】:

    标签: php curl forms-authentication


    【解决方案1】:

    快速提示:在 chrome 中使用您的网络标签,以便查看表单发布到的内容。 例如,此表单不会发布到自己,而是发布到 https://www.eyez-on.com/EZMAIN/processLogIn.php

    您必须进行 2 次 curl 调用。第一个到 processlogin 页面,发布您的凭据。它需要的变量是电子邮件和密码,然后将信息存储到 cookie jar 中,并将 cookie jar 包含在您的下一个 curl 调用中。

    【讨论】:

    • tkon99 - 谢谢一百万!更改发布网址就可以了!还要感谢您提供的提示:Chrome 的网络选项卡。我了解到发布网址不一定在页面源中。
    猜你喜欢
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2016-10-11
    • 1970-01-01
    相关资源
    最近更新 更多