Hot Door CORE Forum
InternetGET/InternetPOST - Printable Version

+- Hot Door CORE Forum (http://hotdoorcore.com/forum)
+-- Forum: All forums (http://hotdoorcore.com/forum/forumdisplay.php?fid=1)
+--- Forum: General discussion (http://hotdoorcore.com/forum/forumdisplay.php?fid=3)
+--- Thread: InternetGET/InternetPOST (/showthread.php?tid=71)

Pages: 1 2


InternetGET/InternetPOST - Rick Johnson - 05-04-2016

Am I correct in assuming that InternetGET and InternetPOST could be used by a plugin to do things such as communicate with a database at the home web site to check if it's the current version?

If so, what do I need to ask my ISP to set up for me (my intuition tells me it just may be SQL-related), and what specifically do I need to study to learn to use this function? A bit of sample code for something like a current-version inquiry would be very helpful, if that's part of what this class does.

Thanks -- Rick


RE: InternetGET/InternetPOST - garrett - 05-04-2016

Either of those classes simply performs a HTTP query (GET or POST) using the operating system's networking libraries. For example, if you use your web browser to go to "http://checkip.dyndns.org", you will see that the DynDNS server is kind enough to tell you your public IP address (i.e. the output is "Current IP Address: xxx.xxx.xxx.xxx"). Using either class to query that same URL will allow you to retrieve the same exact string that your web browser showed, only from within a plugin of course.

So you need a web server and some content to serve up for either class to be of any use. How to do that is beyond the scope of the CORE forum, so you'll have to do a Google search for getting started with web development.


RE: InternetGET/InternetPOST - Rick Johnson - 05-05-2016

Thanks, Garrett. I knew it went beyond the scope of CORE, so I appreciate your steering me in the right direction.


RE: InternetGET/InternetPOST - Rick Johnson - 07-18-2016

Call me Nemo, but sometimes I'm often sure I can do things and later find that I can't. I'm trying to add an InternetGET to my preferences class, to alert the user if there's an update available.

My prefs class contains this, plus the callback functions:

Code:
    hdi::core::InternetGET iGET(hid::core::InternetGET("http://HodorUsesCORE.org/version.php?p=126",HDI_CORE_CALLBACK(sansa::thePrefs, this, verifyVersionOKCB), HDI_CORE_CALLBACK(sansa::thePrefs, this, verifyVersionFailCB)); // my prefs class resides within a namespace

I must have tried fifty variations to create the InternetGET without success, noting that empty InternetGET objects cannot be constructed. Xcode continually scolds me about this class having a private default constructor. Once it's properly created, I understand that I should call its run() function to do the PHP query, then the callback will call the content() function to get the query result. If I find it sometimes takes a while, I'd like to make it asynchronous. Actually, asynchronous sounds like a better approach, regardless.

What is the proper syntax for this? This isn't quite as straightforward as making a dialog's button go to a URL.

Many thanks -- Rick

Edit: I later tried creating it in my main Plugin class as a test. Still stumped. I'll work through any namespace issues of my own making later, but it would be much easier if I first knew the correct syntax to get started. Confused


RE: InternetGET/InternetPOST - garrett - 07-19-2016

If you want an asynchronous InternetGET/POST object, you must allocate it on the heap rather than the stack, as (due to the asynchronicity) the object's lifespan must last longer than the current function call. To accomplish this, the InternetGET/POST class provides a static `Async()` method, which returns a pointer to a properly allocated InternetGET/POST object.

Code:
hdi::core::InternetGET* iGET = hdi::core::InternetGET::Async(<your arguments>);
if(iGET)
{
    iGET->run();
}

And be sure to call the C++ `delete` operator on `iGET` before plugin shutdown. See the docs for the `Async()` method for other notes.


RE: InternetGET/InternetPOST - Rick Johnson - 09-23-2016

The GET function is working great! Now I need to send and receive POST data. I understand that the data sent is a DictionaryPref, but how will my PHP script understand it without CORE on the server? Will it arrive in JSON format? And for a PHP script to send information back to the plugin, is a JSON object best, or is it complicated to format it as a DictionaryPref again? Thanks a ton in advance.


RE: InternetGET/InternetPOST - garrett - 09-29-2016

A DictionaryPref is really just a key-value data structure, which is how HTTP POST data must be transmitted anyway. PHP will then perform its standard process to turn it into key-value data again, stored inside the $_POST variable.

You can send back whatever data you'd like in whatever format, as it's your responsibility to handle the content of the HTTP response. JSON isn't a bad idea, as you can use hdi::core:TonguerefData::parse() to process it.


RE: InternetGET/InternetPOST - Rick Johnson - 09-29-2016

Thanks, Garrett. I really appreciate your meaningful explanations.


RE: InternetGET/InternetPOST - Rick Johnson - 10-01-2016

Everything transfers as expected -- the data sent was interpreted correctly by the server and the JSON array returned was parsed into an std::auto_ptr<hdi::core:TonguerefData>, which seems to be a valid object since I can "stringify" it into the same string that went into it and isEmpty returns false.

The test data I parsed is:

Code:
"{\"name\":\"Chewbacca\",\"number\":\"123.456\",\"other\":\"ABC123\"}"

Is there a straightforward way to copy or move PrefData into a DictionaryPref where I can work with it by key/value pairs? Thanks! Undecided


RE: InternetGET/InternetPOST - garrett - 10-03-2016

(10-01-2016, 06:35 PM)Rick Johnson Wrote: Is there a straightforward way to copy or move PrefData into a DictionaryPref where I can work with it by key/value pairs? Thanks! Undecided

DictionaryPref inherits from PrefData, so you can simply cast the PrefData pointer to a DictionaryPref pointer. To be safe, don't forget to check the PrefData::dataType() method to be sure you're casting to the right subclass.