Visit url: Difference between revisions
imported>Heeheehee m Format-fixing. |
imported>Heeheehee m Minor corrections (spelling and whatnot). |
||
Line 48: | Line 48: | ||
code2={{CodeSample| | code2={{CodeSample| | ||
description=Visits | description=Visits Google.| | ||
code= | code= | ||
<syntaxhighlight> | <syntaxhighlight> | ||
Line 61: | Line 61: | ||
// use visit_url() to store the player profile's HTML in a string | // use visit_url() to store the player profile's HTML in a string | ||
string playerProfile = visit_url(" | string playerProfile = visit_url("showplayer.php?who=" + playerID); | ||
if ( contains_text(playerProfile, "<td>Sorry, this player could not be found.</td>") ) { | if ( contains_text(playerProfile, "<td>Sorry, this player could not be found.</td>") ) { | ||
print("Player " + playerID + "does not exist.","red"); | print("Player " + playerID + "does not exist.","red"); | ||
Line 72: | Line 72: | ||
} | } | ||
else { | else { | ||
print("Problem | print("Problem occurred while parsing for player name","red"); | ||
return ""; | return ""; | ||
} | } | ||
Line 80: | Line 80: | ||
</syntaxhighlight>}}| | </syntaxhighlight>}}| | ||
special=Note that this function returns an empty string if the page does not exist or if the request times | special=Note that this function returns an empty string if the page does not exist or if the request times out. | ||
}} | }} |
Revision as of 05:16, 9 April 2010
Function Syntax
buffer visit_url()
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. The version with no parameters is only meaningful in a relay override script; it retrieves the server page that your script is overriding. All three 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.
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;
}
Special
Note that this function returns an empty string if the page does not exist or if the request times out.