2012-07-31 104 views
0

我正尝试使用PHP将文件提交到Desire2Learn Dropbox。我已经收集了代理上的文件没有问题;它试图将文件从代理传输到出现问题的Dropbox。我正在使用他们的Valence API通过PHP向Desire2Learn Dropbox提交文件

进行此调用的用户上下文在系统上具有足够的权限来执行此操作。

错误是“未知错误发生”。

$attachment=$_FILES["file"]["tmp_name"]; 
$attachmentName=$_FILES["file"]["name"]; 
$type=$_FILES["file"]["type"]; 
$file_name_with_full_path="files_dir/" . $_FILES["file"]["name"]; 
$file_to_upload = array('file_contents'=>'@'.$file_name_with_full_path); 

require_once 'libsrc/D2LAppContextFactory.php'; 
$errorArray = array(
    D2LUserContext::RESULT_OKAY => "Success", 
    D2LUserContext::RESULT_INVALID_SIG => "Invalid signature.", 
    D2LUserContext::RESULT_INVALID_TIMESTAMP => "There is a time skew between server and local machine. Try again.", 
    D2LUserContext::RESULT_NO_PERMISSION => "Not authorized to perform this operation.", 
    D2LUserContext::RESULT_UNKNOWN => "Unknown error occured" 
); 

$port=443; 
$host="subdomain.domain.com"; 

session_start(); 
foreach (array('appId', 'appKey', 'userId', 'userKey') as $e) { 
    if (!isset($_SESSION[$e]) || $_SESSION[$e] == '') { 
     die("Missing $e. Please authenticate first."); 
    } 
} 

$appKey = $_SESSION['appKey']; 
$appId = $_SESSION['appId']; 
$userId = $_SESSION['userId']; 
$userKey = $_SESSION['userKey']; 
session_write_close(); 

$authContextFactory = new D2LAppContextFactory(); 
$authContext = $authContextFactory->createSecurityContext($appId, $appKey); 
$opContext = $authContext->createUserContext($host, $port, true, $userId, $userKey); 

$target_url = $opContext->createAuthenticatedUri('/d2l/api/le/1.0/00004/dropbox/folders/00007/submissions/mysubmissions/', 'POST'); 

$ch = curl_init(); 

$random_hash = md5(date('r', time())); 
$comment = array('Text'=>'some comment','HTML'=>null); 
$comment = json_encode($comment); 

$fp = fopen("files_dir/" . $_FILES["file"]["name"], 'rb'); 

ob_start(); //Turn on output buffering 
fpassthru($fp); 
$filedata = ob_get_clean(); 

$request_pre="--".$random_hash."\r\nContent-Type: application/json\r\n".$comment."\r\n--".$random_hash."\r\nContent-Disposition:form-data; name=\"\"; filename=".$attachmentName."\r\nContent-Type:".$type."\r\n\r\n".$filedata."\r\n--".$random_hash."--\r\n"; 

$request=nl2br($request_pre); 

$length=strlen($request); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("HTTP/1.1", "Content-Type: multipart/mixed; boundary=".$random_hash,"Host:".$host, "Content-length: ".$length)); 
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
curl_setopt($ch, CURLOPT_CAINFO, getcwd().'/cacert.pem'); 
$response = curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); 
$responseCode = $opContext->handleResult($response, $httpCode, $contentType); 
$error_no = curl_errno($ch); 

fclose($fp); 

if ($responseCode == D2LUserContext::RESULT_OKAY) { 
    $ret = "$response"; 
    $tryAgain = false; 
} elseif ($responseCode == D2LUserContext::RESULT_INVALID_TIMESTAMP) { 
    $tryAgain = true; 
} else { 
    $ret = "{$errorArray[$responseCode]}<br/>$response"; 
    $tryAgain = false; 
} 
if ($error_no == 0) { 
    $error = 'File uploaded succesfully.'; 
} else { 
    $error = 'File upload error.'; 
} 
echo "error: ".$error."<br/>"; 


curl_close ($ch); 
echo $result; 

POST字段看起来像这样(该文件看起来不对我'86761')。

--565cef73df4da0f072b9626f4fae7e50 
Content-Type: application/json 
{"Text":"some comment","HTML":null} 
--565cef73df4da0f072b9626f4fae7e50 
Content-Disposition:form-data; name=""; filename="example.pdf" 
Content-Type:application/pdf 
{86761} 
--565cef73df4da0f072b9626f4fae7e50-- 

Unknown error occured 
error: File uploaded successfully 
+0

实际上会发生什么问题?错误?警告? – halfer 2012-07-31 13:37:07

+0

备注:由于D2L不太可能是众所周知的,因此可能需要在GitHub(等)上链接到他们的站点和/或其API,以便读者可以轻松查看库代码。 – halfer 2012-07-31 13:58:17

+0

感谢您的建议。我已经按要求添加了更多细节。我怀疑我在执行fopen/fpassthru时存在问题,但我看不出问题所在。 – moe 2012-07-31 14:17:30

回答

0

我会尝试添加一个简单的text/plain类型的文本文件以查看是否有效。使用你的代码(经过一些修改),我能够将文件提交给Valence的保管箱。

// read file 
$fp = fopen($location, 'r'); 
$contents = fread($fp, filesize($location)); 
fclose($fp); 

$request_pre="--".$random_hash."\r\nContent-Type: application/json\r\n\r\n{\"Text\":\"Some comment\", \"HTML\":null}\r\n\r\n--".$random_hash."\r\nContent-Disposition: form-data; name=\"\"; filename="."\"test1.txt\""."\r\nContent-Type: ".$type."\r\n\r\n".$contents."\r\n\r\n--".$random_hash; 

$request = $request_pre; //nl2br($request_pre); 

$length=strlen($request); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("HTTP/1.1", "Content-Type: multipart/mixed; boundary=".$random_hash)); 
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
$response = curl_exec($ch); 

只是为了说明我已经发现使用PHP Curl工作得不够理想。考虑使用HTTPRequest包。