The basics of SOAP used with PHP
I know that it's not the main idea of this site to answer such kind of questions but in couple of days I'll have the chance to apply for a junior (or maybe more correctly a probationer) position as a PHP programmer and that's why I decided to post here, hoping it will turn out good. The company is some kind of big compared to others here, so it's well known what is the exam for people wanting to get in this position - it's either - writing a pagination script or some sort of SOAP service.I have no problem with the pagination, but since now I have never payed too much attention to SOAP and now I need to learn tha basics ot SOAP service when used with PHP.Giving the postion I'm applying for, noone expect to show something brilliant but I still I need basic understanding of SOAP client and server sevices, maybe I won't even bother for now about WSDL since I don't think I have enough time for everything. So I have a sample code that is most likely what I'll need to write and explain If I'm to write SOAP service :
Client side -
<?php
if (isset($_REQUEST["cname"]) && isset($_REQUEST["cpass"]))
{
$cname = $_REQUEST["cname"];
$md5pass = md5( $_REQUEST["cpass"]);
$client = new SoapClient(null, array(
'location' => "http://localhost/test/BuildInSoapWithWSDL/server.php",
'uri' => "urn://localhost/test/BuildInSoapWithWSDL/",
'trace' => 1 ));
try
{
if开发者_运维问答 ( $client->saveUserNameAndPass($cname, $md5pass))
{echo "Data updated!";}
else
{echo "Error updating data!";}
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";
}
catch (Exception $e)
{
echo 'Exception: ', $e->getMessage(), "\n";
}
}
else
{
echo "Error!";
}
?>
server side -
<?php
$server = new SoapServer(null, array('uri' => 'urn://localhost/test/BuildInSoapWithoutWSDL/'));
$server->addFunction("saveUserNameAndPass");
$server->handle();
function database_connect($host, $account, $password, $dbname)
{
$connect = mysql_connect($host, $account, $password);
$connect = mysql_select_db($dbname, $connect);
return $connect;
}
function saveUserNameAndPass($userName,$passWord)
{
try
{
if (database_connect("localhost", "saveuser", "123456", "savetask") == 1)
{
$userName = mysql_real_escape_string($userName);
$sql = "INSERT INTO accounts (name,passmd5) VALUES ('".$userName."','".$passWord."')";
$result = mysql_query($sql);
mysql_close();
if ($result)
{ return true;}
else
{ return false;}
}
else
{
return false;
}
}
catch (Exception $e)
{
return false;
}
}
?>
Even I have the code I still have poor knowledge of what do what.So I need some explanation ot the basics when writing SOAP service and if it's not acceptable this topci to be discussed here I would appreciate any kind of source where these thing are explained from the point of beginner.
Thanks
SOAP is used just the same as you interact with MySQL database. I can give you real life example, for connections to Atlassian JIRA web application.
At first, you just make connection. You will need an WSDL file, that contains all the stuff, that contains every function that this specific SOAP server allows you to do:
try { $soapObject = new SoapClient('http://jira/rpc/soap/jirasoapservice-v2?wsdl'); }
catch(Exception $ex) { die('SOAP connection failed: '$ex->getMessage()); }
After connection has been made, you just use it. If it is needed, login:
$authToken = $soapObject->login(JIRA_LOGIN, JIRA_PASS);
Then send requests to server:
$jqlquery = "status not in (Closed,Resolved) and assignee = user");
try { $issues = $soapObject->getIssuesFromJqlSearch($authToken, $jqlquery, 20); }
catch(Exception $ex) { die('JIRA query failed: '$ex->getMessage()); }
Work on results:
foreach ($issues as $k => $v) { $users[$v->reporter] = array('fullname'=>$soapObject->getUser($authToken,$v->reporter)->fullname,'name'=>$v->reporter); }
$project = $soapObject->getProjectByKey($authToken,"PROJECT");
Note that getIssuesFromJqlSearch, getUser, getProjectByKey and others, are application-specific commands (in this case, all methods/functions are described in JIRA RPC plugin documentation).
That's it. You don't need to "disconnect", afaik when loading finishes, destructor is called, that closes connection by itself.
精彩评论