将curl命令转换为php

时间:2018-03-23 11:56:13

标签: php curl

我试图从https://incarnate.github.io/curl-to-php/网址转换curl命令。但他们没有给我正确的PHP代码。你能帮忙吗?

curl -i -F account_id=12345 -F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F audioFile=@CasioVoice.wav  https://url/upload

我尝试将此代码转换为php代码。但没有得到适当的输出。

 $cmd = "curl -i -F 
                account_id=12345 -F 
                authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F 
                audioFile=@CasioVoice.wav 
                https://url/upload";
        exec($cmd,$result);

2 个答案:

答案 0 :(得分:3)

总结评论:

curl -i -F account_id=12345 -F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F audioFile=@CasioVoice.wav  https://url/upload

将会是

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

然后你可能不得不与https战斗,具体取决于服务器的认证。如果您需要,CURLOPT_SSL_VERIFYPEERCURLOPT_SSL_VERIFYHOST是一些可供选择的选项,但我们希望您不需要它们。

答案 1 :(得分:0)

你的PHP代码尝试使用参数中的一堆换行符执行curl,并且换行符令人困惑。摆脱新线,它应该工作。

$cmd = "curl -i -F account_id=12345 -F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk -F audioFile=@CasioVoice.wav https://url/upload";

或使用连接来避免运行时的换行符,同时仍然在源代码中使用它们,

 $cmd = "curl -i ".
        "-F account_id=12345 ".
        "-F authhash=BKPda_T497AX4EjBsk3ttw9OcOzk ".
        "-F audioFile=@CasioVoice.wav " .
        "https://url/upload";

,ps,你也可以使用php的libcurl包装器来达到同样的效果,

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