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:

URL inside the login portal:

Php curl:


$login_url = '';

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

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

//Set the useragent
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 = "";
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
        $thispage = new DOMDocument;
        $xpath = new DOMXPath($thispage) ;
        $status = $xpath->evaluate( 'string(//a[contains(text(),"YouTube")])');

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


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