Changing the SOAP end point URL in PHP

The endpoint URL is where the web service can be accessed by a client application. The same web service can have multiple endpoints.

The SoapClient of your PHP, uses the endpoint URL from the WSDL file by default.
It looks like this:

Our application faced an issue where the endpoint got changed and we were getting: “Could not connect to host error.”

To fix that, we changed the endpoint from our php application by adding this:

$client = new soapclient($soapWSDLURL);
$client->__setLocation(‘newEndpointUrl’);

which is the same as :

$client = new soapclient($soapWSDLURL, array(‘location’ => ‘newEndpointUrl’));

Hope that was helpful!
Thanks,
Pallavi

Creating a custom search option in jqGrid : jQuery

Here’s a way to use custom search in the jQgrid.
I have to search data which is not a part of the result grid.
Hence I have created a separate form to get the search values and post the data using jqGrid to get the result set.

var emp = {
initGrid: function(){
var grid_selector = “#grid-table”;
var pager_selector = “#grid-pager”;

jQuery(grid_selector).jqGrid({
url: “url/to/loaddata”,
datatype: “json”,
mtype: “POST”,
colNames: [“First Name”, “Last Name”, “ID”, “Sex”, “DOB”, “Address”, “Score”],
colModel: [
{ name: “firstname”, width: ’50px’},
{ name: “lastname”, width: ’50px’},
{ name: “ID”, width: ’40px’},
{ name: “sex”, width: ’30px’},
{ name: “birthdate”, width: ’30px’},
{ name: “address”, width: ’60px’},
{ name: “score”, width: ’30px’}
],
height: ‘auto’,
viewrecords : true,
rowNum:10,
rowList:[10,20,30],
pager : pager_selector,
altRows: true,
sortname: ‘firstname’,
sortorder: “asc”,
beforeRequest: function(){
//show custom loader before posting data
commonfn.showPageLoader();
},
loadComplete : function(){
// hide the loader after grid is loaded
commonfn.hidePageLoader();
},
postData:{
‘fname’: function () { return $(“#fname”).val(); },
‘lname’: function () { return $(“#lname”).val(); },
‘id’: function () { return $(“#id”).val(); },
‘score’: function () { return $(“#score”).val(); },
‘oper’: function () { return $(“#oper”).val(); },
‘searchId’: function () { return $(“#searchId”).val(); }
},
caption: “”,
autowidth: true,
loadui: ‘disable’
});
},
searchGrid: function(){
// reload the table with search data
if ($.trim($(“#fname”).val()) == ” && $.trim($(“#lname”).val()) == ” &&

$.trim($(“#id”).val()) == ” && $.trim($(“#score”).val()) == ” &&

$(‘#searchId’).val() == ” ){
commonFn.showNotification(‘Please select atleast one search criteria’);
}else{
$(“#grid-table”).trigger(“reloadGrid”, { page: 1 });
}
},
}
var emp = {
emp.initGrid();
}

Here’s a brief description of what the code does:

initGrid initialises the grid with:

url: ‘url/to/loaddata’ This is a url that makes the db query and returns the result set

postData: This is the most important part.
This specifies what data will be posted when we reload the grid.
So here, we get the values of the search fields and pass them.
Then in the server, make the query with those values and return the result set

searchGrid: This is called on clicking the search button or clear button.
It triggers the “reloadGrid” method of jQuery which loads the grid again.

Hope this post was helpful for you!

Making SOAP Calls in Laravel4

In order to make SOAP calls from laravel4 framework, you just need to enable the soap extension in your php.ini file.
And make sure that the soap client and soap server modules are enabled in your phpinfo();
Thats it ! You can make SOAP calls from your Laravel application. Below is the code snippet I used –

$soapWSDL = Config::get(‘filename.soapWSDLPath’);
$client = new soapclient($soapWSDL);

$res = new StdClass();

// Call the SOAP method
$params = array(‘user’ => $user,
‘password’ => $password,
‘requestcontent’ => $contents);

$result = $client->SendRequest($params);

$response = $result->SendRequestResult->Response;

Laravel4: Exporting Large Volumes of data to Excel

After a lot of head banging, I reached at the optimum solution for exporting large volumes (upto 40k rows) of data to excel. Below is the code that can do wonders for you!

Requirements:
1. Install the Maatwebsite Laravel bundle ( This has the PHPExcel library which we will be using as it is faster than the bundle’s wrapper classes) :
https://github.com/Maatwebsite/Laravel-Excel

2. A basic understanding of the Laravel4 framework

public function getExportToExcel(){

$emp = new Employee(); // Create the model class object
$empList = $emp->getAllEmployees(array()); // get all the data to be //exported

// set php's memory limit from 128(default) to 500MB.
// Without this, it gives an error that file does not exist/ blank page

ini_set('memory_limit', '500M');

//Prevent your script from timing out
// This increases the excution time from 30 secs to 3000 secs.
set_time_limit ( 3000 );

$objPHPExcel = new PHPExcel();
$i = 1;

//looping through the result and writing each row onto the excel sheet
for ($i = 0; $i setActiveSheetIndex(0)
->setCellValue("A$i", $empList[$i][0])
->setCellValue("B$i", $empList[$i][1])
->setCellValue("C$i", $empList[$i][2])
->setCellValue("D$i", $empList[$i][3])
->setCellValue("E$i", $empList[$i][4])
->setCellValue("F$i", $empList[$i][5])
->setCellValue("G$i", $empList[$i][6])
->setCellValue("H$i", $empList[$i][7])
->setCellValue("I$i", $empList[$i][8])
->setCellValue("J$i", $empList[$i][9])
->setCellValue("K$i", $empList[$i][10])
->setCellValue("L$i", $empList[$i][11]);
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');
//echo memory_get_usage() . "\n"; -- Echo the memory used at this point
//echo memory_get_peak_usage() . "\n"; -- Echo the maximum memory used

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="result.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); // To make the file downloadable via a popup in your browser
}

It took me around:

51secs, 245 MB of memory to export 15k rows and 12 columns.

65secs , 482MB to export 30k rows and 12 columns

Also, using the PHPExcel classes saves around 1-2% of time than using the maatwebsite bundle’s wrapper classes. So, I chose to that. However, if you may wish to use the bundle’s classes, here’s the code for you.

public function getExportToCsv(){
$excel = App::make('excel');
$emp = new Employee();
$empList = $emp->getAllEmployees(array());

set_time_limit ( 500 );

ini_set('memory_limit', '500M');

//using cache saves quite some time.
//Make sure the cache memory is within the php memory_limit

$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize' => '256M');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

Excel::create('Filename', function($excel) use($empList) {
$excel->sheet('Employees-1', function($sheet) use($empList){
$sheet->fromArray($empList);
});

})->export('csv');
}