Several questions:
First, when you say "with 2 parameters", do you mean parameters
as part of the URL, like "
http://some.url?p1=value&p2=another_value",
or are sent as a JSON/XML payload via POST like
{ "param1" : "value1", "param2" : "value2" }
?
Second, do those parameters stay the same for every call, or do
they change from call to call?
Does your program need to actually process any of the data, or does it
go straight into a log file for processing later?
Is your program meant to run as a daemon or background process, or
is it fired off from an interactive session?
If the parameters are constant *and* part of the URL *and* all you need
to do is append data to a file for later processing, then this should be
sufficient:
#include <stdio.h>
#include <unistd.h>
int main( void )
{
/**
* Command to execute curl on a URL and append the results to a
* log file. Assumes curl is available on your system.
*/
const char *command = "curl
http://some.url?p1=val1&p2=val2 >>
log_file";
unsigned int time_to_sleep = 60;
int i=1;
/**
* Since time_to_sleep never changes, this is equivalent - do you
* really intend for your loop to run forever?
*/
for( ;; )
{
system( command );
sleep( time_to_sleep );
/**
* If your loop is really meant to run "forever",
* then i will eventually overflow, and the
* behavior of signed integer overflow
* is undefined. This also assumes you're
* running from an interactive session, since
* it assumes the presence of stdout.
*/
printf( "step: %d\n", i++ );
}
return EXIT_SUCCESS;
}
If the parameters can be different for each invocation of the program
(i.e., specified on the command line), then you'll need to do some
string processing when you build the command:
#include <stdio.h>
#include <unistd.h>
#define COMMAND_SIZE 64 // Needs to be large enough to store the
// full command string; 64 is large
// enough to store the example above with
// some padding, but you'll need to adjust
// for the real URL and parameter names and
// values.
int main( int argc, char **argv )
{
if ( argc < 3 )
{
fprintf( stderr, "USAGE: %s <param1> <param2>\n", argv[0] );
return EXIT_FAILURE;
}
char command[COMMAND_SIZE+1];
const char *fmt = "curl
http://some.url?p1=%s&p2=%s >> log_file" ;
sprintf( command, fmt, argv[1], argv[2] );
/** rest of the program is the same as the example above */
}
If you need to do a POST instead of a GET (send the parameters as part
of a JSON/XML payload rather than in the URL itself) or you need to
process the data as it comes back, then this gets a *lot* more
complicated in a hurry, to the point where I echo Keith and others
in saying you really, really, *really* don't want to do it in C. You'll
need to use a third-party library since C doesn't natively support
network communications, much less Web programming. I've used libcurl
(
https://curl.se/libcurl/) which is one of the less painful options,
but if you really don't want to do a lot of C programming, then you
will want to explore other options.