php curl 连接超时不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1418583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
curl connect timeout not working
提问by Pascal MARTIN
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.
我正在使用 CURL 连接到多个 xml 提要并在页面加载时处理它们。不幸的是,每隔一段时间页面就会没有响应,我的脚本也会停止。这是我正在使用的代码示例。我将超时设置为 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);
采纳答案by timdev
See the difference between CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT
查看 CURLOPT_CONNECTTIMEOUT 和 CURLOPT_TIMEOUT 的区别
回答by Pascal MARTIN
There are two different timeouts with curl -- see curl_setoptmanual's page:
curl 有两种不同的超时时间——请参见curl_setopt手册页:
CURLOPT_CONNECTTIMEOUT
The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
CURLOPT_CONNECTTIMEOUT
尝试连接时等待的秒数。使用 0 无限期等待。
And :
和 :
CURLOPT_TIMEOUT
The maximum number of seconds to allow cURL functions to execute.
CURLOPT_TIMEOUT
允许 cURL 函数执行的最大秒数。
They both have a "millisecond" version : CURLOPT_CONNECTTIMEOUT_MSand CURLOPT_TIMEOUT_MS, respectively.
它们都有一个“毫秒”版本 :CURLOPT_CONNECTTIMEOUT_MS和CURLOPT_TIMEOUT_MS,分别。
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.
在您的情况下,您可能还想配置第二个:似乎需要时间的不是连接,而是在服务器端构建提要。
回答by omeraygor
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.
我正在使用 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;
}

