Author Topic: write a for loop in order to get a response from an api  (Read 2151 times)

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: [Select]
foo\t 0\t bar\t 1\t bat\t 2\t\n
)

a sloppy pseudocode here can represent what im thinking:

Code: [Select]
//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);
}

i have a feeling json parsing would be a better alternative to my above example of how code was formatted
« Last Edit: November 24, 2017, 12:38:48 AM by theviacom »


so are you asking how to get/post to an API using jettison or regular tcp?

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.

so are you asking how to get/post to an API using jettison or regular tcp?

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

- snip -

so then connectToUrl basically passes the result on to myAPIRequest which will be accessed by doing myAPIRequest::handleText, and %text stores the text itself?

Assuming the API you're trying to use returns a content type of application/json (99% of them), you're going to have a hard time trying to use TCPClient because it thinks everything that doesn't match text/* is binary. So you're going to need to have it save to a file and read it in onDone.

so then connectToUrl basically passes the result on to myAPIRequest which will be accessed by doing myAPIRequest::handleText, and %text stores the text itself?
Yeah pretty much, and based on the example in the OP I got the feeling this isn't really an API but just a php page that outputs text when given certain GET variables. If this is the case just using what I posted will be faster than saving a file and reading it. It would be helpful if you posted the php code here or at least the webpage you're trying to access.

Also should note that handleText gets called for every line, and might contain header information or other stuff you might not want (not sure how TCPClient handles that) but be aware. You might have to do something like "response: your data here" and only grab lines that begin with "response:".
« Last Edit: November 24, 2017, 05:53:40 PM by Crøwn »

I messaged Greek2me and he fixed it in version 15, so it should be really, really simple now: http://www.greek2me.us/code/Support_TCPClient.cs

Code: [Select]
function myRequest() {
    connectToURL("http://jsonplaceholder.typicode.com/posts/1", "GET", "", "myAPIRequest");
}

function myAPIRequest::handleText(%this, %text) {
    %this.text = %this.text @ %text;
}

function myAPIRequest::onDone(%this, %error) {
    if (!%error) {
        echo("JSON: " @ %this.text);
    }
}



The BLG add-on uses jettison very often to send objects.

The BLG add-on uses jettison very often to send objects.
Oh, nice! I'll consider it tried and true, then.