Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function sewiki_httpRequest($url, $post="") {
- global $settings;
- $ch = curl_init();
- //Change the user agent below suitably
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
- curl_setopt($ch, CURLOPT_URL, ($url));
- curl_setopt( $ch, CURLOPT_ENCODING, "UTF-8" );
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookies.tmp");
- curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookies.tmp");
- if (!empty($post)) curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
- //UNCOMMENT TO DEBUG TO output.tmp
- //curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
- //$fp = fopen("output.tmp", "w");
- //curl_setopt($ch, CURLOPT_STDERR, $fp); // Display communication with server
- $xml = curl_exec($ch);
- if (!$xml) {
- throw new Exception("Error getting data from server ($url): " . curl_error($ch));
- }
- curl_close($ch);
- return $xml;
- }
- function sewiki_login () {
- global $settings;
- $wikiroot = "http://www.spaceengineerswiki.com/api.php?";
- $user = "GeneralBot";
- $pass = "Obfuscated";
- $params = "action=login&lgname=$user&lgpassword=$pass&format=xml";
- $url = $wikiroot;
- $data = sewiki_httpRequest($url, $params);
- if (empty($data))
- {
- throw new Exception("No data received from server. Check that API is enabled.");
- }
- $xml = simplexml_load_string($data);
- $expr = "/api/login[@result='NeedToken']";
- $result = $xml->xpath($expr);
- if(!count($result)) {
- echo "<br>";
- echo "Token Get";
- throw new Exception("Login failed");
- }
- else
- {
- //store token received
- $token = $result[0]->attributes()->token;
- echo "Token Got! Token: ".$token;
- }
- //send login again with new token
- $newdata = sewiki_httpRequest($url, $params.'&token='.$token);
- $newxml = simplexml_load_string($newdata);
- //Check for successful login
- $newexpr = "/api/login[@result='Success']";
- $newresult = $newxml->xpath($newexpr);
- if(!count($newresult)) {
- echo "<br>";
- echo "Post Token Get";
- echo "<br>";
- var_dump($newxml);
- throw new Exception("Login failed");
- }
- //return $newresult;
- return "Success!";
- }
- function sewiki_httpGetRequest ($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.tmp");
- curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.tmp");
- curl_setopt($ch, CURLOPT_URL, ($url));
- $response = curl_exec($ch);
- return $response;
- }
- function sewiki_getpage ($page)
- {
- $login = 'Success!';
- //$login = sewiki_login();
- if($login != 'Success!')
- {
- return "Get Page Failed";
- }
- else
- {
- $url = 'http://spaceengineerswiki.com/api.php?';
- $postdata = 'action=query&prop=revisions&titles=' . $page . '&rvprop=content&format=xml';
- $url .=$postdata;
- echo $url;
- $result = sewiki_httpGetRequest($url);
- //$json = json_decode($result);
- $xml = simplexml_load_string($result);
- return $xml;
- //return $result;
- //return $json;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement