Visit url

From Kolmafia
Revision as of 23:29, 21 November 2019 by imported>Fredg1 (adding run_choice() in the seeAlso)
Jump to navigation Jump to search

Function Syntax

buffer visit_url()

buffer visit_url(string page )

buffer visit_url(string page ,boolean use_POST )

buffer visit_url(string page ,boolean use_POST ,boolean encoded )

  • page is the page to visit
  • use_POST is true for a POST request and false for a GET request
  • encoded is false if the parameter is not used. If set to true, then the url is assumed to be pre-encoded by the user.

Returns the HTML from the visited page (not just what is displayed when visiting the page, but all markup) by performing a POST request if use_POST is omitted or true, or a GET request if false. Note that for addresses inside of KoL, it is only necessary to supply the page name for page, and KoLmafia will populate the rest of the url. However, pages outside of KoL require the full url to be supplied for page.

The version with no parameters is only meaningful in a relay override script; it retrieves the server page that your script is overriding. All four versions behave slightly differently in a relay script; they retrieve a version of the page with any KoLmafia decorations added, rather than the raw page from the server.

In a POST request, if &pwd is part of the page string, it will automatically be replaced by &pwd=###### where ###### is the password hash for the current session. For a GET request the password hash will not be added so you'll want to use my_hash().

Code Samples

Visits your private character sheet.

visit_url( "charsheet.php" );

Visits Google.

visit_url( "http://www.google.com/");

Given a player ID, parse a player's profile for his name.

string getPlayerName( int playerID ) {
   string playerName;

   // use visit_url() to store the player profile's HTML in a string
   string playerProfile = visit_url("showplayer.php?who=" + playerID);
   if ( contains_text(playerProfile, "<td>Sorry, this player could not be found.</td>") ) {
      print("Player " + playerID + "does not exist.","red");
      return "";
   }
   // find player name in the string returned by visit_url()
   matcher match_name = create_matcher("<b>([^<]+)</b> \\(#" + playerID + "\\)<br>", playerProfile);
   if ( match_name.find() ) {
      playerName = match_name.group(1);
   }
   else {
      print("Problem occurred while parsing for player name","red");
      return "";
   }

   return playerName;
}

CLI Equivalent

The CLI commands "text", or "http:" will also visit specified urls.

See Also

make_url() | run_choice()

More Information

Note that this function returns an empty string if the page does not exist or if the request times out.

Special

Some people claim that it is better to append the result of this function to a buffer instead of saving it as a string when dealing with an especially large page to avoid time-out. This claim appears to be purely circumstantial and unsupported by the mafia's code.