I'm trying to get the HTML content of a certain page (that requires a login to view) with PHP. I have the login info of course, and I'm even ABLE to login to the page via curl, as the following code demonstrates (I took out the URLs and login info in this question, but it does actually work for me):
<html>
<head>
</head>
<body><?php
$login_url = 'https://www.MyTopSecretDomainPage/login.asp';
$remotePageUrl = 'http://www.MyTopSecretDomainPage/TheOtherPageThatIWantToGetTheContentsOf.asp?variablesThatAreImport=Things';
//These are the post data username and password
$post_data = 'action=login&cookieexists=true&redirect=&page=&partner=&email=someThing@gmail.com&password=TopSecretPassword&userid_to_cookie=1&saveID=yes';
//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);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
/*
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);
//here I'm trying to get the data another page
curl_setopt_array(
$ch, array(
CURLOPT_URL => $remotePageUrl ,
CURLOPT_RETURNTRANSFER => true
));
$output = curl_exec($ch);
//I first echo the $post_result 'cause I need to log in, but that causes me to redirect, not sure how else to do it so I can afterwards get the other data?
echo($postResult);
?>
<script>
</script>
</body>
</html>
most of this code was based on other answers I found here, but those other answers didn't address the problem I am having, which is that after it logs in to the other website successfully, it automatically redirects to that website, even though I turned CURLOPT_FOLLOWLOCATION off, as you can see. I suspect that somehow built into the other website it automatically redirects the user, but I need to still access content from a different page via PHP, and once I'm redirected -- no more php page!!!!!
I think if I can somehow load the login page (and login) on some hidden window or something and after it redirects on that other window, maybe I can then get the content of the other page I want, but I'm not sure if that's the correct solution, or if that would even work at all.
SO: I have successfully logged in to the other page, but how do I actually get the content of a different page on that site, since it redirects me automatically?