Advertisement
jvvg

Wiki Functions

May 18th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. function get_page_contents($title) {
  2.     $data = '';
  3.     while ($data == '') {
  4.         $data = curl_get('http://wiki.scratch.mit.edu/w/api.php?action=query&titles=' . rawurlencode($title) . '&prop=revisions&rvprop=content&format=xml&salt=' . md5(time()));
  5.     }
  6.     $page_xml = @new SimpleXMLElement($data);
  7.     $contents = (string) ($page_xml->query->pages->page->revisions->rev);
  8.     return $contents;
  9. }
  10.  
  11. function curl_post($url, $postfields) {
  12.     $ch = curl_init ();
  13.     curl_setopt ( $ch, CURLOPT_URL, $url);
  14.     curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
  15.     curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  16.     curl_setopt ( $ch, CURLOPT_POST, 1 );
  17.     curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postfields);
  18.     curl_setopt ( $ch, CURLOPT_ENCODING, "" );
  19.     curl_setopt ( $ch, CURLOPT_COOKIEFILE, getcwd () . '/cookies.txt' );
  20.     curl_setopt ( $ch, CURLOPT_COOKIEJAR, getcwd () . '/cookies.txt' );
  21.     curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
  22.     $out = curl_exec ( $ch );
  23.     curl_close($ch);
  24.     return $out;
  25. }
  26.  
  27. function curl_get($url) {
  28.     $ch = curl_init ();
  29.     curl_setopt ( $ch, CURLOPT_URL, $url);
  30.     curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
  31.     curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  32.     curl_setopt ( $ch, CURLOPT_ENCODING, "" );
  33.     curl_setopt ( $ch, CURLOPT_COOKIEFILE, getcwd () . '/cookies.txt' );
  34.     curl_setopt ( $ch, CURLOPT_COOKIEJAR, getcwd () . '/cookies.txt' );
  35.     curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
  36.     $out = curl_exec ( $ch );
  37.     curl_close($ch);
  38.     return $out;
  39. }
  40.  
  41. function submit_edit($title, $contents, $summary, $minor = false) {
  42.     $tokenxml = new SimpleXMLElement(curl_post('http://wiki.scratch.mit.edu/w/api.php?action=query&prop=info|revisions&intoken=edit&titles=' . rawurlencode($title) . '&format=xml', '')); //get token
  43.     $edittoken = (string)$tokenxml->query->pages->page->attributes()->edittoken;
  44.     $return = curl_post('http://wiki.scratch.mit.edu/w/api.php', 'action=edit&title=' . rawurlencode($title) . '&summary=' . $summary . '&text=' . rawurlencode($contents) . '&format=xml&bot=true' . ($minor = true ? '&minor=true' : '') . '&token=' . rawurlencode($edittoken)); //submit the edit
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement