First off, I'm on the intermediate PHP plan, so cURL functions work.
Objective: Take a username and password and log in to another site. This other site sets and works off of cookies, so keep track of the cookies set. Then, go to another page on the site, passing along the cookies so it still registers as logged in and grab information from that page.
Now, the first cURL thing works fine. It logs in, gathers the cookies, and writes the cookies to the specified file.
The second part, however, doesn't work. More specifically, the cookies don't seem to be passed along. The page that it retrieves is not the page that you get when logged on normally in a browser, but rather the page you get if you either weren't logged on or were and just deleted your browser's cookies. So, I think it's safe to assume that it's not seeing the cookies. I really don't know what needs to be changed to get it to work.
Also, i'm getting a regex error
Warning: ereg() [function.ereg]: REG_BADRPT in pathtothefile on line 71. I can't figure out what's going wrong. It's supposed to pick out an alphanumeric key (containing characters in the a-f or 0-9 ranges) that is between 16 and 18 characters long bordered by bold tags. It uses lookahead and lookbehind so that the tags themselves are not matched and I only extract the key. I don't know why this is throwing warnings.
Thanks in advance for any help
Objective: Take a username and password and log in to another site. This other site sets and works off of cookies, so keep track of the cookies set. Then, go to another page on the site, passing along the cookies so it still registers as logged in and grab information from that page.
Code:
//location of cookie storage file (chmod 777)
$cookie_loc = "/home/tauzero/tmp/cookies/hypLogin";
$login_url = "http://thesite?login=$login&pwd=$pass"; //Main Login url
$APIgen_url = "http://thesite/servlet/Preferences?genhapikey"; //Key generating url
//create the cURL handle for main login and set some options
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_loc);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //Wait a max of 10 seconds for connection to fubar
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //Wait a max of 10 seconds for response
//execute the query and close the connection
ob_start(); // start buffer to prevent output
curl_exec($ch);
//ob_end_clean();
curl_close($ch);
unset($ch);
//create the cURL handle for key generation and set some options
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_loc);
curl_setopt($ch, CURLOPT_URL, $APIgen_url);
//execute the query and close the connection
ob_start();
$pref_page = curl_exec($ch);
ob_end_clean();
curl_close($ch);
unset($ch);
echo "<PRE>".htmlentities($pref_page); //for debug purposes
//extract the key from the page
ereg("(?<=<b>)[a-f0-9]{16,18}(?=</b>)",$pref_page,$key);//extract the API key
Now, the first cURL thing works fine. It logs in, gathers the cookies, and writes the cookies to the specified file.
The second part, however, doesn't work. More specifically, the cookies don't seem to be passed along. The page that it retrieves is not the page that you get when logged on normally in a browser, but rather the page you get if you either weren't logged on or were and just deleted your browser's cookies. So, I think it's safe to assume that it's not seeing the cookies. I really don't know what needs to be changed to get it to work.
Also, i'm getting a regex error
Warning: ereg() [function.ereg]: REG_BADRPT in pathtothefile on line 71. I can't figure out what's going wrong. It's supposed to pick out an alphanumeric key (containing characters in the a-f or 0-9 ranges) that is between 16 and 18 characters long bordered by bold tags. It uses lookahead and lookbehind so that the tags themselves are not matched and I only extract the key. I don't know why this is throwing warnings.
Thanks in advance for any help