2

So I have obviously googled the error - but PHP (PHP 7.4.4 (cli)) curl gives me the error:

Curl error: operation aborted by callback with the following code:

private function curl_post($url,$post,$file = null,$file_type = 'audio/wav'){
    $ch = curl_init($url);

    if (!empty($file)){
        $post['SoundFile'] = new CURLFile(UPLOAD_PATH.$file,$file_type,$file);
    }

    // Assign POST data
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);

    if(curl_errno($ch)) echo 'Curl error: '.curl_error($ch);

    curl_close($ch);

    print'<pre>Curl (rec): '."\n";print_r($result);print'</pre>';
}

I control both (Ubuntu) servers and have rebooted them both. I am posting a fairly large amount of data but in the Google searching this didn't seem to be what is triggering the curl_error. Does anyone know what is causing it? It was working perfectly fine and then it stopped.

Additionally putting file_put_contents(time().'.txt','log'); as a break in my receiving server does log the response. So its clearly landing in the right area.

Additionally what I will say is that the 2 servers talk a number of times to each other through curl (so one curls to one then back a bit). Furthermore - error 42 is the CURL response but https://curl.haxx.se/libcurl/c/libcurl-errors.html doesn't seem to provide much help. I've tried tracing the various calls to each other and can't see why it is breaking - it errors/breaks before the post/calls even occur.

Antony
  • 3,875
  • 30
  • 32

1 Answers1

6

So I found the answer and I hope this helps anyone else in this situation. The reason being was because the file was missing on the server for the CURLFile (having previously been there). My code now reads:

    if (!empty($file) && is_file(UPLOAD_PATH.$file)){
        $post['SoundFile'] = new CURLFile(UPLOAD_PATH.$file,$file_type,$file);
    }

And this no longer generates the error. The key was that it errored even before submitting the post but the error itself wasn't that helpful until I broke it down in a separate test script and added the file element back in.

Antony
  • 3,875
  • 30
  • 32
  • Thanks. By the way, a better approach for the **if** condition would be `if (empty($file) || is_file(UPLOAD_PATH.$file)){` so it's also evaluated as false if filename is empty. – José Carlos Sep 09 '21 at 13:43