简体   繁体   English

使用curl PHP处理动态URL

[英]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. 我有登录凭据,可以通过curl登录,但是登录过程有第二阶段,我必须回答一个安全问题。

Problem is every-time I am entering user-id, password it redirect to different url. 问题是每次我输入用户ID时,密码都会重定向到其他URL。

As example: Login url : https://url.com After login: https://url?execution=e4s1&action=caDevicePrint (Security q/a page) 作为例子:登录网址: https://url.com后登录: HTTPS:// URL执行= e4s1和行动= caDevicePrint (安全Q /页)

Here 'execution=e4s1' is changing every-time after login like e1s1, e1s2, e3s2. 在这里,“执行= e4s1”在登录后每次都会更改,例如e1s1,e1s2,e3s2。

I want to set CURLOPT_URL dynamically. 我想动态设置CURLOPT_URL。 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在响应中包含标头。

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. 获得标头后,您可以解析它们以查看网站将您重定向到的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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