简体   繁体   中英

Handling dynamic url using curl php

I want to grab some data from a website. I have login credential and I am able to login via curl, but there is a 2nd stage of login process where I have to answer one security question.

Problem is every-time I am entering user-id, password it redirect to different url.

As example: Login url : https://url.com After login: https://url?execution=e4s1&action=caDevicePrint (Security q/a page)

Here 'execution=e4s1' is changing every-time after login like e1s1, e1s2, e3s2.

I want to set CURLOPT_URL dynamically. below is my code:

    $this->curl = curl_init();      
    curl_setopt($this->curl, CURLOPT_URL, "https://url?execution=e4s1&action=caDevicePrint"); 
    curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($this->curl, CURLOPT_USERAGENT,$this->userAgent);       
    curl_setopt($this->curl, CURLOPT_COOKIEJAR,$this->cookieFile);
    curl_setopt($this->curl, CURLOPT_COOKIEFILE,$this->cookieFile);

    $results = curl_exec($this->curl);
    echo $results;
    if (curl_errno($this->curl)) { 
        echo "Failed loading <br>";
        var_dump(curl_error($this->curl));
        die();
    }

    curl_close($this->curl); 

You would need to grab the headers. This will tell curl to include the headers in its response.

curl_setopt( $this->curl, CURLOPT_HEADER, TRUE );

Once you have the headers, you can parse through them to see what the URL is that the site is redirecting you to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM