标签:php soap soap-client
我正在尝试学习如何使用PHP和SOAP在客户端和服务器之间传输文件(.zip文件).目前我的设置看起来像这样:
require('libraries/nusoap/nusoap.php');
$server = new nusoap_server;
$server->configureWSDL('server', 'urn:server');
$server->wsdl->schemaTargetNamespace = 'urn:server';
$server->register('sendFile',
array('value' => 'xsd:string'),
array('return' => 'xsd:string'),
'urn:server',
'urn:server#sendFile');
但我不确定如果不是字符串,返回类型应该是什么?我正在考虑使用base64_encode.
解决方法:
为了更清楚,我发布了server.php代码和client.php代码.请看下面:
## server.php ##
require_once('lib/nusoap.php'); //include required class for build nnusoap web service server
// Create server object
$server = new soap_server();
// configure WSDL
$server->configureWSDL('Upload File', 'urn:uploadwsdl');
// Register the method to expose
$server->register('upload_file', // method
array('file' => 'xsd:string','location' => 'xsd:string'), // input parameters
array('return' => 'xsd:string'), // output parameters
'urn:uploadwsdl', // namespace
'urn:uploadwsdl#upload_file', // soapaction
'rpc', // style
'encoded', // use
'Uploads files to the server' // documentation
);
// Define the method as a PHP function
function upload_file($encoded,$name) {
$location = "uploads\\".$name; // Mention where to upload the file
$current = file_get_contents($location); // Get the file content. This will create an empty file if the file does not exist
$current = base64_decode($encoded); // Now decode the content which was sent by the client
file_put_contents($location, $current); // Write the decoded content in the file mentioned at particular location
if($name!="")
{
return "File Uploaded successfully..."; // Output success message
}
else
{
return "Please upload a file...";
}
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
================================================== ===================
## client.php ##
require_once('lib/nusoap.php'); //include required class for build nnusoap web service server
$wsdl="http://localhost:81/subhan/webservice3/server.php?wsdl"; // SOAP Server
if($_POST['submit'])
{
$tmpfile = $_FILES["uploadfiles"]["tmp_name"]; // temp filename
$filename = $_FILES["uploadfiles"]["name"]; // Original filename
$handle = fopen($tmpfile, "r"); // Open the temp file
$contents = fread($handle, filesize($tmpfile)); // Read the temp file
fclose($handle); // Close the temp file
$decodeContent = base64_encode($contents); // Decode the file content, so that we code send a binary string to SOAP
}
$client=new soapclient($wsdl) or die("Error"); // Connect the SOAP server
$response = $client->__call('upload_file',array($decodeContent,$filename)) or die("Error"); //Send two inputs strings. {1} DECODED CONTENT {2} FILENAME
// Check if there is anny fault with Client connecting to Server
if($client->fault){
echo "Fault {$client->faultcode} <br/>";
echo "String {$client->faultstring} <br/>";
}
else{
print_r($response); // If success then print response coming from SOAP Server
}
<form name="name1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="uploadfiles"><br />
<input type="submit" name="submit" value="uploadSubmit"><br />
</form>
=================================================
您需要做的就是下载nusoap.php,它将在肥皂库http://sourceforge.net/projects/nusoap/中看到
这是经过全面测试的,它将100%正常运行.
标签:php,soap,soap-client 来源: https://codeday.me/bug/20190928/1829287.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。