2009-09-13 40 views
7

Estoy usando CURL para conectarme a múltiples feeds xml y procesarlos cuando se carga la página. Desafortunadamente, de vez en cuando una página no responde y mi script también se detiene. Aquí hay un ejemplo del código con el que estoy trabajando. Establecí el tiempo de espera en 1, pero parece que no funciona. Luego configuré el tiempo de espera en 0.0001 solo para probar cosas hoy y aún así obtuve feeds xml. ¿Ustedes tienen alguna idea sobre cómo obligar a curl al tiempo de espera cuando un script tarda una eternidad?curl connect timeout no funciona

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

Respuesta

0

ver la diferencia entre CURLOPT_CONNECTTIMEOUT y CURLOPT_TIMEOUT

+1

Lo sentimos, pero esta respuesta no da ninguna información útil. Por supuesto, estas son opciones diferentes y, por supuesto, tenemos que ver la diferencia :) – DarkSide

37

Hay dos tiempos de espera diferentes con el enrollamiento - ver curl_setopt manual's page:

CURLOPT_CONNECTTIMEOUT
El número de segundos de espera al intentar conectar . Use 0 para esperar indefinidamente.

Y:

CURLOPT_TIMEOUT
El número máximo de segundos para permitir que las funciones CURL a ejecutar.

Ambos tienen una "milisegundos" versión: CURLOPT_CONNECTTIMEOUT_MS y CURLOPT_TIMEOUT_MS, respectivamente.


En su caso, es posible que desee configurar el segundo demasiado: lo que parece tomar el tiempo no es la conexión, pero la construcción de la alimentación en el lado del servidor.

+0

Muchas gracias, esto fue extremadamente útil. Estoy tratando de probar para ver si funciona y para hacerlo establecí el tiempo de espera en 1 como en el siguiente código. Estoy analizando xmls con 50-100 elementos, así que me imagino que tomaría más de un milisegundo. Esperaba obtener ningún resultado, pero en cambio todo volvió como siempre. \t curl_setopt ($ curl [$ k], CURLOPT_TIMEOUT_MS, 1); ¿Me falta algo? Muchas gracias Pascal Russ –

+0

buen ejemplo aquí Pascal –

0

estoy usando la biblioteca de Curl para descargar un archivo de Apache Server. downloadFileWithCurlLibrary fonction tiene dos entradas. FileUrl es el archivo que descargará el servidor de formularios. Ejemplo: www.abc.com/myfile.doc. locFile es el directorio y el nombre de archivo que desea guardar. Ejemplo: /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; 
} 
+0

Probablemente recibirías votos si explicas esto más y muestra una demostración de las funciones de devolución de llamada. –

+0

Cuando se produce el tiempo de espera. Esta función devuelve -1. – omeraygor

Cuestiones relacionadas