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 audioFile=@CasioVoice.wav  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 
                audioFile=@CasioVoice.wav 
                https://url/upload";
        exec($cmd,$result);
Maneesh Rao
  • 174
  • 2
  • 18
  • what are you getting instead of the proper output? – Federico klez Culloca Mar 23 '18 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);` – Serving Quarantine period Mar 23 '18 at 11:59
  • @FedericoklezCulloca Nothing is showing on output. – Maneesh Rao Mar 23 '18 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. – tevemadar Mar 23 '18 at 12:08
  • @AlivetoDie If they are providing correct thing. where is my authHash, accountId and audio file? – Maneesh Rao Mar 23 '18 at 12:09
  • Possible duplicate of [PHP + curl, HTTP POST sample code?](https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) – tevemadar Mar 23 '18 at 12:12
  • The only thing missing from the linked question is the ```-i``` part, you need CURLOPT_HEADER for that: ```curl_setopt($ch, CURLOPT_HEADER, 1);```. Documentation: http://php.net/manual/en/function.curl-setopt.php – tevemadar Mar 23 '18 at 12:17

2 Answers2

3

To summarize the comments:

curl -i -F account_id=12345 -F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F audioFile=@CasioVoice.wav  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.

tevemadar
  • 9,697
  • 2
  • 15
  • 36
  • The code is executed now. but my audio file is not pushing to the third party. I am using $filepath = '@' . realpath('/callRecord/CasioVoice.wav') – Maneesh Rao Mar 23 '18 at 12:40
  • 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? – Maneesh Rao Mar 23 '18 at 13:27
  • 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 Mar 23 '18 at 15:08
  • @hanshenrik yes, you are right, ```http_build_query``` was a trap. The ```@``` syntax is expected to work, so I just leave it be. – tevemadar Mar 23 '18 at 15:20
  • @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. – tevemadar Mar 23 '18 at 15:27
  • @tevemadar no, the @ syntax is NOT expected to work anymore. @ was deprecated in PHP 5.5 (in favor of CURLFile ) , was disabled-by-default in PHP 5.6 (see the CURLOPT_SAFE_UPLOAD documentation), and completely removed in PHP7. – hanshenrik Mar 23 '18 at 15:40
  • @hanshenrik great, it's PHP being PHP: ```CURLOPT_POSTFIELDS ``` only says "*As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.*" – tevemadar Mar 23 '18 at 15:46
  • @tevemadar damn, you're right. someone should fix the CURLOPT_POSTFIELDS documentation. – hanshenrik Mar 23 '18 at 15:48
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 audioFile=@CasioVoice.wav 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 audioFile=@CasioVoice.wav " .
        "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 );
hanshenrik
  • 15,263
  • 3
  • 28
  • 61