Debugging Web Services

Debugging normal  PHP web applications is pretty straight forward. I use Eclipse but almost all IDEs have some means of setting break points and seeing the value of variables. I know some people put print or log statements in their code but this is much more time consuming than just setting a breakpoint and looking at the variables there, and maybe steping forward to see how the script runs.

I would be interested to hear if others usually use IDEs like Eclipse and debug in this way. I would hate to work without it. I even use it to check a newly written script or one that I have taken over responsibility for. Stepping through the processing can help you spot problems very easily.

Now all that is ok for a simple script or web application, but what about the server end of a web service? Normally the server runs in response to a client call. The client request may be quite complex, so how do you debug the server side?

Well my answer is to log the client request as it is received by the server as follows. Add this code to the very top of your server entry point, then have the client make its request. The server will log the request then carry on to produce a result (or not if there is some bug you want to fix).

To debug the server side, replace the above code with the following.

That’s it!. Now you can debug the server side without needing a client to make a request. The server entry point thinks it is getting a request from a client, but really it is just coming from the log file.

Now all this might seem trivial or obvious, but it took me a while to work this out when I had my first web service server to debug. Suddenly I’m just debugging a stand alone script rather than a server than depends on an external client request.

I would be very interested to see if this is useful to anyone.

 

 

 

 

Creating Custom Web Services In SugarCRM

I have been using web services to access Sugar for sometime now, for purposes of integration and for accessing data from a portal. While it is reasonably well documented, there are restrictions, and last week I cam across a requirement I just couldn’t meet with the standard web services methods. It was time to explore custom web services.

When I did, I suddenly realised that it was very simple to implement, and allowed me to push much of the business logic back into the server where it belongs. I could create a new method that had all sorts of complex logic,and expose the results via web services.

Now I did think that this might restrict the ability of the client software; any time it needs to access a Sugar instance, that instance needs the custom web service installed. By zipping up the custom web service as a loadable module (a topic I’ll discuss in the future as it is a very easy and powerful way to deploy any sort of customisation) this objection was quickly overcome. In any cases I would normally have access to the server when doing this type of work.

So, I have included a trivial method here, just to prove that I can. I think you will see that just about anything can go into such a method, allowing the web service to provide complex functionality to its client.

Before tackling that, lets see how the standard web service works. We will look at the version 4.1REST option from Sugar PRO 6.5.13.

service/v4_1/rest.php

You can try this in a browser directly, as follows (your url will be slightly different)

http://localhost/sugarPro6.5.13/service/v4_1/rest.php

This will show you documentation of the standard Sugar methods you can call, and their parameters.

Custom Web Service

Now our custom web service is to reside at custom/service so that it its upgrade safe. Lets look at the code, and compare it with the standard Sugar version above.

You will see we just extend the standard classes and create our own. These are below (MySugarRestService.php and MySugarRestServiceImp.php, though we could do without the first)

and MySugarRestServiceImpl.php below. You can see how it extends the Sugar class and adds a new method (getContactDetails).

Note that the method just returns hard coded values.

Accessing The Web Service

To prove that it works you need some a client. The following is very basic code to act as a client calling our new method. In a later post I will show more examples of client code, but for now this is enough to get started.

When you run that client you get the following response.

Which are just the hard coded value set in our server method.

I think that is enough to get started. I haven’t shown the login process, or how values can be retrieved from Sugar. All will have to wait for a new post.

Please contact me if you have any questions.

 

 

Strange PHP

This is post I thought I would start as I come across weird PHP code from time to time and I thought I would like to record it. When we are new at a language we struggle to find ways to achieve results and sometimes this leads to some quirky code. I’m sure others could find examples in my code.

1. Iterating though an array

instead of

 

 

Posted in PHP

Quality of Coding

Sometimes I think I’m on my own in believing that code should be tidy and consistent, and generally easy for other programmers to pick up and work with. I come across so much code that does the job it was intended to do but is very untidy and has only the barest of documentation.

There are lots of practical reasons for writing quality code but I would like to leave those for now and mention something more personal.

In Zend and The Art of Motorcycle Maintenance by Robert Persig, the concept of quality for its own sake is introduced. Early in the book he mentions one example from his own experience. It has always always stuck in my mind for some reason, and have nothing to do with programming.

Robert had a workshop with a big draw full of heavy tools. Every time he had to get a tool he had the dragging the draw out, which always involved lots of physical effort. One day it got him down and he decided to do something about it.

He went off and bought roller bearings and fitted them to the draw. From that time on he could open and close the draw with a single figure. It gave him great joy every time he used it.

Now the joy didn’t come from the saving of all that physical effort; it came from the pure quality he had built in. Every time he used the draw he felt good about it and himself.

He goes on to talk about how we can all build quality into everything we do. He mentioned how even very mundane jobs can be turned into something really fulfilling by concentrating on doing the job as well as it can possibly be done. In other words building in quality and getting a good feeling from it.

The best way to build in quality is to write it that way from the start. Make sure every line of code is something you are proud of. If you think you will come back later and tidy it up, if you are like me  it probably won’t happen.

 

Posted in PHP

Developing with SugarCRM

I’ve been working with SugarCRM for 18 months, and found that there isn’t a lot of online material to help you get started. There are plenty of code snippets but not many full examples you can get working quickly, and that’s what I’m always looking for.

So what I will be doing is adding examples, with enough code and explanation to get something working quickly.

I use SugarCRM Professional, and this changes from time to time. With each example I’ll indicate which version the examples relate to.

Development for Sugar falls into a number of categories as follows. I will be ignoring everything related to using Studio and Module Builder within Sugar as these don’t involve any coding and in any case are well covered elsewhere.

  • Logic Hooks
  • Custom code
  • Custom modules
  • Integration