Home » Php » php – curl connect timeout not working

php – curl connect timeout not working

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m using CURL to connect to multiple xml feeds and process them when the page loads. Unfortunately, every once in awhile a page won’t be responsive and my script will stall as well. Here’s an example of the code that I’m working with. I set the timeout to 1 but that doesn’t appear to be working. I then set the timeout to 0.0001 just to test things today and it still pulled in xml feeds. Do you guys have any ideas on how to force curl to timeout when a script is taking forever.

foreach($urls as $k => $v) {
   $curl[$k] = curl_init();   
   curl_setopt($curl[$k], CURLOPT_URL, $v);
   curl_setopt($curl[$k], CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl[$k], CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($curl[$k],CURLOPT_CONNECTTIMEOUT, 1); 
How to&Answers:

See the difference between CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT

Answer:

There are two different timeouts with curl — see curl_setopt manual’s page :

CURLOPT_CONNECTTIMEOUT
The number
of seconds to wait while trying to
connect. Use 0 to wait indefinitely.

And :

CURLOPT_TIMEOUT
The maximum number
of seconds to allow cURL functions to
execute.

They both have a “millisecond” version : CURLOPT_CONNECTTIMEOUT_MS and CURLOPT_TIMEOUT_MS, respectively.

In your case, you might want to configure the second one too : what seems to take time is not the connection, but the construction of the feed on the server side.

Answer:

i am using Curl library for download a file from Apache Server.
downloadFileWithCurlLibrary fonction has two inputs.
FileUrl is the file will download form server.
Example : www.abc.com/myfile.doc.
locFile is directory and file name you want to save.
Example:/home/projectx/myfile.doc.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <curl/curl.h>

int downloadFileWithCurlLibrary(char *FileUrl,char *locFile)
{
    char dlFile[1024];
    CURL *curl_handle;
    FILE *pagefile;


    memset(dlFile,0x0,sizeof(dlFile));
    /* Create URL */

    sprintf(dlFile,"%s",FileUrl);

    curl_global_init(CURL_GLOBAL_ALL);

    /* init the curl session */
    curl_handle = curl_easy_init();

    /* Set timeout 3 min = 3*60 sec here. */
    curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 1800 );

    /* set URL to get here */
    if (curl_easy_setopt(curl_handle, CURLOPT_URL, dlFile) != CURLE_OK)
        return -1;

    /* Switch on full protocol/debug output while testing */
    //  if (curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L) != CURLE_OK)
    //    return -1;

    /* disable progress meter, set to 0L to enable and disable debug output */
    if (curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L) != CURLE_OK)
        return -1;

    /* send all data to this function  */
    if (curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data) != CURLE_OK)
        return -1;

    /* open the file */
    pagefile = fopen(locFile, "wb");
    if (pagefile)
    {

        /* write the page body to this file handle. CURLOPT_FILE is also known as
           CURLOPT_WRITEDATA*/
        if (curl_easy_setopt(curl_handle, CURLOPT_FILE, pagefile) != CURLE_OK)
            return -1;

        /* get it! */
        if (curl_easy_perform(curl_handle) != 0)
            return -1;

        /* close the header file */
        fclose(pagefile);
    }
    else
    {
        return -1;
    }

    /* cleanup curl stuff */
    curl_easy_cleanup(curl_handle);

    return 0;
}