tech notes

June 27, 2008

symfony object_select_tag options

Filed under: php — admin @ 5:13 pm

I couldn’t find on symfony’s website what options you can pass to the object_select_tag function. It’s definition looks like this:

function object_select_tag($object, $method, $options = array(), $default_value = null)

and options can be:

  • peer_method - peer method you want to use to fetch options. useful for sorting, limiting output etc.
  • text_method - method used to display labels for your options
  • include_custom - your customized option
  • include_title - creates a title for the whole selection based on the name of the called method
  • include_blank - adds blank (”") option to your select tag

June 4, 2008

creating web services with wso2 wsf/php

Filed under: php — admin @ 4:48 pm

I’m not sure if it’s the best way of doing it, but this method of creating web services with wso2 works for me. A bonus here is that it doesn’t involve creating or modyfying any XML/WSDL files as all this is autogenerated or handled by the framework itself.

This example service will return N random digits from a given range.

wsdl autogeneration

wso2 has a nice feature of generating wsdl files based on service configuration. It can do even better than that - it can generate a wsdl file for your service based on the comments you use for functions that describe your service. Of course these comments have to be in a special format, but this format is nothing else than dotproject-style comments that can be used to automatically document your code. Here is an example, it doesn’t implement anything yet, it’s only used to generate a wsdl definition. To avoid URL changes in the service location specified in the generated wsdl I’m creating it in random/index.php in my web server’s root directory, which will be replaced by the service implementation later on:

<?php
/**
* @namespace http://server.com/random
*/

class RandomNumber
{
/**
* @var integer $number
*/

public $number;
}

/**
* @param integer $num number of random digits
* @param integer $min minimum random number
* @param integer $max maximum random number
* @return array of object RandomNumber $numbers array of random numbers generated
*/

function getRandom($num, $min, $max)
{
return new RandomNumber();
}

$service = new WSService(array("operations" => array("getRandom")));
$service->reply();
?>

When you access http://server.com/random/?wsdl you’ll get your wsdl file generated for the service documented above. Save it in your services directory as random.wsdl.

service and client autogeneration

Now we can use this file to generate both a service template and a sample client template.

In wso2-php package there is a file called wsdl2php.php. Use it to generate both templates:

> php wsdl2php.php -s random.wsdl > server.php
> php wsdl2php.php random.wsdl > client.php

you can keep a copy of your index.php file used to generate the wsdl file and replace it with the generated server.php

> cp index.php generate.php; mv server.php index.php

service implementation

At this point you can implement your service, edit your index.php file, read the comments there, and add this simple implementation to the getRandom function:

$response = new getRandomResponse();
for ($i = 1; $i >= $input->num; $i++) {
  $n = new RandomNumber();
  $n->number = rand($input->min, $input->max);
  $response->numbers[] = $n;
}
return $response;

implementation of a sample client

and now modify your client to test it, under //TODO fill in the class fields of $input to match your business logic add:

$input->num = 10;
$input->min = 1;
$input->max = 100;

and implement the business logic to consume your response:

foreach ($response->numbers as $n) {
  printf("%d", $n->number);
}

Start client.php in your browser - you should be able to see 10 randomly generated numbers ;)

Powered by WordPress