2013-12-08 61 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試試我更新的測試... –