আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

CRUD WITH PHP SOAP

I would like to show how can we make a simple CRUD project with PHP Simple Object Access Protocol (SOAP).

Description: SOAP

User Interface

Here is the file structures where I made the project

NOTE: Please check and activated the “extension=php_soap.dll” from php.ini if this extension is not activated yet.

Student Table Query

CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL,
`email` varchar(60) NOT NULL,
`address` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

In the Db.php file has Mysql Database Connection and DML methods insert, update, delete and retrieve the rows

<?php
class Db {

	// Host and Database information
	private $host = "localhost";
	private $user = "root";
	private $pass = "";
	private $db   = "crud_soap";
	private $mysqli;

	public function __construct(){
		
		// Database Connection
		$this->mysqli = new Mysqli($this->host, $this->user, $this->pass, $this->db);
		
		// Checking the connection is okay or not
		if ($this->mysqli->connect_error) {
		    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
		}
	}

	/**
	* Closing the DB connection
	* @params null
	* @return void 
	*/
	public function __destruct(){
		$this->mysqli->close();
	}

	/**
	* Data insertion in student table
	* @params $name, $email, $address
	* @return (int) insert_id
	*/
	public function insert($name, $email, $address){
		$this->mysqli->query("INSERT INTO student (id, name, email, address) VALUES (null, '$name', '$email', '$address')");
		return $this->mysqli->insert_id;
	}

	/**
	* Data updating in student table
	* @params $id, $name, $email, $address
	* @return (boolean) 
	*/
	public function update($id, $name, $email, $address){
		return $this->mysqli->query("UPDATE student SET name='$name', email='$email', address='$address' WHERE id=$id");
	}

	/**
	* Data deletion from student table
	* @params $id
	* @return (boolean) 
	*/
	public function delete($id){
		return $this->mysqli->query("DELETE FROM student WHERE id=$id");
	}

	/**
	* Data retriving from student table
	* @params $condition (optional)
	* @return (array) mixed
	*/
	public function getAll($condition=""){
		$result = $this->mysqli->query("SELECT * FROM student $condition");
		return $result->fetch_all(MYSQLI_ASSOC);
	}

	/**
	* Row data retriving from student table according to $id
	* @params $id
	* @return (array) mixed
	*/
	public function getById($id){
		return $this->mysqli->query("SELECT * FROM student WHERE id=$id")->fetch_assoc();
	}
}
?>

The server.php file has configuration of SOAP SERVER. Here included the Db.php file and made an object of SOAPServer and set the Db Class up.

<?php
include "Db.php";

try {
  $server = new SOAPServer(
    NULL,
    array(
     'uri' => 'http://localhost/crud_soap/lib/server.php'
    )
  );

  // SETTING UP THE Db CLASS
  $server->setClass('Db'); 
  $server->handle();
}

catch (SOAPFault $f) {
  print $f->faultstring; exit;
}

?>

In the client.php file has $client object of SoapClient with server file path as location and uri and trace true. The trace option enables tracing of request so faults can be back traced.

<?php
$client = new SoapClient(null, array(
      'location' => "http://localhost/crud_soap/lib/server.php",
      'uri'      => "http://localhost/crud_soap/lib/server.php",
      'trace'    => 1 
    )
);
?>

CREATE

Okay, Now going to insert data into student table. So, first need a form! Here you go the form create.php

<!DOCTYPE html>
<html>
<head>
	<title>Create Data</title>
</head>
<body>
	<div style="width: 500px; margin: 20px auto;">

		<!-- showing the message here-->
		<div><?php echo $message;?></div>

		<table width="100%" cellpadding="5" cellspacing="1" border="1">
			<form action="create.php" method="post">
			<tr>
				<td>Name:</td>
				<td><input name="name" type="text"></td>
			</tr>
			<tr>
				<td>Email:</td>
				<td><input name="email" type="text"></td>
			</tr>
			<tr>
				<td>Address:</td>
				<td><textarea name="address"></textarea></td>
			</tr>
			<tr>
				<td><a href="read.php">See Data</a></td>
				<td><input name="submit_data" type="submit" value="Insert Data"></td>
			</tr>
			</form>
		</table>
	</div>
</body>
</html>

So, we got the form but now we need the insertion process. Here you go the processing code

<?php
$message = ""; // initial message 
if( isset($_POST['submit_data']) ){

	// Includes client to get $client object
	include 'lib/client.php';

	// Gets the data from post
	$name = $_POST['name'];
	$email = $_POST['email'];
	$address = $_POST['address'];

	/**
	* Calling the "insert" method by "__soapCall" from SOAP SERVER 
	* $client: object of SOAP CLIENT
	* @params: $name, $email, $address
	*/
	if( $client->__soapCall("insert", array($name, $email, $address)) ){
		$message = "Data is inserted successfully.";
	}else{
		$message = "Sorry, Data is not inserted.";
	}
}
?>

NOTE: I am going to make the form and data insertion process in same file for the User Interface.

Finally create.php

<?php
$message = ""; // initial message 
if( isset($_POST['submit_data']) ){

	// Includes client to get $client object
	include 'lib/client.php';

	// Gets the data from post
	$name = $_POST['name'];
	$email = $_POST['email'];
	$address = $_POST['address'];

	/**
	* Calling the "insert" method by "__soapCall" from SOAP SERVER 
	* $client: object of SOAP CLIENT
	* @params: $name, $email, $address
	*/
	if( $client->__soapCall("insert", array($name, $email, $address)) ){
		$message = "Data is inserted successfully.";
	}else{
		$message = "Sorry, Data is not inserted.";
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Create Data</title>
</head>
<body>
	<div style="width: 500px; margin: 20px auto;">

		<!-- showing the message here-->
		<div><?php echo $message;?></div>

		<table width="100%" cellpadding="5" cellspacing="1" border="1">
			<form action="create.php" method="post">
			<tr>
				<td>Name:</td>
				<td><input name="name" type="text"></td>
			</tr>
			<tr>
				<td>Email:</td>
				<td><input name="email" type="text"></td>
			</tr>
			<tr>
				<td>Address:</td>
				<td><textarea name="address"></textarea></td>
			</tr>
			<tr>
				<td><a href="read.php">See Data</a></td>
				<td><input name="submit_data" type="submit" value="Insert Data"></td>
			</tr>
			</form>
		</table>
	</div>
</body>
</html>

READ

Now, I am going to show the read functionality. Here the file read.php
NOTE: In this file included the client.php to getting the $client object and retrieving the all students from student table by calling “getAll” method. Here “__soapCall” is the method of SoapClient where two Parameters are mandatory 1st parameter is “method name (getAll)” and 2nd one is the parameter list of method of 1st parameter ( parameters of getAll ).

<?php
// Includes client to get $client object
include 'lib/client.php';

/**
* Calling the "getAll" method by "__soapCall" from SOAP SERVER 
* $client: object of SOAP CLIENT
* @params: null
*/
$result = $client->__soapCall("getAll", array());

?>
<!DOCTYPE html>
<html>
<head>
	<title>Data List</title>
</head>
<body>
	<div style="width: 500px; margin: 20px auto;">
		<a href="create.php">Create New</a>
		<table width="100%" cellpadding="5" cellspacing="1" border="1">
			<tr>
				<td>Name</td>
				<td>Email</td>
				<td>Address</td>
				<td>Action</td>
			</tr>
			<?php foreach($result as $row) {?>
			<tr>
				<td><?php echo $row['name'];?></td>
				<td><?php echo $row['email'];?></td>
				<td><?php echo $row['address'];?></td>
				<td>
					<a href="update.php?id=<?php echo $row['id'];?>">Edit</a> | 
					<a href="delete.php?id=<?php echo $row['id'];?>" onclick="return confirm('Are you sure?');">Delete</a>
				</td>
			</tr>
			<?php } ?>
		</table>
	</div>
</body>
</html>

UPDATE

update.php here i am retrieving the row data from student table according to the selected id (look at the url of below image to get the selected id). Getting the id from URL by GET method and retrieving the row data by calling “getById” method where $id is the parameter of “getById” method.

$id = $_GET['id']; // id from url

/**
* Calling the "getById" method by "__soapCall" from SOAP SERVER 
* $client: object of SOAP CLIENT
* @params: $id
*/
$data = $client->__soapCall("getById", array($id));
?>

<!DOCTYPE html>
<html>
<head>
	<title>Update Data</title>
</head>
<body>
	<div style="width: 500px; margin: 20px auto;">

		<!-- showing the message here-->
		<div><?php echo $message;?></div>

		<table width="100%" cellpadding="5" cellspacing="1" border="1">
			<form action="" method="post">
			<input type="hidden" name="id" value="<?php echo $id;?>">
			<tr>
				<td>Name:</td>
				<td><input name="name" type="text" value="<?php echo $data['name'];?>"></td>
			</tr>
			<tr>
				<td>Email:</td>
				<td><input name="email" type="text" value="<?php echo $data['email'];?>"></td>
			</tr>
			<tr>
				<td>Address:</td>
				<td><textarea name="address"><?php echo $data['address'];?></textarea> </td>
			</tr>
			<tr>
				<td><a href="read.php">Back</a></td>
				<td><input name="submit_data" type="submit" value="Update Data"></td>
			</tr>
			</form>
		</table>
	</div>
</body>
</html>

We are getting the previous data in above form but we need to process the form data to be updated. Here is the update processing code

<?php
$message = ""; // initial message 

// Includes client to get $client object
include 'lib/client.php';

// Updating the table row with submited data according to id once form is submited 
if( isset($_POST['submit_data']) ){

	// Gets the data from post
	$id = $_POST['id'];
	$name = $_POST['name'];
	$email = $_POST['email'];
	$address = $_POST['address'];

	/**
	* Calling the "update" method by "__soapCall" from SOAP SERVER 
	* $client: object of SOAP CLIENT
	* @params: $id, $name, $email, $address
	*/
	if( $client->__soapCall("update", array($id, $name, $email, $address))  ){
		$message = "Data is updated successfully.";
	}else{
		$message = "Sorry, Data is not updated.";
	}
}

NOTE: I am going to make the form and data update process in same file for the User Interface.

Finally update.php

<?php
$message = ""; // initial message 

// Includes client to get $client object
include 'lib/client.php';

// Updating the table row with submited data according to id once form is submited 
if( isset($_POST['submit_data']) ){

	// Gets the data from post
	$id = $_POST['id'];
	$name = $_POST['name'];
	$email = $_POST['email'];
	$address = $_POST['address'];

	/**
	* Calling the "update" method by "__soapCall" from SOAP SERVER 
	* $client: object of SOAP CLIENT
	* @params: $id, $name, $email, $address
	*/
	if( $client->__soapCall("update", array($id, $name, $email, $address))  ){
		$message = "Data is updated successfully.";
	}else{
		$message = "Sorry, Data is not updated.";
	}
}

$id = $_GET['id']; // id from url

/**
* Calling the "getById" method by "__soapCall" from SOAP SERVER 
* $client: object of SOAP CLIENT
* @params: $id
*/
$data = $client->__soapCall("getById", array($id));
?>

<!DOCTYPE html>
<html>
<head>
	<title>Update Data</title>
</head>
<body>
	<div style="width: 500px; margin: 20px auto;">

		<!-- showing the message here-->
		<div><?php echo $message;?></div>

		<table width="100%" cellpadding="5" cellspacing="1" border="1">
			<form action="" method="post">
			<input type="hidden" name="id" value="<?php echo $id;?>">
			<tr>
				<td>Name:</td>
				<td><input name="name" type="text" value="<?php echo $data['name'];?>"></td>
			</tr>
			<tr>
				<td>Email:</td>
				<td><input name="email" type="text" value="<?php echo $data['email'];?>"></td>
			</tr>
			<tr>
				<td>Address:</td>
				<td><textarea name="address"><?php echo $data['address'];?></textarea> </td>
			</tr>
			<tr>
				<td><a href="read.php">Back</a></td>
				<td><input name="submit_data" type="submit" value="Update Data"></td>
			</tr>
			</form>
		</table>
	</div>
</body>
</html>

DELETE

delete.php In this file deleting the row from student table according to the selected id which id is getting from URL(like update). The deletion confirmation are taking from read.php file (see the below image) by clicking on delete button.

NOTE: If confirmation is “ok” then below code will be executed. Here called the “delete” method with $id as parameter.

<?php

// Includs client to get $client object
include 'lib/client.php';

$id = $_GET['id']; // id from url

/**
* Calling the "delete" method by "__soapCall" from SOAP SERVER 
* $client: object of SOAP CLIENT
* @params: $id
*/
if( $client->__soapCall("delete", array($id)) ){
	$message = "Record is deleted successfully.";
}else {
	$message = "Sorry, Record is not deleted.";
}

echo $message;
?>
<a href="read.php">Back to List</a>

Conclution

It was a simple CRUD with PHP SOAP. You can get an idea from this program to work with PHP SOAP. To download the source code Download Source.

I am Bakul Sinha Full Stack Software Developer. Currently working as a Senior Software Engineer at Byteshake Limited (UK Based Company). I have been working on Web Application Development, Website Design and Development last 5 years and Android Apps Development last 1 year.

7 comments on “CRUD WITH PHP SOAP
  1. Greetings from Venezuela Mr. Sinha, I am doing a project based on SOAP services and when I reviewed their programming code I found it excellent. When trying to execute it I get the following error:
    [Client] DTD are not supported by SOAP
    I have checked in detail with the help of a debugger and I not encounter where this is the problem. I would appreciate your help.

  2. I have problem like “Fatal error: Uncaught Error: Class ‘SoapClient’ not found in C:\xampp\htdocs\crud_soap\lib\client.php:3 Stack trace: #0 C:\xampp\htdocs\crud_soap\read.php(3): include() #1 {main} thrown in C:\xampp\htdocs\crud_soap\lib\client.php on line 3”
    how to fix it? thanks before

    • Hi Aprilia,

      Please check and activated the “extension=php_soap.dll” from php.ini if this extension is not activated yet and restart your xammp or apache. Seems this extension is not activated in your local server.

      Thanks
      Bakul Sinha

      • Hi Bakul

        Thanks a lot, It solved. But i had another problem in read.php. the problem is like this

        Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML document in C:\wamp\www\crud_soap\read.php:11
        Stack trace: #0 C:\wamp\www\crud_soap\read.php(11): SoapClient->__soapCall(‘getAll’, Array) #1 {main} thrown in C:\wamp\www\crud_soap\read.php on line 11
        ( ! ) SoapFault: looks like we got no XML document in C:\wamp\www\crud_soap\read.php on line 11

        I think there is a problem about array in line:
        $result = $client->__soapCall(“getAll”, array());

        I have insert the data, it works, but the data cant be seen. can u help me to fix it? Thanks before Bakul

        Regards,
        Aprilia

Leave a Reply

Your email address will not be published. Required fields are marked *