Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:57:57+00:00 2026-06-03T22:57:57+00:00

I’m currently using the following (old) code to log into a site… public function

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:57:58+00:00Added an answer on June 3, 2026 at 10:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.