在提交之前卷曲获取页面的内容

时间:2012-10-09 11:05:27

标签: php curl

我正在尝试访问我使用curl注册的网站的帐户,问题是登录页面有一个隐藏的身份验证输入字段,例如<input type='hidden' name='auth' value='adih2yerjh'/>,有没有办法确保在提交表单之前使用curl检查此字段的网站?这是我的功能模拟

function login(){
    $login_email = 'email';
    $login_pass = 'password';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.mysite.com/login.php');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
    $page = curl_exec($ch);
    echo $page;
}

修改:每次加载页面时,验证码都会更改

2 个答案:

答案 0 :(得分:1)

你必须提出两个卷曲请求 首先获取页面内容并尝试从HTML中提取该名称。您可能还需要处理会话或cookie 然后你可以提交第二个curl请求。

答案 1 :(得分:1)

你可以尝试

$url = "http://www.mysite.com/login.php";
$html = file_get_contents($url);
preg_match("~<input type='hidden' name='auth'  value='(.*?)' />~",$html,$authHidden);

/**
 // Run your post request with auth=$authHidden
 */