2013-12-08 26 views
1

我有一个在终端工作正常的卷曲脚本。PHP:卷曲脚本工作,但不是在PHP文件

curl -XPOST \ 
    -F "[email protected]/www/myfile.wav;type=audio/wav" \ 
    -H 'Authorization: Bearer $MY_TOKEN' \ 
    'https://myurl' 

我已经试过这种方式在我的PHP文件,

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

?> 

我得到一个错误,从服务器。似乎我没有按照服务器的要求提出请求。我错过了PHP脚本中的任何东西吗?

回答

0

因为它是一个HTTPS URL,你需要把这个参数设置的使用

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
+0

没有工作:( –

+0

你收到什么错误?“不起作用”真的不会帮助任何人回答你的问题。你能详细说明错误吗? –

+0

服务器刚刚返回“en error occurred”。它是我正在使用的第三方服务。当我运行curl脚本时服务器正确返回。 –

1

试试这个:

$数据=阵列( '文件'=>的file_get_contents($路径$文件名) );

PHP正在使用的CURL库可能存在问题。

或者PHP可能被阻止进行HTTPS连接。

尝试了一系列使用curl在PHP这样第一个与POST假简单的测试:

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

,然后用POST真的......

<?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; 
?> 
+0

nope。没有运气:( –

+0

@Rifat也有可能是curl库中有一个bug,终端和PHP脚本似乎没有任何区别 –

+0

@Rifat试试我更新的测试... –