Advertisement
Guest User

sewikibot

a guest
Sep 23rd, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. <?php
  2.  
  3. function sewiki_httpRequest($url, $post="") {
  4. global $settings;
  5.  
  6. $ch = curl_init();
  7. //Change the user agent below suitably
  8. 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');
  9. curl_setopt($ch, CURLOPT_URL, ($url));
  10. curl_setopt( $ch, CURLOPT_ENCODING, "UTF-8" );
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  12. curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookies.tmp");
  13. curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookies.tmp");
  14. if (!empty($post)) curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
  15. //UNCOMMENT TO DEBUG TO output.tmp
  16. //curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
  17. //$fp = fopen("output.tmp", "w");
  18. //curl_setopt($ch, CURLOPT_STDERR, $fp); // Display communication with server
  19.  
  20. $xml = curl_exec($ch);
  21.  
  22. if (!$xml) {
  23. throw new Exception("Error getting data from server ($url): " . curl_error($ch));
  24. }
  25.  
  26. curl_close($ch);
  27.  
  28. return $xml;
  29. }
  30.  
  31.  
  32. function sewiki_login () {
  33. global $settings;
  34. $wikiroot = "http://www.spaceengineerswiki.com/api.php?";
  35. $user = "GeneralBot";
  36. $pass = "Obfuscated";
  37. $params = "action=login&lgname=$user&lgpassword=$pass&format=xml";
  38. $url = $wikiroot;
  39.  
  40. $data = sewiki_httpRequest($url, $params);
  41.  
  42. if (empty($data))
  43. {
  44. throw new Exception("No data received from server. Check that API is enabled.");
  45. }
  46.  
  47. $xml = simplexml_load_string($data);
  48.  
  49. $expr = "/api/login[@result='NeedToken']";
  50. $result = $xml->xpath($expr);
  51.  
  52. if(!count($result)) {
  53. echo "<br>";
  54. echo "Token Get";
  55.  
  56. throw new Exception("Login failed");
  57. }
  58. else
  59. {
  60. //store token received
  61. $token = $result[0]->attributes()->token;
  62. echo "Token Got! Token: ".$token;
  63. }
  64.  
  65.  
  66.  
  67.  
  68. //send login again with new token
  69. $newdata = sewiki_httpRequest($url, $params.'&token='.$token);
  70. $newxml = simplexml_load_string($newdata);
  71. //Check for successful login
  72. $newexpr = "/api/login[@result='Success']";
  73. $newresult = $newxml->xpath($newexpr);
  74.  
  75. if(!count($newresult)) {
  76. echo "<br>";
  77. echo "Post Token Get";
  78. echo "<br>";
  79. var_dump($newxml);
  80. throw new Exception("Login failed");
  81. }
  82. //return $newresult;
  83. return "Success!";
  84. }
  85.  
  86. function sewiki_httpGetRequest ($url)
  87. {
  88. $ch = curl_init();
  89. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  90. curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.tmp");
  91. curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.tmp");
  92. curl_setopt($ch, CURLOPT_URL, ($url));
  93. $response = curl_exec($ch);
  94. return $response;
  95. }
  96. function sewiki_getpage ($page)
  97. {
  98. $login = 'Success!';
  99. //$login = sewiki_login();
  100. if($login != 'Success!')
  101. {
  102. return "Get Page Failed";
  103.  
  104. }
  105. else
  106. {
  107. $url = 'http://spaceengineerswiki.com/api.php?';
  108. $postdata = 'action=query&prop=revisions&titles=' . $page . '&rvprop=content&format=xml';
  109. $url .=$postdata;
  110. echo $url;
  111. $result = sewiki_httpGetRequest($url);
  112.  
  113. //$json = json_decode($result);
  114. $xml = simplexml_load_string($result);
  115. return $xml;
  116. //return $result;
  117. //return $json;
  118.  
  119. }
  120. }
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement