PHP中的UploadFile C#替代方案

时间:2014-04-08 17:00:44

标签: c# php curl

我在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#示例不同,因为我收到了内部服务器错误消息。有人知道这些差异吗?

1 个答案:

答案 0 :(得分:0)

; 8尝试将文件直接上传到POSTFIELDS

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

另外,请确保您有权访问xml文件。