I'm trying to make an php app that that logs on Comcast on here
https://login.comcast.net/login
and download a file
http://xfinity.comcast.net/
remembering the cookie.
This is what i have so far:
<?php
$username="username";
$password="password";
$url="https://login.comcast.net/login";
$cookie="cookie.txt";
$postdata = "user=".$username."&passwd=".$password."&rm=2&deviceAuthn=false&forceAuthn=true&s=ccentral-cima&r=comcast.net&continue=http://xfinity.comcast.net/";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
file_put_contents("page.txt",$ch);
echo $cookie;
?>
What this script does is just login, and show me
https://customer.comcast.com/Public/Home.aspx
as i was logged in, and then when i press Email for example it simply treats me like i have never logged in, requesting me username and password. All i want is to fetch the number of emails by saving http://xfinity.comcast.net/ (with a cookie) to page.txt
Can this be possible?
Make sure to set CURLOPT_COOKIEFILE
. Jar is what is written into, file is being send to server.
Also, if you want to do both in one request, you have to query the server twice. Once to log in (with CURLOPT_COOKIEJAR
) and second time to obtain the actual content (with CURLOPT_COOKIEFILE
).
Moreover, there's no need to set CURLOPT_FOLLOWLOCATION
to false
as the login for usually does redirect. I would change it to true
, just to make sure.
First of all it would be handy if you put the curl lines into a function.
Now the steps should be rather easy.
EDIT
Make sure cookie.txt is in the same directory as your php file.
and make sure it is writable. ( chmod 777 )
to check :
if (is_writable('cookie.txt')) {
echo 'The cookie is writable';
} else {
echo 'The cookie is not writable';
## start by making it writable :
if (! chmod ( 'cookie.txt', 0777 ))
die ( 'chmod() failed on file cookie.txt' );
}
- login and save login cookie data with curl. ( make sure cookie.txt exists and is writable ).
- Do another curl WITH cookie enabled and retrieve page.
Something like this :
//@param string $url : url of page/file
//@param bool $binary : binary file.
//@param string $post : post data in format : formvar1=VAR1&formvar2=VAR2
//@param string $cookie : cookie file.
function curl($url ,$binary=false,$post=false,$cookie =false ){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
if($cookie){
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
}
if($binary)
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
if($post){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
return curl_exec ($ch);
}
Then login :
$username="username";
$password="password";
$url="https://login.comcast.net/login";
$cookie="cookie.txt";
$postdata = "user=".$username."&passwd=".$password."&rm=2&deviceAuthn=false&forceAuthn=true&s=ccentral-cima&r=comcast.net&continue=http://xfinity.comcast.net/";
// function
$ch = curl($url,false,$postdata,$cookie);
Logged in ? check cookie.txt for content.
Step 2 , get second page.
$url ='http://xfinity.comcast.net/'
$cookie="cookie.txt";
$ch =curl($url,false,false,$cookie);
echo $ch;
Something like that... Let me know if it works.