簡體   English   中英

使用curl登錄並移至另一頁

[英]Login with curl and move to another page

我正在嘗試使用CURL訪問網站中的一個頁面,但是它需要登錄我嘗試使用代碼登錄並且成功

<?php

    $user_agent       = "Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20140319 Firefox/24.0 Iceweasel/24.4.0";
    $curl_crack = curl_init();

    CURL_SETOPT($curl_crack,CURLOPT_URL,"https://www.vininspect.com/en/account/login");
    CURL_SETOPT($curl_crack,CURLOPT_USERAGENT,$user_agent);
    CURL_SETOPT($curl_crack,CURLOPT_PROXY,"183.78.169.60:37899");
    CURL_SETOPT($curl_crack,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
    CURL_SETOPT($curl_crack,CURLOPT_POST,True);
    CURL_SETOPT($curl_crack,CURLOPT_POSTFIELDS,"LoginForm[email]=naceriwalid%40hotmail.com&LoginForm[password]=passwordhere&toploginform[rememberme]=0&yt1=&toploginform[rememberme]=0");
    CURL_SETOPT($curl_crack,CURLOPT_RETURNTRANSFER,True);
    CURL_SETOPT($curl_crack,CURLOPT_FOLLOWLOCATION,True);
    CURL_SETOPT($curl_crack,CURLOPT_COOKIEFILE,"cookie.txt"); //Put the full path of the cookie file if you want it to write on it
    CURL_SETOPT($curl_crack,CURLOPT_COOKIEJAR,"cookie.txt"); //Put the full path of the cookie file if you want it to write on it
    CURL_SETOPT($curl_crack,CURLOPT_CONNECTTIMEOUT,30);
    CURL_SETOPT($curl_crack,CURLOPT_TIMEOUT,30);  

    $exec = curl_exec($curl_crack);
    if(preg_match("/^you are logged|logout|successfully logged$/i",$exec))
    {
        echo "yoooha";
    }

?>

現在我面臨的唯一問題就是說我不想重定向到登錄頁面,我想重定向到這個頁面http://example.com/buy ,我怎么能在相同的代碼?

如果您想在登錄后前往/buy ,只需使用相同的卷曲手柄並發出該頁面的另一個請求。 cURL將在句柄持續時間內保留cookie(以及后續請求,因為您將它們保存到文件中並使用cookie jar讀回。

例如:

$user_agent       = "Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20140319 Firefox/24.0 Iceweasel/24.4.0";
$curl_crack = curl_init();

CURL_SETOPT($curl_crack,CURLOPT_URL,"https://www.vininspect.com/en/account/login");
CURL_SETOPT($curl_crack,CURLOPT_USERAGENT,$user_agent);
CURL_SETOPT($curl_crack,CURLOPT_PROXY,"183.78.169.60:37899");
CURL_SETOPT($curl_crack,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
CURL_SETOPT($curl_crack,CURLOPT_POST,True);
CURL_SETOPT($curl_crack,CURLOPT_POSTFIELDS,"LoginForm[email]=naceriwalid%40hotmail.com&LoginForm[password]=passwordhere&toploginform[rememberme]=0&yt1=&toploginform[rememberme]=0");
CURL_SETOPT($curl_crack,CURLOPT_RETURNTRANSFER,True);
CURL_SETOPT($curl_crack,CURLOPT_FOLLOWLOCATION,True);
CURL_SETOPT($curl_crack,CURLOPT_COOKIEFILE,"cookie.txt"); //Put the full path of the cookie file if you want it to write on it
CURL_SETOPT($curl_crack,CURLOPT_COOKIEJAR,"cookie.txt"); //Put the full path of the cookie file if you want it to write on it
CURL_SETOPT($curl_crack,CURLOPT_CONNECTTIMEOUT,30);
CURL_SETOPT($curl_crack,CURLOPT_TIMEOUT,30);  

$exec = curl_exec($curl_crack);
if(preg_match("/^you are logged|logout|successfully logged$/i",$exec))
{
    $post = array('search' => 'keyword', 'abc' => 'xyz');

    curl_setopt($curl_crack, CURLOPT_POST, 1); // change back to GET
    curl_setopt($curl_crack, CURLOPT_POSTFIELDS, http_build_query($post)); // set post data
    curl_setopt($curl_crack, CURLOPT_URL, 'http://example.com/buy'); // set url for next request

    $exec = curl_exec($curl_crack); // make request to buy on the same handle with the current login session
}

以下是使用PHP和cURL發出多個請求的其他一些示例:

如何使用Curl和SSL以及cookie登錄 (鏈接到其他多個示例)

登錄后使用cURL從網站上抓取數據?

使用PHP和cURL登錄Pinterest不起作用

使用PHP和Curl登錄Google,Cookie已關閉?

PHP卷曲 - 餅干問題

您只需在登錄競爭后更改URL,然后運行curl_exec如下所示:

<?php

//login code goes here

if(preg_match("/^you are logged|logout|successfully logged$/i",$exec))
{
    echo "Logged in! now lets go to other page while we are logged in, shall we?";
    //The new URL that you want to go to while logged in goes in bottom line :
    CURL_SETOPT($curl_crack, CURLOPT_URL, "https://new_url_to_go.com/something");
    $exec = curl_exec($curl_crack);
    // now $exec contains the the content of new page with login
} 


curl_close($curl_crack);//dont forgert to close curl session at last
?>

首先定義這些函數以獲取包含url頭和內容的關聯數組(請參閱http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl ):

/**
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 */
function get_web_page( $url, $params, $is_post = true )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "Mozilla/4.0 (compatible;)", // i'm mozilla
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    if($is_post) { //use POST

        $options[CURLOPT_POST] = 1;
        $options[CURLOPT_POSTFIELDS] = http_build_query($params);

    } else { //use GET

        $url = $url.'?'.http_build_query($params);

    }

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

登錄成功后嘗試加載' http://www.example.com/buy '。

// after curl login setup
$exec = curl_exec($curl_crack);
if(preg_match("/^you are logged|logout|successfully logged$/i",$exec))
{
    // close login CURL resource, and free up system resources
    curl_close($curl_crack);

    $params = array('product_id'=>'xxxx', qty=>10);
    $url = 'http://www.example.com/buy';

    //use above function to get the url content via POST params
    $result = get_web_page($url, $params, true);

    if($result['http_code'] == 200) {
        //echo the content
        echo $result['content'];
        die();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM