User talk:Patrick Nagel/Login with snoopy post-1.15.3
Latest comment: 13 years ago by Patrick Nagel in topic Be independent of the cookie name
Be independent of the cookie name[edit]
Instead of:
- $snoopy->cookies["wiki_session"] = $response[login][sessionid];
I used:
- $snoopy->cookies = getCookieHeaders($snoopy->headers);
and the function getCookieHeaders is:
# This function parses Snoopy's header array and returns a nice array of cookies function getCookieHeaders($headers){ $cookies = array(); foreach($headers as $header) if(preg_match("/Set-Cookie: ([^=]*)=([^;]*)/", $header, $matches)) $cookies[$matches[1]] = $matches[2]; return $cookies; }
It works better because you don't have to know the name of the cookie in advanced. (I took the function from http://www.mail-archive.com/mediawiki-api@lists.wikimedia.org/msg00099.html)
- Thanks, I added a link to your proposal. --Patrick Nagel 02:38, 23 July 2010 (UTC)
Trim whitespace from Snoopy results before passing to unserialize[edit]
Instead of:
$response = unserialize($snoopy->results);
I used:
$response = unserialize(trim($snoopy->results));
I was getting a simple error and it had a simple solution. I didn't take the time to figure out how whitespace was getting into my API results. Here is where it came from: http://www.microtuts.com/php-unserialize-error-at-offset-simple-solution/.