我正在集成我的代码以在RSSBus中创建文件连接。 以下是我的Javascript代码:
<?php
$header = base64_encode(USERNAME. ":" . USERPASS);
$content = base64_encode('Welcome');
?>
<div id="result">
Content will loading here...
</div>
<script src="../../../js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var surl = 'http://xxxxxxx/api.rsc/files?x-rssbus-uthtoken='+token+'&@jsonp';
var postdata = {
PortId: "xxxxxxxxx",
Folder: "Send",
Filename: "testfile.xml",
Content: "<?php echo $content; ?>"
};
$.ajax({
type: "POST",
url: surl,
processData: false,
contentType: 'application/json',
data: JSON.stringify(postdata),
dataType: 'jsonp',
crossDomain: true,
}).done(function(data){
var data = JSON.stringify(data);
$("#result").html("<pre>"+data+"</pre>");
}).fail(function(data){
console.log('fail');
});
});
</script>
我在php中运行了这个javascript。所有代码都完美无缺,但无法在“发送”文件夹中创建该文件。我也得到了正确的响应,但它转换为GET参数和错误给出像所有响应中的“405 Method Not Allowed”。当成功响应但文件未在文件夹中创建时。
请尽快向我提供解决方案。
答案 0 :(得分:2)
请尝试使用以下代码。我成功地创造了文件。
$url = 'xxxxxxxxxxxxxxxx/api.rsc/files';
$portid = 'PORT';
$folder = 'Send or Receiver Folder name';
$filename = 'mytest.xml';
$content = base64_encode('file content');
$fields = array( 'PortId' => $portid, 'Filename' => $filename, 'Folder' => $folder, 'Content' => $content);
$data_string = json_encode($fields);
$header = array('Authorization: Basic '.base64_encode(AS2_USERNAME. ":" . AS2_USERPASS)
,'Content-Type: application/json'
,'Content-Length: '.strlen($data_string)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
if(!curl_errno($ch)){
$info = curl_getinfo($ch);
_printr($info);
echo "<br /><br />";
}else{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
var_dump( $result );