0

In PHP, a script is making a cURL request to a web service and then reusing the handle to perform more requests to the same web service. Sniffing the connection, I see that it works fine until like the 140th request (the number varies). Then it seems to send no data to the server and it responds with a bad request. If the script keeps trying to perform the requests, it starts to send garbage, the following requests are just strings like this one:

W47Sj47cDxTYW4gTHVpcztEPjtwPFNhbnRhIENydXo7Wj47cDxTYW50YSBGZTtTPjtwPFNhbnRpYWdvIERlbCBFc3Rlcm87Rz47cDxUaWVycmEgRGVsIEZ1ZWdvO1Y%2BO3A8VHVjdW1hbjtUPjs%2BPjs%2BOzs%2BO3Q8cDw7cDxsPG9uRm9jdXM7PjtsP(...)RDYW5jZWxhcicsJycpXDs7Pj4%2BOzs%2BOz4%2BOz4%2BO2w8Y2hrTm9TdW1pbmlzdHJhTWFpbDtjaGtGYWN0dXJhY2lvbjtpbWdCdG5GaWx0cm9GYWN0Oz4%2BaPpaTHB5VO%2ByKbxCglGRNS2EaFE%3D&rblTipoPersona=F&txtNombre=ANIBAL&txtApellido=LAJARA&txtNroDoc=23964779&txtFechaNac=27%2F07%2F1974&txtMail=anibal-nob%40hotmail.com&chkFacturacion=o(...)cmbTipoDoc=1&cmbSexo=M&cmbCicloFacturacion=02&cmbProvinciasFact=S&cmbLocalidadesFact=14244

which looks like just part of the data without headers (dots added by me).

I could (and I will) just reset the connection if I get a bad request response, but I would like to find out why this happens.

What could cause this? Are there limits to how many times a cURL handle can be reused, or how much data a single handle/php instance can transfer or something?

Code is like this:

$ch=curl_init();

while($running) {
    $vars=array(what i need to send);
    $url='http://something/';

    $opts=array();
    $opts[CURLOPT_RETURNTRANSFER]=true;
    $opts[CURLOPT_FAILONERROR]=false;

    $opts[CURLOPT_FORBID_REUSE]=false;
    $opts[CURLOPT_FRESH_CONNECT]=false;

    $opts[CURLOPT_AUTOREFERER]=true;

    $opts[CURLOPT_ENCODING]='gzip,deflate';

    $opts[CURLOPT_TIMEOUT]=0;
    $opts[CURLOPT_CONNECTTIMEOUT]=0;

    $opts[CURLOPT_HTTPHEADER]=array(
        'Expect:',
        'Connection: Keep-Alive',
        'Keep-Alive: 3600'          
    ); 

    $opts[CURLOPT_HEADER]=true; 

    $opts[CURLOPT_POST]=true;
    $opts[CURLOPT_POSTFIELDS]=$vars;

    $opts[CURLOPT_URL]=$url;
    $opts[CURLOPT_FOLLOWLOCATION]=true;

    curl_setopt_array($ch,$opts);

    $res=curl_exec($ch);

    ...
}

The script is being executed from CLI without execution time limit.

Gabriel
  • 2,170
  • 1
  • 17
  • 20
  • 1
    I think it might be a timeout problem. This is just off the top of my head but I believe I remember reading that cURL will only keep a line open so long and then it auto closes it. Let me go do some research. Be right back. – Mark Manning Jun 30 '16 at 01:12
  • You may want to take a look at : https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html – Mark Manning Jun 30 '16 at 01:13
  • Ah! Much better! Look at THIS: http://stackoverflow.com/questions/2582057/setting-curls-timeout-in-php – Mark Manning Jun 30 '16 at 01:16
  • I now have to say that I believe this to be a duplicate of http://stackoverflow.com/questions/2582057/setting-curls-timeout-in-php – Mark Manning Jun 30 '16 at 01:17
  • Hi and thanks. I ommited it in the post, but it also has `$opts[CURLOPT_TIMEOUT]=0;` and `$opts[CURLOPT_CONNECTTIMEOUT]=0;` – Gabriel Jun 30 '16 at 01:17
  • Also the script is being executed from CLI without time limit. – Gabriel Jun 30 '16 at 01:18
  • If it looks like the other answer answers your question - be sure to mark this question as closed. Also, you might want to bring your question up there since some of those people who have answered there might see your question and be able to help you more. (I've just more or less directed you to that question.) – Mark Manning Jun 30 '16 at 01:22
  • It does not look like the other question. I upvoted you for your help, but it's not solved. – Gabriel Jun 30 '16 at 01:25
  • Thank you. I noticed, reading the other question, that they say to remember to up your CPU time limit for PHP. They showed set_time_limit(0);// to infinity for example (msangel). Are you sure it is cURL that is timing out and not PHP? – Mark Manning Jun 30 '16 at 01:26
  • I've updated the question, it hasn't time limit, the script keeps running fine and cURL keeps sending wrong requests but the script never halts, so php time limit is discarded. – Gabriel Jun 30 '16 at 01:29
  • Ok. So last idea : Have you tried breaking up your request into two requests? I know it may seem a bit dumb to have to do that but using FTP I once ran into a timeout problem and it had to do with the connection itself. For unknown reasons the line would just drop. By breaking up my FTP transfers I was able to do everything. It is the only other thing I can think of. :-( – Mark Manning Jun 30 '16 at 01:33
  • Yes, I will probably do something like that as a quick fix, but I still wonder why this happens. Thank you! – Gabriel Jun 30 '16 at 01:36
  • Your welcome! If you figure it out please do post. I'll see it and read what you found out. Always nice to increase what you know. :-) – Mark Manning Jun 30 '16 at 01:37

0 Answers0