Tuesday 12 July 2011

PHP: How to send a POST request with parameters

 

The following piece of PHP code shows how to send a POST request to a website passing some requests parameters. It may be useful if you needed to process the page that is normally requested using POST method e.g. form submission result page.

The request is similar to what your browser would send if you populated a form using POST method on a webpage.
// Create map with request parameters
$params = array ('surname' => 'Filip', 'lastname' => 'Czaja');

// Build Http query using params
$query = http_build_query ($params);

// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );

// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));

// Read page rendered as result of your POST request
$result = file_get_contents (
'http://www.sample-post-page.com', // page url
false,
$context);

// Server response is now stored in $result variable so you can process it

Monday 11 July 2011

Error starting Tc server in STS 2.7

So I started learning Spring MVC by example using the Spring MVC Showcase. I downloaded STS and cloned the GIT repo to get the local copy of the code. I loaded the Maven project and built it successfully.

When I tried to start the VMware vFabric tc Server Developer Edition 2.5 I got the following exception:
Publishing the configuration...
Error copying file to C:/Program Files/springsource/vfabric-tc-server-developer-2.5.0.RELEASE/spring-insight-instance/backup\catalina.policy: C:\Program Files\springsource\vfabric-tc-server-developer-2.5.0.RELEASE\spring-insight-instance\conf\catalina.policy (The system cannot find the path specified)
C:\Program Files\springsource\vfabric-tc-server-developer-2.5.0.RELEASE\spring-insight-instance\conf\catalina.policy (The system cannot find the path specified)
Error copying file to C:/Program Files/springsource/vfabric-tc-server-developer-2.5.0.RELEASE/spring-insight-instance/backup\catalina.properties: C:\Program Files\springsource\vfabric-tc-server-developer-2.5.0.RELEASE\spring-insight-instance\conf\catalina.properties (The system cannot find the path specified)
C:\Program Files\springsource\vfabric-tc-server-developer-2.5.0.RELEASE\spring-insight-instance\conf\catalina.properties (The system cannot find the path specified)
Error copying file to C:/Program Files/springsource/vfabric-tc-server-developer-2.5.0.RELEASE/spring-insight-instance/backup\context.xml: C:\Program Files\springsource\vfabric-tc-server-developer-2.5.0.RELEASE\spring-insight-instance\conf\context.xml (The system cannot find the path specified)
C:\Program Files\springsource\vfabric-tc-server-developer-2.5.0.RELEASE\spring-insight-instance\conf\context.xml (The system cannot find the path specified)
Error copying file to C:/Program Files/springsource/vfabric-tc-server-developer-2.5.0.RELEASE/spring-insight-instance/backup\jmxremote.access: C:\Program Files\springsource\vfabric-tc-server-developer-2.5.0.RELEASE\spring-insight-instance\conf\jmxremote.access (The system cannot find the path specified)

(...)

Solution

I'm running 64-bit version of STS on Windows 7. By default programs don't use the Admin account. It was enough to run the STS as Admin (Right click on shortcut -> "Run as Administrator").
That's it! Simple, isn't it? :)

 

Getting started with Spring MVC and Hibernate

Soon I'll be joining a new project using mainly Spring MVC + Hibernate. Since I've never used those technologies and know only their general purpose I need to do some reading.

Here are the links that were recommended to me:Would you recommend any others?

Thursday 7 July 2011

CodeIgniter: Resetting form validation

 

In one of my php projects I'm using CodeIgniter and its Form Validation library. I have validation rules defined in a config file located at:

system\application\config\form_validation.php

Sample rules definition for action "item/add" could look as follows:
$config = array(
'item/add' => array(
array(
'field' => 'name',
'label' => 'lang:name',
'rules' => 'trim|xss_clean|required|max_length[50]'
),
array(
'field' => 'type',
'label' => 'lang:type',
'rules' => 'trim|xss_clean|required|callback_type_check'
)
),
(...)
As you can see I'm using both built-int and custom rules (callback_type_check).

This works fine with my 'Add Item' form.

However, I wanted to reuse the validation logic at other place, where the user can provide multiple items to add at once in a file where each row represents a single item. So I read line by line from the file and want to validate each line. To do that I reset values in $_POST array and perform validation:
$_POST["name"] = $nameReadFromFile;
$_POST["type"] = $typeReadFromFile;
if ($this->form_validation->run('item/add') == FALSE) {
// handle validation error for current item
}
The problem is that when validation fails for one item then the error is persisted and all following invocations will fail as well, even if items are valid.

So, I added a reset function to my controller that resets Form Validation library:
function _reset_validation()
{
// Store current rules
$rules = $this->form_validation->_config_rules;

// Create new validation object
$this->form_validation = new CI_Form_validation();

// Reset rules
$this->form_validation->_config_rules = $rules;
}
The function simply remembers the rules that were loaded from config file when validation object was created, creates a new validation object and resets the rules. I call it after each row is validated.