Hi All: I have blown 8 hrs over 2 days trying to figure out what is wrong with my PHP program to try to do a query.
I am trying to build a simple PHP program to get the contents of recent changes on a wiki I admin.
Here is the code, with a couple of things "anonymized" (surrounded by << >>):
========= begin code ===========
<?php //login and get the correct security values to pass in on subsequent queries $wikiURL = "<<mydomain>>/mediawiki"; $logincmd = $wikiURL."/api.php?action=login&lgname=Sgg&lgpassword=<<mypwd>>&format=json";
$handle = fopen($logincmd,"rb"); $contents = stream_get_contents($handle); fclose($handle);
$loginVals = json_decode($contents, true);
if($loginVals["login"]["result"] != "Success"){ echo"Failed to login, result returned ", $loginVals["login"]["result"]; exit(-1); }
//set up the security parm values and create query $userid = $loginVals["login"]["lguserid"]; $username = $loginVals["login"]["lgusername"]; $token = $loginVals["login"]["lgtoken"];
$query = $wikiURL."/api.php?action=query&list=recentchanges&format=xml&lgtoken=$token&lgusername=$username&lguserid=$userid"; echo" Query",$query," ";
//execute the get and print the results $handle = fopen($query,"rb"); $contents = stream_get_contents($handle); fclose($handle);
print_r(" Query results"); print_r($contents); print_r(" end query results "); ?>
===============end code ================
The login works fine, returns reasonable looking tokens. The Query results, however are empty.
When I copy the query that was produced (note the echo statement), and paste that into a browser (that I have previously used to login to the wiki, I get some query results back. When I paste that query into a browser I rarely use (ie haven't logged into the wiki), I get empty result set.
So, am I misusing the login parms somehow on the query url? Are the login parms not working? Should I use cookie manipulation instead?
Please help
sgg
sggraham@nc.rr.com schreef:
So, am I misusing the login parms somehow on the query url? Are the login parms not working? Should I use cookie manipulation instead?
Yes, you should use cookies. You'll get a cookie sent to you on action=login, which you should send back at every next request. In PHP, Snoopy [1] makes handling cookies easier. Login parameters the way you tried to use them currently don't work. Maybe they will in the future, who knows.
Catrope
I've drawn up a simple script that logs in and fetches information using the Snoopy class. Snoopy is available at http://snoopy.sourceforge.net/ Simply download the tarball and extract the Snoopy.class.php file in the same directory as your script. Most important is the getCookieHeaders() function, which sets up the cookies correctly.
Hope this helps,
Catrope
== PHP code starts here ==
<?php require_once('Snoopy.class.php');
$wikiPath = "http://en.wikipedia.org/w"; $apiPath = "$wikiPath/api.php";
function getCookieHeaders($headers) { // This function parses Snoopy's header array and returns a nice array of cookies $cookies = array(); foreach($headers as $header) if(preg_match("/Set-Cookie: ([^=]*)=([^;]*)/", $header, $matches)) $cookies[$matches[1]] = $matches[2]; return $cookies; }
$snoopy = new Snoopy;
// Example 1: Build a table of namespaces $request_vars = array( 'action' => 'query', 'meta' => 'siteinfo', 'siprop' => 'namespaces', 'sishowalldb' => '', 'format' => 'php' // NEVER, EVER, forget this one );
if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}");
$array = unserialize($snoopy->results); $namespaceArr = $array['query']['namespaces']; echo "<table border>\n<tr><th>ID</th><th>Name</th></tr>\n"; foreach($namespaceArr as $ns) echo "<tr><td>{$ns['id']}</td><td>{$ns['*']}</td></tr>\n"; echo "</table>\n";
// Example 2: Get userinfo before and after login $request_vars = array('action' => 'query', 'meta' => 'userinfo', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}"); $array = unserialize($snoopy->results); $userinfo = $array['userinfo']; if(isset($userinfo['anon'])) echo "Anonymous user {$userinfo['name']}<br>"; else echo "Registered user {$userinfo['name']}<br>";
echo "Logging in...<br>";
$request_vars = array('action' => 'login', 'lgname' => 'yourusername', 'lgpassword' => 'yourpassword', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}");
// Fetch the cookies from the login request and tell Snoopy to use them from now on // This only has to be done once $snoopy->cookies = getCookieHeaders($snoopy->headers);
$request_vars = array('action' => 'query', 'meta' => 'userinfo', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}"); $array = unserialize($snoopy->results); $userinfo = $array['userinfo']; if(isset($userinfo['anon'])) echo "Anonymous user {$userinfo['name']}<br>"; else echo "Registered user {$userinfo['name']}<br>";
== PHP code ends here ==
Roan Kattouw schreef:
function getCookieHeaders($headers) { // This function parses Snoopy's header array and returns a nice array of cookies $cookies = array(); foreach($headers as $header) if(preg_match("/Set-Cookie: ([^=]*)=([^;]*)/", $header, $matches)) $cookies[$matches[1]] = $matches[2]; return $cookies; }
As it turns out, you can throw out that function and use $snoopy->setCookies(); instead. Snoopy's better than I thought. Improved version of the script follows below.
Catrope
== PHP code starts here == <?php require_once('Snoopy.class.php');
$wikiPath = "http://en.wikipedia.org/w"; $apiPath = "$wikiPath/api.php";
$snoopy = new Snoopy;
// Example 1: Build a table of namespaces
$request_vars = array( 'action' => 'query', 'meta' => 'siteinfo', 'siprop' => 'namespaces', 'sishowalldb' => '', 'format' => 'php' // NEVER, EVER, forget this one );
if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}");
$array = unserialize($snoopy->results); $namespaceArr = $array['query']['namespaces']; echo "<table border>\n<tr><th>ID</th><th>Name</th></tr>\n"; foreach($namespaceArr as $ns) echo "<tr><td>{$ns['id']}</td><td>{$ns['*']}</td></tr>\n"; echo "</table>\n";
// Example 2: Get userinfo before and after login
$request_vars = array('action' => 'query', 'meta' => 'userinfo', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}"); $array = unserialize($snoopy->results); $userinfo = $array['userinfo']; if(isset($userinfo['anon'])) echo "Anonymous user {$userinfo['name']}<br>"; else echo "Registered user {$userinfo['name']}<br>";
echo "Logging in...<br>";
$request_vars = array('action' => 'login', 'lgname' => 'yourusername', 'lgpassword' => 'yourpassword', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}");
// We're only really interested in the cookies $snoopy->setcookies();
$request_vars = array('action' => 'query', 'meta' => 'userinfo', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}"); $array = unserialize($snoopy->results); $userinfo = $array['userinfo']; if(isset($userinfo['anon'])) echo "Anonymous user {$userinfo['name']}<br>"; else echo "Registered user {$userinfo['name']}<br>";
Roan,
Wonderful. I've been trying to send cookies to the API from PHP for ages. Thank you.
Yours cordially, Jesse Martin (Pathoschild)
Sorry to drag this thread back up but I'd appreciate some insight into the login API and whether my approach has any chance of working or if I'm barking up the wrong tree.
I'm trying to write a modification for phpBB2 that would allow any successful login to phpBB2 to automatically login to the mediawiki install on the same domain.
I was successful in using the code below to get the server to login using the user and password supplied in the phpBB2 login BUT I quickly realised that the cookie information was being associated with the server IP and not back to the users browser.
So, my question is can I use the API in media wiki this way or am I completely out to lunch here?
Any help would be appreciated.
Martin
Roan Kattouw wrote:
Roan Kattouw schreef:
function getCookieHeaders($headers) { // This function parses Snoopy's header array and returns a nice array of cookies $cookies = array(); foreach($headers as $header) if(preg_match("/Set-Cookie: ([^=]*)=([^;]*)/", $header, $matches)) $cookies[$matches[1]] = $matches[2]; return $cookies; }
As it turns out, you can throw out that function and use $snoopy->setCookies(); instead. Snoopy's better than I thought. Improved version of the script follows below.
Catrope
== PHP code starts here == <?php require_once('Snoopy.class.php');
$wikiPath = "http://en.wikipedia.org/w"; $apiPath = "$wikiPath/api.php";
$snoopy = new Snoopy;
// Example 1: Build a table of namespaces
$request_vars = array( 'action' => 'query', 'meta' => 'siteinfo', 'siprop' => 'namespaces', 'sishowalldb' => '', 'format' => 'php' // NEVER, EVER, forget this one );
if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}");
$array = unserialize($snoopy->results); $namespaceArr = $array['query']['namespaces']; echo "<table border>\n<tr><th>ID</th><th>Name</th></tr>\n"; foreach($namespaceArr as $ns) echo "<tr><td>{$ns['id']}</td><td>{$ns['*']}</td></tr>\n"; echo "</table>\n";
// Example 2: Get userinfo before and after login
$request_vars = array('action' => 'query', 'meta' => 'userinfo', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}"); $array = unserialize($snoopy->results); $userinfo = $array['userinfo']; if(isset($userinfo['anon'])) echo "Anonymous user {$userinfo['name']}<br>"; else echo "Registered user {$userinfo['name']}<br>";
echo "Logging in...<br>";
$request_vars = array('action' => 'login', 'lgname' => 'yourusername', 'lgpassword' => 'yourpassword', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}");
// We're only really interested in the cookies $snoopy->setcookies();
$request_vars = array('action' => 'query', 'meta' => 'userinfo', 'format' => 'php'); if(!$snoopy->submit($apiPath, $request_vars)) die("Snoopy error: {$snoopy->error}"); $array = unserialize($snoopy->results); $userinfo = $array['userinfo']; if(isset($userinfo['anon'])) echo "Anonymous user {$userinfo['name']}<br>"; else echo "Registered user {$userinfo['name']}<br>";
Mediawiki-api mailing list Mediawiki-api@lists.wikimedia.org http://lists.wikimedia.org/mailman/listinfo/mediawiki-api
WebSnail wrote:
Sorry to drag this thread back up but I'd appreciate some insight into the login API and whether my approach has any chance of working or if I'm barking up the wrong tree.
I'm trying to write a modification for phpBB2 that would allow any successful login to phpBB2 to automatically login to the mediawiki install on the same domain.
I was successful in using the code below to get the server to login using the user and password supplied in the phpBB2 login BUT I quickly realised that the cookie information was being associated with the server IP and not back to the users browser.
So, my question is can I use the API in media wiki this way or am I completely out to lunch here?
Any help would be appreciated.
Martin
You're better using an auth plugin. I'm sure there's already one for PhpBB login.
WebSnail wrote:
Sorry to drag this thread back up but I'd appreciate some insight
into the
login API and whether my approach has any chance of working or if I'm barking up the wrong tree.
I'm trying to write a modification for phpBB2 that would allow any successful login to phpBB2 to automatically login to the mediawiki
install
on the same domain.
I was successful in using the code below to get the server to login
using
the user and password supplied in the phpBB2 login BUT I quickly
realised
that the cookie information was being associated with the server IP
and not
back to the users browser.
So, my question is can I use the API in media wiki this way or am I completely out to lunch here?
Any help would be appreciated.
Martin
You're better using an auth plugin. I'm sure there's already one for PhpBB login.
There is something out there already but the problem is that it's in the wrong direction, ie you have to login using mediawiki instead of the other way round.
Ordinarily this wouldn't be a problem but the phpBB2 login also handles the Gallery2 and a number of other integrated tools too.
Martin wrote:
You're better using an auth plugin. I'm sure there's already one for PhpBB login.
There is something out there already but the problem is that it's in the wrong direction, ie you have to login using mediawiki instead of the other way round.
Are you sure on it?
An auth plugin fires on login, so it can authenticate the user against the phpbb password db, and create the mw user if not existing for mediawiki.
In fact, http://www.mediawiki.org/wiki/Extension:PHPBB/Users_Integration seems to be what you want.
[quote] This method only works for PHPBB 2.*. Users will use mediawiki to login, disable the phpbb login page[/quote]
As mentioned earlier this is not the functionality I need.. I'm after phpBB2 as the primary login because it already provides the integrated login for a number of other apps.
Thanks though.
Martin wrote:
You're better using an auth plugin. I'm sure there's already one for PhpBB login.
There is something out there already but the problem is that it's in
the
wrong direction, ie you have to login using mediawiki instead of the
other
way round.
Are you sure on it?
An auth plugin fires on login, so it can authenticate the user against the phpbb password db, and create the mw user if not existing for mediawiki.
In fact, http://www.mediawiki.org/wiki/Extension:PHPBB/Users_Integration seems to be what you want.
Mediawiki-api mailing list Mediawiki-api@lists.wikimedia.org http://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Another, more reusable solution would be to add OpenID support for both (it probably exists already) and have one of them to be a provider for another one plus, maybe configure them to use 'immediate' response for the same-domain logins (might require additional coding).
Sergey
On Nov 10, 2007 5:24 PM, Martin info@websnail.net wrote:
WebSnail wrote:
Sorry to drag this thread back up but I'd appreciate some insight
into the
login API and whether my approach has any chance of working or if I'm barking up the wrong tree.
I'm trying to write a modification for phpBB2 that would allow any successful login to phpBB2 to automatically login to the mediawiki
install
on the same domain.
I was successful in using the code below to get the server to login
using
the user and password supplied in the phpBB2 login BUT I quickly
realised
that the cookie information was being associated with the server IP
and not
back to the users browser.
So, my question is can I use the API in media wiki this way or am I completely out to lunch here?
Any help would be appreciated.
Martin
You're better using an auth plugin. I'm sure there's already one for PhpBB login.
There is something out there already but the problem is that it's in the wrong direction, ie you have to login using mediawiki instead of the other way round.
Ordinarily this wouldn't be a problem but the phpBB2 login also handles the Gallery2 and a number of other integrated tools too.
Mediawiki-api mailing list Mediawiki-api@lists.wikimedia.org http://lists.wikimedia.org/mailman/listinfo/mediawiki-api
WebSnail schreef:
So, my question is can I use the API in media wiki this way or am I completely out to lunch here?
Not completely. You can fetch the cookie using your PHP script and spit out those cookies to the client. That way your client gets the session ID, and the server will (hopefully) not care the IP has changed, since AOL and proxies do this all the time. (Don't know for sure though, you'd best just try.)
Roan Kattouw (Catrope)
Not sure about this... I did try this using some code of my own but it didn't seem to pass on all the information required or at least it didn't pass on the cookie necessary to "remember" the login.
I'll post up my code when I've had a chance to tidy it up.
WebSnail schreef:
So, my question is can I use the API in media wiki this way or am I completely out to lunch here?
Not completely. You can fetch the cookie using your PHP script and spit out those cookies to the client. That way your client gets the session ID, and the server will (hopefully) not care the IP has changed, since AOL and proxies do this all the time. (Don't know for sure though, you'd best just try.)
Roan Kattouw (Catrope)
Mediawiki-api mailing list Mediawiki-api@lists.wikimedia.org http://lists.wikimedia.org/mailman/listinfo/mediawiki-api
mediawiki-api@lists.wikimedia.org