So here is the scenario, I am trying to get the content from the user login dashboard which is protected inside the login form. In other words, we need to enter login information before in order to access the source page from where we have to get content.

login portal URL: https://xxxxxx.xxx/login

URL inside the login portal: 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';
}

?>

inside cookie.txt here

So here I just trying to fetch XPath of the anchor tag (which is inside the login portal) and trying to display its value(Youtube). but unfortunately, the code seems doesn't work. It's return FALSE.

I guess I am missing something important. Kindly any suggestion, code modification & implementation is highly appreciated.

0 Answers