UPDATE: Turns out my code works. Browser was caching previous failed response. Thanks for the pointers.
I'm building a prototype and one thing I'd like to do is perform a service if the user is a valid member of NYTimes.com by providing their credentials.
Using curl, I'm trying to perform a login to the site, and then check for success or failure.
My code, below, doesn't return errors but drops me back at the login page:
<?php
class Login {
function Verify() {
print $this->getContent();
}
function getContent() {
$url = 'http://www.nytimes.com/auth/login';
// URI can be any NYT web page to be redirected to upon successful login
// SAVEOPTION and Submit2 are Optional but in original web form so included here
$fields = array(
'is_continue'=> 'true',
'USERID' => urlencode('ENTER_YOUR_USERNAME'),
'PASSWORD' => urlencode('ENTER_A_PASSWORD'),
'URI' => urlencode('http://www.nytimes.com/robots.txt'),
'OQ' => '',
'OP' => '',
'SAVEOPTION' => 'YES',
'Submit2' => 'Log In'
);
$fields_string = '';
if(!$curld = curl_init($url)) {
echo "Could not connect to the specified resource";
exit;
}
$ch = curl_init();
$useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, "curl_login_cookie.txt");
curl_setopt($ch ,CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$result = ob_get_contents();
ob_end_clean();
return $result;
}
}
$login = new Login;
$result = $login->Verify();
?>
Any pointers, or suggestions welcome.