SOAP in Laravel

By | August 1st 2018 02:23:04 AM | viewed 99 times

SOAP - Server Side CRUD Script in Laravel 5.0:

First in php.ini file enable: extension=php_soap.dll

add the following route in app/Http/route.php

Route::any('soapServer',['uses'=>'Web\SoapServerController@index','as'=>'soapServer']);

Route::resource('soap','Web\SoapController');  
Route::get('soap/glist',['uses'=>'Web\SoapController@getGallaryAsJson', 'as'=>'soap/glist']);   
Route::get('soap/gedit/{id}',['uses'=>'Web\SoapController@editGallaryAsJson', 'as'=>'soap/gedit/id']); 
Route::put('soap/gupdate',['uses'=>'Web\SoapController@postUpdateGallary', 'as'=>'soap/gupdate']); 
Route::post('soap/ginsert',['uses'=>'Web\SoapController@inImage', 'as'=>'soap/ginsert']);
Route::delete('soap/delete/{id}',['uses'=>'Web\SoapController@destroy', 'as'=>'soap/delete/id']);


add a controller as App/Http/Controllers/Web/SoapController.php

<?php namespace App\Http\Controllers\Web;

use Auth;
use Illuminate\Contracts\Auth\Guard;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\User;
use App;
use URL;
use App\Models\Web\WebImageGallery;
use App\Models\Web\Web_Section;
use App\Models\Web\Web_Category;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\File;

class SoapController extends Controller {
    
    public function getGallaryAsJson($params){
           
            $data = array();
            $location = URL::to('/').'/public/images/upload/gallery/';
            
            $limit = $params['limit']?:5;
            $userIns = WebImageGallery::orderBy('id','desc')
                       ->limit($limit)
                       ->get();
            
            foreach($userIns as $userI){
                    $basePath = null;
                    if($userI->web_section_id && $userI->web_category_id){                  
                       $basePath = $location.$userI->web_sections->section_name.'/'.$userI->web_categorys->category_name.'/';
                     }else{                   
                      $basePath = $location.$userI->web_sections->section_name.'/';  
                    }       
                
                
               $data[] = array(
                        'id'=>$userI->id,
                        'title'=>$userI->title,
                        'thumb_path'=>$basePath.$userI->thumb_path
                       ); 
             }

            if($data) {
		        header('Content-type: application/json');
                return json_encode(array('posts'=>$data));
            }else{
               return json_encode(array('posts'=>"no data found")); 
            }
            
        
             
        }

        public function editGallaryAsJson($id){
            
           $location = URL::to('/').'/public/images/upload/gallery/';
           $userIns = WebImageGallery::find($id);
            
           $basePath = null;
           
                if($userIns->web_section_id && $userIns->web_category_id){                  
                   $basePath = $location.$userIns->web_sections->section_name.'/'.$userIns->web_categorys->category_name.'/';
                 }else{                   
                  $basePath = $location.$userIns->web_sections->section_name.'/';  
                } 
             
                  $data = array(
                        'id'=>$userIns->id,
                        'title'=>$userIns->title,
                        'thumb_path'=>$basePath.$userIns->thumb_path,
                        'web_section_id'=>$userIns->web_section_id,
                        'web_category_id'=>$userIns->web_category_id,
                        'active'=>$userIns->active
                       );   

            if($userIns) {
		        header('Content-type: application/json');
                return json_encode($data);
            }else{
               return json_encode(array('0'=>"no data found")); 
            }
            

        }
        
        public function postUpdateGallary($data){

           $insert =null;
           $galleryission = null;           
           $location = null;
           $fileObject = null;

           if($data['file_name']){
               $fileObject = new \Symfony\Component\HttpFoundation\File\UploadedFile($data['fup_ukmFile'],$data['file_name'],$data['type'],$data['size'],null,TRUE); 
           }
          
           try{
  
            if ($data['id']) {
                
                     $location = base_path()."/public/images/upload/gallery/";
                     
                            if($fileObject){

                                       if($data['web_section_id'] && $data['web_category_id']){
                                        $location = $location.DB::select("select * from web_sections where id='".$data['web_section_id']."'")[0]->section_name."/"; 
                                        $location = $location.DB::select("select * from web_categorys where id='".$data['web_category_id']."'")[0]->category_name."/"; 

                                             if(!File::exists($location)) {
                                                  File::makeDirectory($location, 0775, true);
                                              }

                                      }else{

                                          $location = $location.DB::select("select * from web_sections where id='".$data['web_section_id']."'")[0]->section_name."/"; 

                                             if(!File::exists($location)) {
                                                  File::makeDirectory($location, 0775, true);
                                              }

                                      }

                            }                           
                                                                              
                            $galleryission = WebImageGallery::find($data['id']);

                            $galleryission->title = $data['fup_title'];
                            
                            if($fileObject){
                               $galleryission->thumb_path = WebServices::fileUploadThumb($fileObject,true,$location,100,70); 
                               $galleryission->medium_path = WebServices::fileUploadMedium($fileObject,true,$location,300,210); 
                               $galleryission->large_path = WebServices::fileUpload($fileObject,true,$location); 
                               $galleryission->web_section_id= $data['web_section_id'];
                               $galleryission->web_category_id= ($data['web_category_id'])? $data['web_category_id']:null;
                            }

                            $galleryission->active = $data['fup_active'];
                            $galleryission->update_date = Carbon::now();
                            $insert = $galleryission->update(); 

            }
            
              if($insert) {
		           header('Content-type: application/json');
                      return json_encode(array(0=>1));
                }else{
                     header('Content-type: application/json');
                     return json_encode(array('0'=>"no data found")); 
                }

           }catch(Exception $e){  
                header('Content-type: application/json');
                return json_encode(array('0'=>"error")); 
           }      
 
        }
        
        public function inImage($data){
            
           $insert =null;
           $galleryission = null;           
           $location = null;

           $fileObject = new \Symfony\Component\HttpFoundation\File\UploadedFile($data['fup_ukmFile'],$data['file_name'],$data['type'],$data['size'],null,TRUE);
          
           try{
  
            if ($data['fup_title'] && $fileObject) {
                        
                         $location = base_path()."/public/images/upload/gallery/";
                                            
                        if($data['web_section_id'] && $data['web_category_id']){
                          $location = $location.DB::select("select * from web_sections where id='".$data['web_section_id']."'")[0]->section_name."/"; 
                          $location = $location.DB::select("select * from web_categorys where id='".$data['web_category_id']."'")[0]->category_name."/"; 
                          
                               if(!File::exists($location)) {
                                    File::makeDirectory($location, 0775, true);
                                }
                          
                        }else{
                            
                            $location = @$location.DB::table('web_sections')->select('section_name')->where("web_section_id",$data['web_section_id'])->first()->section_name."/"; 
                                                
                               if(!File::exists($location)) {
                                    File::makeDirectory($location, 0775, true);
                                } 
                        }
                                                                               
                            $galleryission = new WebImageGallery();

                            $galleryission->title = $data['fup_title'];
                            $galleryission->thumb_path = WebServices::fileUploadThumb($fileObject,true,$location,100,70); 
                            $galleryission->medium_path = WebServices::fileUploadMedium($fileObject,true,$location,300,210); 
                            $galleryission->large_path = WebServices::fileUpload($fileObject,true,$location);                         
                            $galleryission->web_section_id= $data['web_section_id'];
                            $galleryission->web_category_id= ($data['web_category_id'])? $data['web_category_id']:null;
                            $galleryission->active = $data['fup_active'];
                            $galleryission->create_date = Carbon::now();
                            $insert = $galleryission->save(); 
							
            }
            
          
            
               if($insert) {
		           header('Content-type: application/json');
                      return json_encode(array(0=>1));
                }else{
                     header('Content-type: application/json');
                     return json_encode(array('0'=>"no data found")); 
                }


           }catch(Exception $e){ 
               
                  header('Content-type: application/json');  
                  return json_encode(array('0'=>"error"));
                     
           }      
            

        }
        
        
        public function destroy($id){
            
            $del = null;
            
            $location ="public/images/upload/gallery/";
            $webImageGallerys = WebImageGallery::find($id);
            
                  if($webImageGallerys->web_section_id && $webImageGallerys->web_category_id){
                     
                          $location = $location.DB::select("select * from web_sections where id='".$webImageGallerys->web_section_id."'")[0]->section_name."/"; 
                          $location = $location.DB::select("select * from web_categorys where id='".$webImageGallerys->web_category_id."'")[0]->category_name."/"; 
                          $thumb_file = $location.$webImageGallerys->thumb_path; 
                          $medium_file = $location.$webImageGallerys->medium_path; 
                          $large_file = $location.$webImageGallerys->large_path; 
                          
                               if(File::exists($thumb_file) || File::exists($medium_file) || File::exists($large_file)) {
                                                                                                      
                                    File::delete($thumb_file);
                                    File::delete($medium_file);
                                    File::delete($large_file);
                                }
                          
                        }else{
                            
                            $location = $location.DB::select("select * from web_sections where id='".$webImageGallerys->web_section_id."'")[0]->section_name."/"; 
                            
                            $thumb_file = $location.$webImageGallerys->thumb_path; 
                            $medium_file = $location.$webImageGallerys->medium_path; 
                            $large_file = $location.$webImageGallerys->large_path; 
                                     
                               if(File::exists($thumb_file) || File::exists($medium_file) || File::exists($large_file)) {
                                                                                                      
                                    File::delete($thumb_file);
                                    File::delete($medium_file);
                                    File::delete($large_file);
                                }
                            
                        }

          
             
             
             
              if($webImageGallerys->delete()) {
		           header('Content-type: application/json');
                      return json_encode(array(0=>true));
                }else{
                     header('Content-type: application/json');
                     return json_encode(array(0=>false));
                }
             
             
        }
              

}

add a soap server as App/Http/Controllers/Web/SoapServerController.php

<?php namespace App\Http\Controllers\Web;

use Auth;
use Illuminate\Contracts\Auth\Guard;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use App;
use URL;

use App\Models\Web\WebImageGallery;
use App\Models\Web\Web_Section;
use App\Models\Web\Web_Category;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\File;

class SoapServerController extends Controller {
    
    public $soapInstance = null;
    public function index(){
        ini_set("soap.wsdl_cache_enabled", 0);
        ini_set("session.auto_start", 0); 
       
        $params = array('location'=>'http://localhost/ukmodak/soapServer','uri'=>'urn://localhost/ukmodak/soapServer');
        $this->soapInstance = new \SoapServer(null,$params);
        
        $this->soapInstance->setClass('\App\Http\Controllers\Web\SoapController');
        $this->soapInstance->handle();
        //print_r($this->soapInstance);die;
    }
}

SOAP - Client Side Script:

file url:http://localhost/soapclient/client.php

<?php
 
$client = new SoapClient(null, array(
	'location' => "http://localhost/ukmodak/soapServer",
	'uri'=> "urn://localhost/ukmodak/soapServer",
	'trace'=> 1
	)
);

//print_r($client);die;

?>

file url:http://localhost/soapclient/list.php

<?php

<?php 
include __dir__.'/client.php';
$requestIns = json_decode($client->__soapCall('getGallaryAsJson',array('limit'=>2)));
 //print_r($requestIns);die;
 


?>
   
     <html>
     <title>upload</title>
     <head >
	 <link href="bootstrap-3.3.7/dist/css/bootstrap.css" rel="stylesheet" type="text/css">
     <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
		


<script>
 function previewFile() {
  var preview = document.querySelector('img');
  var file    = document.querySelector('input[type=file]').files[0];
 
  var reader  = new FileReader();

  reader.addEventListener("load", function () {
    preview.src = reader.result;
  }, false);

  if (file) {
    var dd = reader.readAsDataURL(file);
  }
}


</script>

<script>
  jQuery(function(){
	
	jQuery('#ukm').change( function(event) {
		 var tmppath = URL.createObjectURL(event.target.files[0]);
		    
			jQuery("img").fadeIn("fast").attr('src',tmppath);
			
			alert(json.stringfy(event.target.files[0]));
		
	});
	
  });  
</script>

</head>	
<body>
	 
<div class="panel panel-success">
              <div class="panel-heading">New Gallery</div>
              
              <div class="panel-body">
                  

 
<?php
 
 if( $requestIns){

?>
 <table class="table">
  <caption> By Built-in SoapClient</caption>
   <tr>
	    <th>Title</th>
		<th>Image</th>
		<th>action</th>
    </tr>
<?php

  
  foreach($requestIns as  $requestI){
  foreach($requestI as  $resp){
    echo'<tr>';
	  echo'<td>'.$resp->title.'</td>';
      echo'<td><img style="width:100px;" src="'.$resp->thumb_path.'"></td>';
	  $edit ="http://localhost/soapclient/edit.php?id=".$resp->id."";
	  $delete ="http://localhost/soapclient/insertUpdate.php?delete=d&id=".$resp->id."";
	  $add ="http://localhost/soapclient/upload.php";
	  echo"<td><a class='btn btn-sm btn-primary' href=".$edit.">edit</a>";
	  echo"<a class='btn btn-sm btn-warning' href=".$delete.">delete</a>";
	  echo"<a class='btn btn-sm btn-warning' href=".$add.">Add</a></td>";
    echo'</tr>';
     }	
  }

 echo'<table>';
 }
 
?>

                  
                  
			    </div>
			</div>
		</body>
</html>	



file url:http://localhost/soapclient/edit.php

<?php
include __dir__.'/client.php';
$galleryIns = null;
?>  
   <html>
    <title>upload</title>
    <head>
	    <link href="bootstrap-3.3.7/dist/css/bootstrap.css" rel="stylesheet" type="text/css" >
		  <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
		


<script>
 function previewFile() {
  var preview = document.querySelector('img');
  var file    = document.querySelector('input[type=file]').files[0];
 
  var reader  = new FileReader();

  reader.addEventListener("load", function () {
    preview.src = reader.result;
  }, false);

  if (file) {
    var dd = reader.readAsDataURL(file);
	getFilePath();
  }
}

function getFilePath(){
         var file    = document.querySelector('input[type=file]').files[0];
         var filePath=jQuery('#ukm').val(); 
		  var path = (window.URL || window.webkitURL).createObjectURL(file);
		  const reader = new FileReader();
		 alert(reader.readAsText(path));
    
}
</script>

<script>
  jQuery(function(){
	
	jQuery('#ukm').change( function(event) {
		 var tmppath = URL.createObjectURL(event.target.files[0]);
		    
			jQuery("img").fadeIn("fast").attr('src',tmppath);
			
			alert(json.stringfy(event.target.files[0]));
		
	});
	
  });  
 </script>

</head>	
<body>
	
<?php

   $id = $_GET['id'];
  
    $galleryIns = json_decode($client->__soapCall('editGallaryAsJson',array('id'=>$id)));
	
 if($galleryIns){

?> 
	 <div class="panel panel-success">
              <div class="panel-heading">Update Gallery</div>

              <div class="panel-body">
                  
                        <form class="form-horizontal" role="form" method="POST" action="http://localhost/soapclient/insertUpdate.php" enctype="multipart/form-data">     
                        <div class="form-group col-md-4">
                                                    
							<label>Web Section</label>
							<div>
							  <select name="web_section_id" class="form-control" value="web_section_id; ?>">
							  <option value="39">Image Gallery</option>
							  </select>
							</div>
						</div>
                                                
                     <div class="form-group col-md-4">
                                                    
							<label>Web Category</label>
							<div>
							
							 <select name="web_category_id" class="form-control" value="web_category_id;?>">
							    <option value="20">Image Gallery</option>
							</select>
	
							</div>
						</div>
                                                
                                 <div class="col-md-12 col-lg-12 cls">
		                               <div class="form-group col-md-4">
		                                    <label>Upload File</label>
				                           <div>
										    <input type="file" name="fup_ukmFile" id="ukm"  accept=".gif,.jpg,.jpeg,.png" onchange="previewFile()">
                                           
                                              <img src="" height="200" alt="Image preview...">

				                           </div>
                                        </div>
            
						                <div class="form-group col-md-4">
						                         <label>Title</label>
						                         <div>
						                         <input type="hidden" name="id" value="id; ?>"/>
												 
						                         <input type="text" pattern="^[A-Za-z_-\\]{5,50}" class="form-control" id="fup_title" name="fup_title" value="title; ?>">

						                         </div>
						                 </div>

                  
						                 <div class="form-group col-md-4">
						                         <label>Is Publish</label>
						                         <div>
						                                
														  <select name="fup_active" class="form-control" value="active; ?>">
							                                <option value="1">Publish</option>
															<option value="0">Unpublish</option>
							                              </select>
						                         </div>
						                 </div>

                                 </div>
                           
                                <div class="form-group">
                                        <div class="col-md-12" align="center">
										       <input type="hidden" name="update" value="update"/>
											   
                                                <button type="submit" class="btn btn-primary">
                                                        Submit
                                                </button>
                                        </div>
                                </div>
                        </form>

                  
                  
			    </div>
			</div>
		</body>
		
<?php
  }
?>
</html>	

file url:http://localhost/soapclient/insertUpdate.php


<?php 
include __dir__.'/client.php';
$requestIns = null;

 if(@$_POST['update']=='update'){
  
		    $data = array(
			         'id'=>$_POST['id'],
			         'web_category_id'=>$_POST['web_category_id'],
					 'web_section_id'=>$_POST['web_section_id'],
					 'fup_ukmFile'=>$_FILES['fup_ukmFile']["tmp_name"],
					 'tmp_path'=>$_FILES['fup_ukmFile']["tmp_name"],
					 'file_name'=>$_FILES['fup_ukmFile']["name"],
					 'type'=>$_FILES['fup_ukmFile']["type"],
					 'size'=>$_FILES['fup_ukmFile']["size"],
					 'fup_title'=>$_POST['fup_title'],
					 'fup_active'=>$_POST['fup_active']
			);
              
				 if(json_decode($client->__soapCall('postUpdateGallary',[$data]))){
					return header("Location: http://localhost/soapclient/list.php", true, 301);
				 }else{
					header("Location: http://localhost/soapclient/edit.php?id=".$_POST['id']."", true, 301);
				 }
				 
				 

  }else if($_POST['insert']=='insert'){

			   $data = array(
				         'web_category_id'=>$_POST['web_category_id'],
						 'web_section_id'=>$_POST['web_section_id'],
						 'fup_ukmFile'=>$_FILES['fup_ukmFile']["tmp_name"],
						 'tmp_path'=>$_FILES['fup_ukmFile']["tmp_name"],
						 'file_name'=>$_FILES['fup_ukmFile']["name"],
						 'type'=>$_FILES['fup_ukmFile']["type"],
						 'size'=>$_FILES['fup_ukmFile']["size"],
						 'fup_title'=>$_POST['fup_title'],
						 'fup_active'=>$_POST['fup_active']
				);
				  
				  
				 if(json_decode($client->__soapCall('inImage',[$data]))){
					return header("Location: http://localhost/soapclient/list.php", true, 301);
				 }else{
					header("Location: http://localhost/soapclient/upload.php", true, 301);
				 }
  
  
  }else if($_GET['delete']=='d'){

				 if(json_decode($client->__soapCall('destroy',array('id'=>$_GET['id'])))){
					return header("Location: http://localhost/soapclient/list.php", true, 301);
				 }else{
				   return header("Location: http://localhost/soapclient/list.php", true, 301);
				 }
  }
  
  


 
  
  ?>

file url:http://localhost/soapclient/upload.php


   <html>
    <title>upload</title>
    <head>
	    <link href="bootstrap-3.3.7/dist/css/bootstrap.css" rel="stylesheet" type="text/css">
		  <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
		


<script>
 function previewFile() {
  var preview = document.querySelector('img');
  var file    = document.querySelector('input[type=file]').files[0];
 
  var reader  = new FileReader();

  reader.addEventListener("load", function () {
    preview.src = reader.result;
  }, false);

  if (file) {
    var dd = reader.readAsDataURL(file);
  }
}

</script>

<script>
  jQuery(function(){
	
	jQuery('#ukm').change( function(event) {
		 var tmppath = URL.createObjectURL(event.target.files[0]);
		    
			jQuery("img").fadeIn("fast").attr('src',tmppath);
			
			alert(json.stringfy(event.target.files[0]));
		
	});
	
  });  
  </script>	

	</head>	
	<body>
	 
	 <div class="panel panel-success">
              <div class="panel-heading">New Gallery</div>
              
              <div class="panel-body">
                  
                        <form class="form-horizontal" role="form" method="POST" action="http://localhost/soapclient/insertUpdate.php" enctype="multipart/form-data">      
                        <div class="form-group col-md-4">
                                                    
							<label>Web Section</label>
							<div>
							  <select name="web_section_id" class="form-control" value="">
							  <option value="39">Image Gallery</option>
							  </select>
							</div>
						</div>
                                                
                     <div class="form-group col-md-4">
                                                    
							<label>Web Category</label>
							<div>
							
							 <select name="web_category_id" class="form-control" value="">
							    <option value="20">Image Gallery</option>
							</select>
	
							</div>
						</div>
                                                
                                 <div class="col-md-12 col-lg-12 cls">
		                               <div class="form-group col-md-4">
		                                    <label>Upload File</label>
				                           <div>
										    <input type="file" name="fup_ukmFile" id="ukm"  accept=".gif,.jpg,.jpeg,.png" onchange="previewFile()">
                                           
                                              <img src="" height="200" alt="Image preview...">

				                           </div>
                                        </div>
            
						                <div class="form-group col-md-4">
						                         <label>Title</label>
						                         <div>
						                         <input type="text" pattern="^[A-Za-z_-\\]{5,50}" class="form-control" id="fup_title" name="fup_title" value="">
						                         </div>
						                 </div>

                  
						                 <div class="form-group col-md-4">
						                         <label>Is Publish</label>
						                         <div>
						                                
														  <select name="fup_active" class="form-control" value="">
							                                <option value="1">Publish</option>
															<option value="0">Unpublish</option>
							                              </select>
						                         </div>
						                 </div>

                                 </div>
                           
                                <div class="form-group">
                                        <div class="col-md-12" align="center">
										       <input type="hidden" name="insert" value="insert"/>
                                                <button type="submit" class="btn btn-primary">
                                                        Submit
                                                </button>
                                        </div>
                                </div>
                        </form>

                  
                  
			    </div>
			</div>
		</body>
</html>	
  
bONEandALL