How to create session with php curl in mediawiki api -
i working on mediawiki api , want create user login api . referred link. not able create session .
here code
first step
i have created session id
session_start(); $data = "action=login&lgname=wiki&lgpassword=gjnlt&lgtoken=5ae555656110dd20a2b0504e4d7e35e0"; // login $result=call($data); // session id function call($data=null) { $ch = curl_init('http://192.168.1.32/eb_new/mediawiki/api.php?format=json'); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $data); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_httpheader, array( 'accept: application/json', 'content-length: ' . strlen($data)) ); $result = curl_exec($ch); return $result=json_decode($result); }
the response got .
{"login":{"result":"success","lguserid":5,"lgusername":"wiki","lgtoken":"d85fd0201276632858ce4ad3ac351de4","cookieprefix":"eb_new_wiki_","sessionid":"3b8cdab43d4cda6b4548a3cc27604e20"}}
second step
now have session id in $result->login->sessionid
.to set session made call set session
setsession($result->login->sessionid); // set session function setsession($sessionid) { $field_array= array( 'accept' => 'http_accept', 'accept-charset' => 'http_accept_charset', 'accept-encoding' => 'http_accept_encoding', 'accept-language' => 'http_accept_language', 'connection' => 'http_connection', 'host' => 'http_host', 'referer' => 'http_referer', 'user-agent' => 'http_user_agent', 'set-cookie'=>'enwikisession='.$sessionid.' ;domain=http://192.168.1.32/eb_new/mediawiki' ); $curl_request_headers=array(); foreach ($field_array $key => $value) { if(isset($_server["$value"])) { $server_value=$_server["$value"]; $curl_request_headers[]="$key: $server_value"; } }; $curl_request_headers[]="expect: "; session_write_close(); $data = "action=login&lgname=wiki&lgpassword=gjnlt&lgtoken=5ae555656110dd20a2b0504e4d7e35e0"; // login $ch = curl_init('http://192.168.1.32/eb_new/mediawiki/api.php?format=json'); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $data); curl_setopt($curl_handle, curlopt_httpheader, $curl_request_headers); //curl_setopt($curl_handle, curlopt_cookie, 'enwikisession='.$sessionid.' ;domain=http://192.168.1.32/eb_new/mediawiki'); curl_setopt($ch, curlopt_returntransfer, true); echo $result = curl_exec($ch); }
but user not logged in . wrong . appreciated . thanks
structure of login request
send login request using post (get requests cause error).
api.php?action=login&lgname=bob&lgpassword=secret [try in [apisandbox] ][1] result
{ "login": { "result": "needtoken", "token": "b5780b6e2f27e20b450921d9461010b4", "cookieprefix": "enwiki", "sessionid": "17ab96bd8ffbe8ca58a78657a918558e" } }
the body of post can empty. request return session cookie in http header
(set-cookie: enwikisession=17ab96bd8ffbe8ca58a78657a918558e; path=/; domain=.wikipedia.org; httponly)
that have return second request if framework not automatically. sessionid parameter added in mediawiki 1.17 , later.
https://en.wikipedia.org/wiki/special:apisandbox#action=login&lgname=bob&lgpassword=secret
Comments
Post a Comment