2014-04-08 53 views
1

我已經在C#中被賦予此代碼示例UploadFile C#替代

var fileUploadClient = new WebClient(); 
fileUploadClient.Headers.Add("Content-Type", "application/xml"); 
var rawResponse = fileUploadClient.UploadFile(Url, FilePath); 

我想用複製這個在PHP:

$handle = curl_init(); 
    curl_setopt($handle, CURLOPT_URL, "http://api.dev/import"); 
    curl_setopt($handle, CURLOPT_HTTPHEADER, array(
     'Content-Type: application/xml', 
     'Authorization: api '.$this->token 
    )); 
    curl_setopt($handle, CURLOPT_HEADER, true); 
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($handle, CURLOPT_VERBOSE, true); 
    curl_setopt($handle, CURLOPT_POST, true); 
    curl_setopt($handle, CURLOPT_POSTFIELDS, array('file' => new \CurlFile('/tmp/import.xml', 'application/xml', 'import.xml'))); 

    $response = curl_exec($handle); 
    $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); 

但是好像這是不相同到C#示例,因爲我收到了內部服務器錯誤消息。有人知道不同之處嗎?

+0

目錄權限? – Maximus2012

回答

0

; 8Try將文件直接上傳到POSTFIELDS

## also remember to use full path 
curl_setopt($handle, CURLOPT_POSTFIELDS, file_get_contents('/home/dir/import.xml')); 

此外,請確保您有權限訪問XML文件。

+0

沒有區別,我仍然得到相同的錯誤。我有權訪問該文件,權限不是問題。 – Christoffer

+0

這似乎是他們在示例中使用的函數。它只是說我將文件發佈到服務器。有人知道我和我在這裏的區別嗎? http://msdn.microsoft.com/en-us/library/ms144229.aspx – Christoffer