The problem is that the form key is associated with the session. Therefore, when you receive the form key, you need to use a cookie. Here's how I do it:
<?php class PoxnoraAPI { const URL = 'https://auth.station.sony.com/login?theme=poxnora&cid=1056360&service=https://poxnora.station.sony.com/cas/merge.do®Service=https://poxnora.station.sony.com /play/load.do'; protected $cookieFile; public function __construct() { // create a new cookie file $this->getCookieFile(); } // Creates new cookie file in system temp dir protected function getCookieFile() { $this->cookieFile = tempnam(sys_get_temp_dir(), 'CDL'); } // Gets form key from login page protected function getFormKey() { $ch = curl_init(); $this->setCurlOpts($ch); $result = curl_exec($ch); curl_close($ch); $key = $this->matchFormKey($result); if (!$key) { throw new Exception('Unable to get key from form'); } return $key; } protected function matchFormKey($result) { preg_match_all('<input type="hidden" name="lt" value="(.*)">', $result, $matches); return isset($matches[1][0]) ? $matches[1][0] : false; } // Uses username, password, and form key to login public function login($username, $password) { $key = $this->getFormKey(); $ch = curl_init(); $this->setCurlOpts($ch); $data = "lt=$key&_eventId=submit&username=$username&password=$password"; $this->setCurlPost($ch, $data); $result = curl_exec($ch); curl_close($ch); // check if there a form key. If there a form key then we're on // the login page again $key = $this->matchFormKey($result); return !$key; } // Add post data to curl protected function setCurlPost($ch, $postData) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); } // Add default curl params protected function setCurlOpts($ch) { curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile); curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile); curl_setopt($ch, CURLOPT_TIMEOUT, 40000); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, self::URL); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_REFERER, self::URL); } } $api = new PoxnoraAPI(); $isLoggedIn = $api->login('USERNAME_HERE', 'PASSWORD_HERE'); if ($isLoggedIn) { echo '<h1>Successfully logged in!<h1>'; } else { echo '<h1>Error logging in</h1>'; }
source share