PHP curl multi on passworded site

1.2k views Asked by At

I'm currently using the following (old) code to log into a site...

public function login() {
    $url1 = 'https://...';  /* Initial page load to collect initial session cookie data */
    $url2 = 'https://...';  /* The page to POST login data to */
    $url3 = 'https://...';  /* The page redirected to to test for success */
    $un = 'user';
    $pw = 'pass';

    $post_data = array(
        'authmethod' => 'on',
        'username'   => $un,
        'password'   => $pw,
        'hrpwd'      => $pw
    );

    $curlOpt1 = array(
        CURLOPT_URL            => $url1,
        CURLOPT_COOKIEJAR      => self::COOKIEFILE,
        CURLOPT_COOKIEFILE     => self::COOKIEFILE,
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_HEADER         => FALSE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE
    );

    $curlOpt2 = array(
        CURLOPT_URL            => $url2,
        CURLOPT_COOKIEJAR      => self::COOKIEFILE,
        CURLOPT_COOKIEFILE     => self::COOKIEFILE,
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_POST           => TRUE,
        CURLOPT_POSTFIELDS     => http_build_query($post_data)
    );

    $this->ch = curl_init();
    if ( !$this->ch ) {
        throw new Exception('Unable to init curl. ' . curl_error($curl));
    }

    /* Load the login page once to get the session ID cookies */
    curl_setopt_array( $this->ch, $curlOpt1 );
    if ( !curl_exec( $this->ch ) ) {            
        throw new Exception( 'Unable to retrieve initial auth cookie.' );
    }

    /* POST the login data to the login page */
    curl_setopt_array($this->ch, $curlOpt2);
    if ( !curl_exec( $this->ch ) ) {
        throw new Exception( 'Unable to post login data.' );
    }

    /* Verify the login by checking the redirected url. */
    $header  = curl_getinfo( $this->ch );
    $retUrl = $header['url'];

    if ( $retUrl == $url3 ) {
        /* Reload the login page to get the auth cookies */
        curl_setopt_array( $this->ch, $curlOpt1 );
        if ( curl_exec( $this->ch ) ) {
            return true;
        } else {
            throw new Exception( 'Unable to retrieve final auth cookie.' );
        }
    } else {
        throw new Exception( 'Login validation failure.' );
    }

    return false;
}

I then use...

public function getHtml($url) {
    $html = FALSE;

    try {
        curl_setopt($this->ch, CURLOPT_URL, $url);
        $page = curl_exec($this->ch);
    } catch (Exception $e) {
        ...
    }

    /* Remove all tabs and newlines from the HTML */
    $rmv = array("\n","\t");
    $html = str_replace($rmv, '', $page);

    return $html;
}

...for each page request. My question is, how can i convert this to use curl_multi_exec to make several hundred look-ups quicker? I can't find any examples of curl_multi WITH login. Do I simply replace all curl_execs with curl_multi_exec? Also, if you see any other glaringly obvious mistakes, comments are surely welcome.

To be clear, I would like to log in with a single user/pass then reuse those credentials for multiple page requests.

1

There are 1 answers

0
Isius On BEST ANSWER

It's been awhile but I wanted to post my final solution. I found an awesome multi-curl library, rolling-curl, that helped. Basically after collecting the login cookie (shown in my original question) I feed it, and other options, back into the rolling curl instance for each multi request, then execute the batch. Works like a charm.

public function getMultiPage(array $urls, $url_prepend=NULL, $callback) {
    $rc = new RollingCurl(array('Att_Screen_Scraper', $callback));
    $rc->window_size = 15;   /* number of threads to run */
    $rc->options = array(
        CURLOPT_COOKIEJAR      => self::COOKIEFILE,
        CURLOPT_COOKIEFILE     => self::COOKIEFILE,
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_HEADER         => FALSE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE
    );

    foreach ($urls as $i=>$url) {
        $request = new RollingCurlRequest($url_prepend . $url);
        echo $url_prepend . $url . "<br>\n";
        $rc->add($request);
    }

    if(!$rc->execute()) {
        throw new Exception('RollingCurl execute failed');
    }

    return TRUE;
}

Note that this solution requires a callback to handle the return of each request. RollingCurl's documentation describes this well so I won't reiterate it here.