登录网站cURL [英] Logging in to a website cURL

查看:261
本文介绍了登录网站cURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用cURL。我需要登录到一个网站。
我有设置cookie文件和检索问题,以便我可以访问该页面不只是一次,但几次。
我在网上找到了代码,用于登录到一个网站和Scrap一个网页的一些详细信息,导致获取该网页需要很多时间。
所以我只是想知道如果它是好!代码belove(它只是为代码中的Scraping其未准备就绪)

I am using cURL for the first time. I need to login to a site. I have problem with setting cookie file and to retrive, so that i can acces that page not just one time, but several times. I found the code on the web, for logging in to a site and Scrap a page for some detailed info, cause to get that page it takes to much time. so i just want to know if it is OK! the code belove(it is just for login in the code for Scraping its not ready)

<?php

curl_login('http://mywantedsite.com/login.php','user=******&pass=******','','off');

echo curl_grab_page('http://mywantedsite.com/somepage.php','','off');

function curl_login($url,$data,$proxy,$proxystatus){
    $fp = fopen("cookie.txt", "w");
    fclose($fp);
    $login = curl_init();
    curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($login, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($login, CURLOPT_TIMEOUT, 40);
    curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
    if ($proxystatus == 'on') {
        curl_setopt($login, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($login, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($login, CURLOPT_PROXY, $proxy);
    }
    curl_setopt($login, CURLOPT_URL, $url);
    curl_setopt($login, CURLOPT_HEADER, TRUE);
    curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($login, CURLOPT_POST, TRUE);
    curl_setopt($login, CURLOPT_POSTFIELDS, $data);
    ob_start();      // prevent any output
    return curl_exec ($login); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($login);
    unset($login);    
}                  

function curl_grab_page($site,$proxy,$proxystatus){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    if ($proxystatus == 'on') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, $site);
    ob_start();      // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($ch);
}  
?>


推荐答案

您需要检查您是否发布所有发布字段..
一些网站使用安全令牌或会话ID来防止bots在他们的网站上登录。
无论如何,你需要安装Live HTTP headers firefox扩展。打开它,并尝试手动登录,然后看到最新实际上被张贴当您按下登录按钮。
。然后添加到第一功能并再次测试

you need to check if you are posting all the "posting fields".. some sites use security tokens or sessions ids to prevent bots from logging on their sites. anyway, you need to install Live HTTP headers firefox extension. open it and try to login manually, then see whats being posted actually when you press login button. after you get the values. add then to the first function and test again.

这篇关于登录网站cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆