如何使用php curl从受保护的登录门户获取数据

时间:2019-04-29 11:25:37

标签: php curl remote-access

因此,在这种情况下,我正在尝试从受登录表单保护的用户登录仪表板中获取内容。换句话说,我们需要先输入登录信息,才能从必须获取内容的地方访问源页面。

登录门户URL https://xxxxxx.xxx/login

登录门户内的URL https://xxxxxx.xxx/user_profile/dashboard

Php curl:

<?php

$login_url = ' https://xxxxxx.xxx/login';


//These are the post data username and password
$post_data = 'username=xxxx&password=xxxx';

//Create a curl object
$ch = curl_init();

//Set the useragent
$agent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

//Set the URL
curl_setopt($ch, CURLOPT_URL, $login_url );

//This is a POST query
curl_setopt($ch, CURLOPT_POST, 1 );

//Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

//We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Follow Location redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

//Execute the action to login
$postResult = curl_exec($ch);



$url = "https://xxxxxx.xxx/user_profile/dashboard";
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
if(!empty($ch))
{
        $thispage = new DOMDocument;
        Libxml_use_internal_errors(true);
        $thispage->loadHTML($html);
        Libxml_clear_errors();
        $xpath = new DOMXPath($thispage) ;
        $status = $xpath->evaluate( 'string(//a[contains(text(),"YouTube")])');

        if(strpos($status, "YouTube") !== false) {
        $status = "YouTube";
          echo $status;
        }
        else
        {
           echo 'FALSE'; 
        }

}

else{
echo 'Page not found';
}

?>

内部cookie.txt here

所以在这里,我只是尝试获取定位标记的XPath(位于登录门户内部)并尝试显示其值(Youtube)。但不幸的是,该代码似乎无法正常工作。返回FALSE。

我想我缺少了一些重要的东西。几乎任何建议,代码修改和实现都将受到高度赞赏。

0 个答案:

没有答案