Posted by stephen
on July 28, 2010
Ideas and Sorts,
PHP /
No Comments
As I have been going through different code bases, I keep seeing things that just really amaze me. It all surrounds coding standards, or the lack thereof. Different places have different ideas of what is best, and I can only offer mine to the fray. However, once a standard is in place, no matter how odd it may be, it is important to keep to those standards. This is for a few reasons.
First, it is important for readability. By seeing the code in the same format and structure it helps to get through lines of code quickly. It helps because all of the code is in the same format throughout the code. The person looking at the code can quickly understand without having to go back and figure out what is happening because the code is all of a sudden out of whack and what you may think is happening may not be happening. Especially when indentation may also be out of line and brackets are not used. Take for example the two examples below
$fin=0;
foreach($var as $v){
if($v==3){$fin=$v+3}
unset($fin);echo $v;
}
As a stand alone example it may be easy to decipher what is happening. In a file that has over 3000 lines of code, it may be overlooked and even cause issues if changes are made that affect that block. By breaking from the set coding standards and doing it “your own way”, sure it may be quicker for you, but it creates havoc, even for you later on.
Continue reading…
Posted by stephen
on July 26, 2010
PHP /
No Comments
I am going to take a break from the NuSOAP server finish up, and that will come soon. Instead, the update to PHP 5.2.14 has come out. What is really odd about this news, is that according to the release of the news, it also
“marks the end of the active support for PHP 5.2. Following this release the PHP 5.2 series will receive no further active bug maintenance. Security fixes for PHP 5.2 might be published on a case by cases basis. All users of PHP 5.2 are encouraged to upgrade to PHP 5.3.”
There are a lot of different applications that are just now adopting 5.2, and it will not be actively supported.
Continue reading…
Tags: PHP
Posted by stephen
on July 20, 2010
Applications,
PHP /
No Comments
In the last post, the NuSOAP server introduced a new level of complexity, and had a multidimensional array, along with creating a more robust status message for the client to help determine the success or failure of the request. This post will examine the remaining functions in the NuSOAP server, and making the rest of the functions work, and more complex types including third and fourth level complexities along with a non-named (integer) array for the result.
We will be building on the code we used last time, so as a refresher, the code from the last post can be located at:
http://www.hirdweb.com/webservice/20100710_server.txt
http://www.hirdweb.com/webservice/20100710.txt
First off, here are the remaining functions to be exposed in the service:
- showGroupItems – Shows what is needed based on a set event, all items are made up of course
- showMadLib – Returns a mad lib based on passing in a number of different items
- showNumbers – Returns a non associative array of numbers, requires an ID, and returns the numbers associated with that ID
Each of these webservices will require a little different structure.
showGroupItems will have a third level of complexity
showMadLib will require a long list of strings as the input
showNumbers will return an array of string elements, integer value for the element, instead of a named element
Continue reading…
Tags: NuSOAP, PHP, webservices
Posted by stephen
on July 10, 2010
Applications /
1 Comment
The NuSOAP server consists of a function that will take an ID and a name/phrase and inserts that into the Shakespeare quote. The function returns a semi-complex type which consists of 2 array elements, a status message and the phrase. However, the webservice needs to expand, add a new function, and make error messages dynamic. So the first thing we will tackle is getting the status message into a more dynamic format.
First, plan it out, always plan it out. The status message will need to accept an “error code”, a string for the struct name, and a variable for a custom message if it does fit in within the specified error codes. Now, just as a disclaimer here, this is just an example of how it can be planned. Some services may not need this extent, some may need a lot more. Remember that it needs to be able to handle the requirements of your application.
With that planned out, here is a possible solution to the function.
function error($err, $struct = 'phrase', $message = ''){
$error['status']['status']='error';
$error[$struct]= '';
switch ($err) {
case "empty":
$error['status']['message']='There was nothing passed to the webservice, so nothing can be returned.';
break;
case "partial":
$error['status']['message']="Not all required parameters were passed, so the webservice can not return any information.";
break;
case "not_found":
$error['status']['message']='The parameter you passed was not found, please try again.';
break;
case "bounds":
$error['status']['message']='The ID that was passed is out of the allowable boundaires. Please try your request again.';
break;
case "less_than_zero":
$error['status']['message']='The number recieved is less than zero, and will not work with this service. Please double check the number and try again.';
break;
case 'custom':
$error['status']['message'] = $message;
break;
default:
$error['status']['message'] = 'There was a problem with your request, please try again.';
break;
}
return $error;
}
Continue reading…
Tags: NuSOAP, PHP, webservices
Posted by stephen
on July 07, 2010
Applications /
3 Comments
In the previous post, we examined the client side of NuSOAP. In this post, we examine a very simplistic approach to building a server. In this example, it is going to use a very basic set up, and will return only a string. No complex data will be returned, and it will be a quick look at how to create a NuSOAP server. The more complex approaches will come later.
Using the previous information in the post, we have a client that we can text the service on. Before I complete the webservice server, I always do some quick testing of the functionality before sending it to the web service. In this example, we will use one of the services built previously:
showPhrases – Phrases from Shakespeare plays that replace the names in the phrase with the passed in name/string
This webservice requires 2 items to be passed:
id – A number from 0 to 4 (In this example I only used 5 phrases)
name – A name or variable that can be used to replace the names in the phrases
The first thing we should always do, is map out the plan for this function.
Continue reading…
Tags: NuSOAP, PHP, webservices
Posted by stephen
on July 02, 2010
Applications,
PHP /
No Comments
Doing a lot with webservices lately. Which is really a great thing if there is a central repository of information that needs to be disseminated between different external systems. I am doing a lot with NuSOAP and php SOAP. But this tutorial is going to be about the NuSOAP libraries. You can get these libraries from:
http://sourceforge.net/projects/nusoap/
First thing to do is to download this, and install into a directory that you can access. For different security reasons, it may be needed to keep these libraries outside of the accessible web directories.
The next step is to determine what needs to be required to get the data. This may include authentication and credentials, id’s, phrases, or anything else. It could be possible to not have anything required and just return all records. And that is the next step: determine what to expose. What type of information would you want to send back to the world? Hopefully it is not sensitive data, and only the data that needs to be exposed.
Now with that in mind, we are ready to go. I have set up a webservice server that has five different functions:
showPhrases – Phrases from Shakespeare plays that replace the names in the phrase with the passed in name/string
showTaxes – Calculates the tax based on the passed in price and state
showGroupItems – Shows what is needed based on a set event, all items are made up of course
showMadLib – Returns a mad lib based on passing in a number of different items
showNumbers – Returns a non associative array of numbers, requires an ID, and returns the numbers associated with that ID
Each of these functions provides a little more to view based on the webservice. The first and fourth functions will return a string of data. The second function returns an array with different data types. The third returns a complex type of a multidimensional array. I did this because there are many different tutorials out there using NuSOAP, but only seem to return a basic type and has very little to help when setting up the WSDL when it needs to be more complex. The fifth function demonstrates how to return a non associative array with the webservice in a complex type.
I will break down each function per post. But now we need a way that we can test these functions when we get them going. So the first thing to do is create a NuSOAP client to grab the exposed data. For the client, we will use the showTaxes example that has been created.
Continue reading…
Tags: NuSOAP, PHP, webservices
Posted by stephen
on June 21, 2010
Everything Else /
No Comments
For the past year or so, I have been tremendously busy and unable to keep up with the blog. However, now more free time is available to me. So starting next week, I will be posting more about PHP frameworks, including Cake and Symfony, security issues I see come up, and different ideas about databases, searches and other items. I will also be posting more about C# and ASP items. As I get through more development for the Apple platform, I will also post those as well. Hopefully this will be a good resource for others, as I use this to post when I learn something, so that I can go back and find what I used in order to keep things going.
And if there is anything that needs to be discussed, please let me know. We can always start up a discussion on anything.
Posted by stephen
on December 07, 2009
Everything Else /
No Comments
The PHP Advent Calendar 2009 is going on, and it is a tremendous resource for the community.
http://phpadvent.org/2009
I highly suggest subscribing to the feed, and/or following on Twitter. It is a great read every day.
Posted by stephen
on October 20, 2009
Applications /
1 Comment
Have you used the Spreadsheet_Excel_Writer PEAR package to output data to a spreadsheet? In this data that is being exported, have you needed to format the data? What about numbers, and then formatting further to a currency format. The currency formats for this data: one for Great British Pounds (£) and one for the Euro (€). The formatting needs to happen with those symbols. So here is the journey . . .
Since this post is not about the querying of the data, that is not going to be covered, but rather just the issues with formatting the currency symbols for Euro and GBP in the Writer. And since this is not about the Spreadsheet writer ins and outs, if you would like to know more about that, please see Package Information: Spreadsheet_Excel_Writer
Continue reading…
Tags: PEAR, PHP
Posted by stephen
on May 04, 2009
Applications,
PHP /
13 Comments
This post is about my experience with loading in PEAR to a CakePHP 1.2.x application. This may be the right way or the wrong way, but I got it to work throughout the application. I had to do some changes, and if there is a better way of doing this, please let me know.
First off, here is the issue. I needed to be able to export a group of records from the database to an excel spreadsheet. I have tried to use the Excel Spreadsheet add in that is listed on the Bakery. It works nice, and I had to do some modification for 1.2, but it worked. But not the way I wanted it. I have used the PEAR library Spreadsheet_Excel_Writer before and I like the type of control that I wanted, over the cells, the formatting, the merging, etc etc etc. It provides the type of control that I wanted. So here is what I did to get this to work with the CakePHP framework.
First, I have to download the PEAR library and the Spreadsheet_Excel_Writer libraries to use. Since I use a local system to help develop, I could download these libraries to the local system and transport these over to the CakePHP application. So I went to PEAR site to get the libraries. To download these I ran the following commands:
pear install PEAR-1.8.1
pear install OLE-1.0.0RC1
pear install Spreadsheet_Excel_Writer-0.9.1
URL’s are listed below:
http://pear.php.net/package/PEAR/download
http://pear.php.net/package/Spreadsheet_Excel_Writer/download
http://pear.php.net/package/OLE/download
Continue reading…
Tags: cakePHP, PEAR, PHP