Blockland Forums > Modification Help
write a for loop in order to get a response from an api
theviacom:
i am writing an api with php, and i just simply need to know how to do this
i also am unsure how to format a GET request, and here's an example of the kind i want to make:
api.php?a=04&v=00
(action 04 just simply means retrieve entry counts as an integer, and value 00 means send the integer back upon connection and query)
also
api.php?a=03&v=00&e=XX
(action 03 means send back XX entries in ascending order with a value of 00 which just means send it back with each piece of information ending with a \t)
(for example, if entry foo was #0 in the database, and its bar was 1 and bat was 2, then it would say
--- Code: ---foo\t 0\t bar\t 1\t bat\t 2\t\n
--- End code ---
)
a sloppy pseudocode here can represent what im thinking:
--- Code: ---//cf means "cannon fodder"
//pingback will be a GET request to /api.php?a=04 which asks the database for entry count,
//making a network request that many times
//the NetworkRequest function will send a GET request to /api.php?a=03 in order to return a list of data, and the actual entry to return is specified for each for looping of $cf
for($cf = 0; $cf < $pingback; $cf++) {
function NetworkRequest()
{
TCPobject.connect("api.php?a=03&v=00&e=" $cf);
//i dont know what code i could use to read the response and then process it with regex to separate out data spaced by \t characters
//i could try to format my data as json instead
}
NetworkRequest($cf);
}
--- End code ---
i have a feeling json parsing would be a better alternative to my above example of how code was formatted
theviacom:
https://github.com/qoh/jettison
Kyuande:
so are you asking how to get/post to an API using jettison or regular tcp?
Crøwn:
I would suggest using TCPClient. It allows for easy GET/POST requests with TCP objects and takes care of the messy stuff for you. You can read the documentation too for help but here's an example of what you're trying to do:
function NetworkRequest()
{
connectToUrl("http://yourSite.com/api.php?a=03&v=00&e=" @ $somethingElse, "GET", "", "myAPIRequest");
}
(You can't define a function inside a for loop like you're doing in your example)
So now TCPClient is going to handle that request for you, the 4th parameter in the function is the class name which allows you to do this:
function myAPIRequest::handleText(%this, %text)
{
echo("This is some text that was on the page:" SPC %text);
}
If you look at the documentation page I linked you can read more about the class functions.
Depending on the size and type of data you're storing in the database you may want to use JSON objects and in that case I would definitely go with jettison.
theviacom:
--- Quote from: Kyuande on November 24, 2017, 02:22:16 AM ---so are you asking how to get/post to an API using jettison or regular tcp?
--- End quote ---
send a get request with tcpobject (or greeks tcpclient) which will then get json data as a response and then use jettison to interpret it
--- Quote from: Crøwn on November 24, 2017, 12:45:22 PM ---- snip -
--- End quote ---
so then connectToUrl basically passes the result on to myAPIRequest which will be accessed by doing myAPIRequest::handleText, and %text stores the text itself?