Compared to the standard authentication in the rest of your applications, SOAP authentication isn't too complicated. There are two easy ways of integrating it into your SOAP server: using HTTP Basic authentication or processing a custom SOAP header.
Make sure you have soap enabled in your server machine. Write "phpinfo();" and look for the word "Soap Client" as below:
If you don't find above soap enabled then you have to enable it. Navigate explorer to /c/xampp/php and edit "php.ini" and uncomment the line marked in below image and restart server and check again if your server has soap enabled:
Soap-server:
<?php
function pc_authenticate_user($username, $password)
{
$is_valid = false;
if(strlen(trim($username)) > 0 && trim($username) == "adm" && strlen(trim($password)) > 0 && trim($password) == "pwd") {
$is_valid = true;
}
if ($is_valid) {
return true;
}
else {
return false;
}
}
class pc_SOAP_return_time
{
public function __construct() {
// Throw SOAP fault for invalid username and password combo
if (!pc_authenticate_user($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) ) {
throw new SOAPFault("Incorrect username and password combination.", 401);
}
}
function return_time($name, $lastName) {
return "Time: ".time().", First Name: ".$name.", Last Name: ".$lastName;
}
function accept_xml() {
$postData = file_get_contents("php://input");
return $postData;
}
}
$server = new SOAPServer(null, array('uri' => 'urn:pc_SOAP_return_time'));
$server->setClass('pc_SOAP_return_time');
$server->handle();
Soap-client
<?php $opts = array( 'location' => 'http://localhost/ci/dragon71/soap-check/server.php', 'uri' => 'urn:pc_SOAP_return_time', 'login' => 'adm', 'password' => 'pwd' ); $client = new SOAPClient(null, $opts); $result = $client->__soapCall('return_time', array("Pritom", "Kumar")); print_r($result);
Or
<?php $xml = '<?xml version="1.0" encoding="utf-8"?>'. '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'. ' xmlns:xsd="http://www.w3.org/2001/XMLSchema"'. ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'. '<soap:Body>'. '<accept_xml xmlns="http://localhost/ci/dragon71/soap-check/server.php/">'. '<ItemId>15</ItemId>'. '</accept_xml>'. '</soap:Body>'. '</soap:Envelope>'; $url = "http://localhost/ci/dragon71/soap-check/server.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $headers = array(); array_push($headers, "Content-Type: text/xml; charset=utf-8"); array_push($headers, "Accept: text/xml"); array_push($headers, "Cache-Control: no-cache"); array_push($headers, "Pragma: no-cache"); if($xml != null) { curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml"); array_push($headers, "Content-Length: " . strlen($xml)); } curl_setopt($ch, CURLOPT_USERPWD, "adm:pwd"); /* If required */ curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); header('Content-Type: text/xml'); print_r($response);
Make sure you have soap enabled in your server machine. Write "phpinfo();" and look for the word "Soap Client" as below:
If you don't find above soap enabled then you have to enable it. Navigate explorer to /c/xampp/php and edit "php.ini" and uncomment the line marked in below image and restart server and check again if your server has soap enabled:
No comments:
Post a Comment