1

I have a curl script that is working fine in terminal.

curl -XPOST \
     -F "file=@var/www/myfile.wav;type=audio/wav" \
     -H 'Authorization: Bearer $MY_TOKEN' \
     'https://myurl'

I have tried this way in my php file,

<?php

    $url = "https://myurl";
    $key =  "myKey";

    $path = "/var/www/";
    $fileName = "myfile.wav";
    $data['file']=  "@".$path.$fileName;
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $key"));

    $response = curl_exec($ch);

    curl_close($ch);

    echo $response;

?>

I get an error from the server. Seems I am not making request as the server expects. Am I missing anything in the php script?

2 Answers 2

1

Try this:

$data = array('file' => file_get_contents($path.$fileName));

There may be a problem with your CURL library that PHP is using.

Or PHP may be blocked from making HTTPS connections.

Try a series of simple tests using CURL in PHP like this first one with POST false:

<?php 

    $url = "https://myurl/";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    header("Content-Type: text/plain");
    echo $response;
?>

and then with POST true...

<?php 

    $url = "https://myurl/";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    header("Content-Type: text/plain");
    echo $response;
?>
Sign up to request clarification or add additional context in comments.

5 Comments

@Rifat it's also possible there is a bug in the curl library. There doesn't seem to be any difference in the terminal, and the PHP script.
The server just returns error occurred. if I do an exec on my script its getting executed but no luck with the php script !
@Rifat If your question included the values for your url and key/token then I could use a HTTP debugger to find out what's different. Otherwise, I'd recommend using Charles (on Mac) or Fiddler (on windows)
@Rifat I was able to upload to my own server using exactly the same code you posted that you say doesn't work. So I know it does work, customized for my server :)
I am using the exec command for the time being. No idea what to do instead!
0

Since it's an HTTPs URL , You need to make use of this parameter

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

2 Comments

What error do you get ? doesn't work really won't help anyone answer your question. Could you elaborate the error ?
The server is just returning "en error occurred". its a third party service I am using. But the server returns correctly when I run the curl script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.