Maneesh Rao
Maneesh Rao

Reputation: 184

Covert the curl command into php

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);

Upvotes: 3

Views: 83

Answers (2)

tevemadar
tevemadar

Reputation: 13205

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.

Upvotes: 3

hanshenrik
hanshenrik

Reputation: 21465

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 );

Upvotes: 0

Related Questions