Curl error: operation aborted by callback
Asked Answered
U

1

5

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.

Uncivil answered 10/8, 2020 at 13:59 Comment(11)
print_r($result);print' ~ what is the extra print for???Sate
Just allows for HTML outputtingUncivil
Please put proper blankspaces after , and . and before { it's horrible to read parametersMass
@Uncivil - look closely at that line.... .Sate
Which line sorry?Uncivil
print_r($result);print' - notice anything that might cause an error? .?Sate
The whole line reads print'<pre>Curl (rec): '."\n";print_r($result);print'</pre>'; so no ... but equally the error is output before this point??Uncivil
my fault - I misread that line - apologies for that.Sate
Where in the target url does the request fail? Or does the target url process the request OK?Sate
Np @ProfessorAbronsius! The initial stage processes and makes it back to the source page. I've tried to exit and output at the various stages but no output/response. Weirdly the failure happens quite early on - it would appear before the post has fully finished.Uncivil
Perhaps, if you have not already reviewed this it might be useful - It deals with a case in C but still might be applicable. As you are using v7.4 this also might be useful ?Sate
U
10

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.

Uncivil answered 10/8, 2020 at 16:9 Comment(1)
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.Sikkim

© 2022 - 2024 — McMap. All rights reserved.