When working against complex soap servers from PHP it’s generally a nice thing to be able to act on exceptions and not just provide the error message. I’ve been working against X-Fire web services for the last few months, and every time an exception is thrown it’s returned as a SoapFault with the actual exception name as property in the detail field. I wanted proper exceptions which I could do something useful with, so I poked around and came up with something which fixes this. The basic idea is to catch the soap fault exception, parse it for information and build a new exception based on the name which is retrieved. The code is a bit gritty since the actual name of the exception is a field, but that’s nothing that reflection can’t solve. The doRequest method is the method used to call the web service methods. This is a part of my soap client class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Do the soap request
*
*/
protected function doRequest($name, $args = array()) {
  try {
    $response = $this->__soapCall($name, $args, $this->options);
    return $response;
  }
  catch(Exception $e) {
    $newEx = $this->buildException($e);
    throw $newEx;
  }
}
 
 
/**
* parses a soap fault and returns the error code and message
* used by buildException
*
* @param SoapFault $e
* @return array
*/
protected function parseSoapFault(SoapFault $e) {
  $pos = strpos($e->faultstring, ':');
  $code = "";
  if($pos) {
    $code = substr($e->faultstring, 0, $pos);
    $message = trim(substr($e->faultstring, $pos+1, strlen($e->faultstring)));
  } else {
    $message = $e->faultstring;
  }
  return array($code, $message);
}
 
 
/**
* This method takes a SoapFault and builds a proper exception from it
*
* The SoapFault can hold a server side exception name in it's detail field. This
* method takes a peek to see if that field exists, and if it does the name of the
* field is extracted via reflection. A new exception is created of the class that
* was just retrieved and the message and code is set.
*
* The message and error code is extracted from the soap fault.
* @param unknown_type $e
* @return unknown
*/
protected function buildException($e){
  list($code, $message) = $this->parseSoapFault($e);
  if (!isset($e->detail)){ // No exception found
    $ex = new GenericException($message,$code);
    return $ex;
  }
 
  // get the actual name of the exception
  $reflectionObject = new ReflectionObject($e->detail);
  $properties = $reflectionObject->getProperties();
  $exceptionName = $properties[0]->name;
 
  $exception = new $exceptionName($message,$code);
  return $exception;
}
Aug 112007

When I first started using PHP on a larger scale I didn’t have any experience with frameworks, so I started creating some minor helpers which made my day easier. After my first project I came across Prado. Since I had been using .net previously I grasped the concepts really quickly, so it was love at first sight. It took around a week to get fully productive with Prado, and it’s saved me countless hours of nitty gritty work. Since I first started using Prado I’ve found the forums to be the best source for information and support when I can’t find help in the documentation. The community around Prado isn’t that big (yet), but there are quite a few people which are very active. At first I can be a bit hard to navigate around the available resources, so I’ve composed a list of links which is worth checking out:

A few nice-to-know links and blogs related to Prado:

Planet Prado
Pulls data from most of the major Prado related bloggers. Nice place to stay up to date on what’s going on in the Prado world.

Prado Roadmap
The trac-based road map for the Prado project. Shows the current milestones and planned releases, with information about which features to expect, which bugs are reported and so on.

Prado Blog Tutorial
A step-by-step tutorial on how to build a blog using Prado. Nice place for beginners wanting to learn Prado

Prado Quickstart Tutorial
The term tutorial isn’t quite fitting, since it’s more of a reference. The Quickstart is a nice reference for control and component usage in addition to documenting severa key features and concepts in the Prado framework.

Prado Component Repository
Trac set up for user-submitted components and controls. This project is still in the early stages, but worth keeping an eye on.

Prado Community Project
The Prado Community Project is a new project created by the most active users of Prado. The goal of this project is to ensure that Prado gets a growth in both users and material. There are several projects planned which looks very interesting. If you have some time to offer, please stop by the forums and help us out.

There has been lots of interest in the AJAX (active controls) features in Prado lately, but the lack of client side controls has been a turn-off for many. The area that’s gotten most request is drag / drop. Prado 2 had controls for this, but it’s been absent in Prado 3. Several users on the forums has been trying to get it working, but nothing has ever been completed. The main issue has been the communication between the client side scripts and the server side control (callbacks). I’ve spent some time digging trough the Prado code and created a concept control which fires a callback.

Code:

MyDraggable.js code

MyDraggable.php

Home.php

Home.page

Stay tuned for Part II which hopefully shows something more useful :)