卷曲连接超时不起作用

发布于 2024-08-04 19:48:25 字数 479 浏览 2 评论 0原文

我使用 CURL 连接到多个 xml feed 并在页面加载时处理它们。不幸的是,每隔一段时间,页面就不会响应,我的脚本也会停止。这是我正在使用的代码的示例。我将超时设置为 1,但这似乎不起作用。然后,我将超时设置为 0.0001,只是为了今天进行测试,但它仍然提取 xml 提要。你们对于如何在脚本永远运行时强制curl超时有什么想法吗?

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); 

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); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月朦胧 2024-08-11 19:48:26

我正在使用 Curl 库从 Apache 服务器下载文件。
downloadFileWithCurlLibrary 函数有两个输入。
FileUrl 是将从服务器下载的文件。
示例:www.abc.com/myfile.doc。
locFile 是要保存的目录和文件名。
示例:/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;
}

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;
}
苯莒 2024-08-11 19:48:25

curl 有两种不同的超时 - 请参阅curl_setopt 手册页面

CURLOPT_CONNECTTIMEOUT
数字
尝试时等待的秒数
连接。使用 0 无限期等待。

和 :

CURLOPT_TIMEOUT
最大数量
允许 cURL 函数执行的秒数
执行。

它们都有一个“毫秒”版本:分别是 CURLOPT_CONNECTTIMEOUT_MSCURLOPT_TIMEOUT_MS

在您的情况下,您可能也想配置第二个:似乎花费时间的不是连接,而是服务器端提要的构建。

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.

吻泪 2024-08-11 19:48:25

查看 CURLOPT_CONNECTTIMEOUT 和 CURLOPT_TIMEOUT 之间的区别

See the difference between CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文