Visit url: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
mNo edit summary
imported>Heeheehee
m Answered StD's RFI, hopefully.
Line 72: Line 72:
</syntaxhighlight>}}|
</syntaxhighlight>}}|


special=Note that this function returns void if the page does not exist or if the request times-out.
special=Note that this function returns void if the page does not exist or if the request times-out. The 0-argument version is primarily for use in relay override scripts to grab the HTML of the current page.
}}
}}
{{RFI|Is the special note correct? It appears to sometimes return an empty string & sometimes void; I'm unclear on how the two interact|There's a 0-argument version; what does it do? Reload the last page?}}
{{RFI|Is the special note correct? It appears to sometimes return an empty string & sometimes void; I'm unclear on how the two interact}}

Revision as of 23:36, 23 March 2010

needs(better_code_samples);

Function Syntax

buffer visit_url(string page )

buffer visit_url(string page ,boolean use_POST )

  • page is the page to visit
  • use_POST is true for a POST request and false for a GET request

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.

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 occured while parsing for player name","red");
		return "";
	}

	return playerName;
}

Special

Note that this function returns void if the page does not exist or if the request times-out. The 0-argument version is primarily for use in relay override scripts to grab the HTML of the current page.


Attention KoLmafia Experts!

We need your help; some details of this function's operation are unknown or unclear.

The following specific question has been raised:

  • Is the special note correct? It appears to sometimes return an empty string & sometimes void; I'm unclear on how the two interact