Using Curl/Post to execute a HTTP request I have a loop in a php file to HTTP_Request Server Via CURL->POST.

But I don't know if I'm missing any security considerations. Also, is there a better way to perform this http request?

&lt;?php
$j = 0;
while ($j &lt;= 1) {
    $url    = 'http://127.0.0.1/index.php';
    $fields = array(
        'input1' =&gt; 'variable1',
        'input2'    =&gt; 'variable2',
    );
    $postvars         = http_build_query($fields);
    $COOKIE_FILE_PATH = "/tmp/cookiescron.txt";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
    curl_setopt($ch, CURLOPT_TIMEOUT, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
    // execute post
    curl_exec($ch);
    // close connection
    curl_close($ch);
    $j++;
    sleep(25);
}
?&gt;


This executes 2 requests in around 25~30 seconds.
Well, I didn't take the time to test my snippets, but here are two suggestions...

#1 (preferred) - Bundle all of your input data into a single array, then pass it to your receiving url with a single curl call.  It is best practice to minimize total calls (avoid iterated calls) so long as the operation works for your project, of course.  This avoids sleepy time too.

$ch = curl_init();
$fields = [
    ['input1' =&gt; 'variable1', 'input2' =&gt; 'variable2'],
    ['input1' =&gt; 'variable3', 'input2' =&gt; 'variable4']
];
$COOKIE_FILE_PATH = "/tmp/cookiescron.txt";  // this doesn't appear to be used
$options = [
    CURLOPT_URL               =&gt; 'http://127.0.0.1/index.php',
    CURLOPT_POST              =&gt; true,  // https://www.php.net/manual/en/function.curl-setopt.php says boolean is expected
    CURLOPT_POSTFIELDS        =&gt; http_build_query($fields),
    CURLOPT_TIMEOUT           =&gt; 2,
    CURLOPT_RETURNTRANSFER    =&gt; false,
    CURLOPT_FORBID_REUSE      =&gt; true,
    CURLOPT_CONNECTTIMEOUT    =&gt; 2,
    CURLOPT_DNS_CACHE_TIMEOUT =&gt; 10,
    CURLOPT_FRESH_CONNECT     =&gt; true,
    CURLOPT_HTTPHEADER        =&gt; ['Connection: close']
];
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);




#2 - If your requirements obligate the use of iterated curl calls, I'd recommend only updating the CURLOPT_POSTFIELDS value within the loop.  (Again, not tested)

$ch = curl_init();
$fields = [
    ['input1' =&gt; 'variable1', 'input2' =&gt; 'variable2'],
    ['input1' =&gt; 'variable3', 'input2' =&gt; 'variable4']
];
$COOKIE_FILE_PATH = "/tmp/cookiescron.txt";  // this doesn't appear to be used
$options = [
    CURLOPT_URL               =&gt; 'http://127.0.0.1/index.php',
    CURLOPT_POST              =&gt; true,  // https://www.php.net/manual/en/function.curl-setopt.php says boolean is expected
    CURLOPT_TIMEOUT           =&gt; 2,
    CURLOPT_RETURNTRANSFER    =&gt; false,
    CURLOPT_CONNECTTIMEOUT    =&gt; 2,
    CURLOPT_DNS_CACHE_TIMEOUT =&gt; 10,
];
foreach ($fields as $data) {
    $options[CURLOPT_POSTFIELDS =&gt; http_build_query($data)]; // overwrites previous data
    curl_setopt_array($ch, $options);
    curl_exec($ch);
    sleep(25);
}
curl_close($ch);


The takeaway here is to always try to leave as many processes as possible OUTSIDE of the loop to reduce calls/workload.

Relevant documentation:


https://www.php.net/manual/en/function.curl-setopt.php
https://www.php.net/manual/en/function.curl-setopt-array.php