3

I have tried to convert the curl command from https://incarnate.github.io/curl-to-php/ URL. but they are not giving me proper php code for that. Can you please help out.

curl -i -F account_id=12345 -F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F [email protected]  https://url/upload

I tried this code to convert into php code. but not getting proper output.

 $cmd = "curl -i -F 
                account_id=12345 -F 
                authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F 
                [email protected] 
                https://url/upload";
        exec($cmd,$result);
8
  • what are you getting instead of the proper output? Commented Mar 23, 2018 at 11:57
  • You are getting correct thing:- // Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://url/upload"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close ($ch); Commented Mar 23, 2018 at 11:59
  • @FedericoklezCulloca Nothing is showing on output. Commented Mar 23, 2018 at 12:07
  • @AlivetoDie OP wants to send an encoded form (-F, which also implies a POST request), and get back HTTP headers (-i). The converter site completely disregards these details. Commented Mar 23, 2018 at 12:08
  • @AlivetoDie If they are providing correct thing. where is my authHash, accountId and audio file? Commented Mar 23, 2018 at 12:09

2 Answers 2

3

To summarize the comments:

curl -i -F account_id=12345 -F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F [email protected]  https://url/upload

is going to be

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://url/upload");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
    array(
       'account_id' => '12345',
       'authhash' => 'BKPda_T497AX4EjBsk3ttw9OcOzk',
       'audioFile' => new CURLFile('CasioVoice.wav')));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);

$server_output = curl_exec ($ch);
curl_close ($ch);

And then you may have to fight with https, depending on the server's certification. If you need that, CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST are some options to look into, but let's hope you will not need them.

Sign up to request clarification or add additional context in comments.

8 Comments

The code is executed now. but my audio file is not pushing to the third party. I am using $filepath = '@' . realpath('/callRecord/CasioVoice.wav')
I have putted the correct authHash and account_id in curl call. but it giving me 401 error. While same thing I am doing on curl command. it is working. Please help where I am lacking?
no it's not, you're wrong. guys, stop upvoting this post, this is wrong. this would send a application/x-www-form-urlencoded-formatted request with no file attached whatsoever, OP's code is actually uploading a file, and using the multipart/form-data-format.
@hanshenrik yes, you are right, http_build_query was a trap. The @ syntax is expected to work, so I just leave it be.
@ManeeshRao remove the http_build_query thing, that was a bad idea. Also, it is not clear if you want to get the response or pass it through your code - in the latter case curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); should be commented/removed.
|
0

your PHP code try to execute curl with a bunch of newlines in the parameters, and the newlines are confusing curl. get rid of the newlines, and it should work.

$cmd = "curl -i -F account_id=12345 -F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F [email protected] https://url/upload";

or use concatenation to avoid the newlines at runtime, while still having them in the source code,

 $cmd = "curl -i ".
        "-F account_id=12345 ".
        "-F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk ".
        "-F [email protected] " .
        "https://url/upload";

, ps, you can also use php's libcurl wrapper to the same effect,

$ch = curl_init ();
curl_setopt_array ( $ch, array (
        CURLOPT_POSTFIELDS => array (
                "account_id" => 12345,
                "authhash" => "BKPda_T497AX4EjBsk3ttw9OcOzk",
                "audioFile" => new CURLFile ( "CasioVoice.wav" ) 
        ),
        CURLOPT_URL => "https://url/upload",
        CURLINFO_HEADER_OUT=>1
) );
curl_exec ( $ch );
curl_close ( $ch );

Comments

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.