11

I'm trying to access one page in a website with CURL, however it needs to be logged in i tried the code to login and it was successful

<?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";
    }

?>

Now the only problem I'm facing let's say that i don't want to be redirected to the logged in page, i want to be redirected to this page http://example.com/buy, how i can do that in the same code?

SniperCoder
  • 823
  • 2
  • 11
  • 23

3 Answers3

14

If you want to go to /buy after you log in, just use the same curl handle and issue another request for that page. cURL will retain the cookies for the duration of the handle (and on subsequent requests since you are saving them to a file and reading them back with the cookie jar.

For example:

$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
}

Here are some other examples of using PHP & cURL to make multiple requests:

How to login in with Curl and SSL and cookies (links to multiple other examples)

Grabbing data from a website with cURL after logging in?

Pinterest login with PHP and cURL not working

Login to Google with PHP and Curl, Cookie turned off?

PHP Curl - Cookies problem

Community
  • 1
  • 1
drew010
  • 68,777
  • 11
  • 134
  • 162
  • You are right, i missed that one, i mean that idea. Thank you and congratulation the bounty point :) Also if you can modify your example and make a second request (Post) from example/buy that will be better and it will help me a lot and thank you very much – SniperCoder Nov 16 '15 at 05:06
  • Hi @SniperCoder I modified the request to `/buy` to be post. It's pretty much the same logic you used for the login. Some of the other example I posted show multiple post requests as well. When re-using the same curl handle for multiple requests just keep using `curl_exec` as you would perhaps only modifying the request type GET/POST, the URL, and the post data if any. Otherwise its all the same. You only need to set the majority of options when first setting up the curl handle. Beyond that the only options that usually need to be changed is the url and request type. – drew010 Nov 16 '15 at 18:41
  • Thank you very much for your help, Thank you :) – SniperCoder Nov 16 '15 at 21:58
  • i had also this small problem if you can help me with it please. I did not find any answer in the internet about it http://stackoverflow.com/questions/33745688/access-denied-for-curl-post – SniperCoder Nov 16 '15 at 23:43
1

You just need to change the URL after login is compete and then run curl_exec Like this :

<?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
?>
Zeus
  • 1,235
  • 12
  • 20
0

First define these function to get an associative array containing the url header and content (see 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;
}

try this to load the 'http://www.example.com/buy' after login is successful.

// 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();
    }
}
David
  • 522
  • 3
  • 7