Archive for the ‘ PHP ’ Category

Timezones in FuelPHP

Just thought I would make a note that if you are seeing weird timezones in your FuelPHP programs that you should checkout fuel/app/config/config.php around line 72


/**
 * DateTime settings
 *
 * server_gmt_offset    in seconds the server offset from gmt timestamp when time() is used
 * default_timezone        optional, if you want to change the server's default timezone
 */
'server_gmt_offset'    => 0,
 'default_timezone'    => 'America/Denver'

I have already set these to my local timezone of  ‘America/Denver’ but you can simply comment them out and the default from your php.ini will take effect.

State of Zend Framework 2.0

Matthew Weier O’Phinney has posted about the State of Zend Framework 2.0:

The past few months have kept myself and my team quite busy, as we’ve turned our attentions from maintenance of the Zend Framework 1.X series to Zend Framework 2.0. I’ve been fielding questions regularly about ZF2 lately, and felt it was time to talk about the roadmap for ZF2, what we’ve done so far, and how the community can help.

He goes on to talk about the processes being taken to get Zf2.0 the exemplar of PHP 5.3 and to add namespaces to the project. He talks about his team, the road map and the struggles they have already expereienced:

After completing this process, my entire team — all three of us — started the work of migrating the code to namespaces. Ralph wrote a tool that scanned the library and created a map file of existing classes and suggested namespace/classname combinations. We then used this tool as a launching point for the migration, each of us working on a component at a time. This work was by no means automated — we discovered very quickly that such a tool only took care of the most cursory work. I detailed some of our findings a couple months back; we ran into a number of issues we never anticipated, and the progress has been far from speedy. At this point, however, we have migrated everything but theZend_Service classes, the MVC, and those components that build on top of the MVC (Application, Navigation, Form, etc.).

Finally he shares details about getting ZF2.0 available through Git and Github, and what the community can do to help:

A number of contributors are also starting to discuss rewrites and refactoring of components. Much of this is being done on the zf-contributors mailing list, and some on the #zftalk.dev channel on Freenode. If you are interested in contributing, I highly recommend subscribing to the list and dropping into the channel when you can.

You can read the post in it’s entirety at  http://weierophinney.net

PHP, Soap and WSDL Caching

Today I was offered a free lesson in how PHP handles Soap, and more specifically WSDL caching.

When you instantiate a PHP Soap Class you pass it a WSDL (Web Services Description Language) and it looks like this:


$client = new SoapClient('http://example.com/services/thescript.php?wsdl');

Depending on how your caching is setup, this will go out to the WSDL provided, pull in the document and store it in a cache. The default cache for PHP is 86400 seconds. If you are familiar with time in seconds you know that that is 24 hours. So by default you only actually read the WSDL once in a 24 hour period. Normally this is not a problem.

What is a WSDL?

For that answer let’s turn to the W3 site:

WSDL is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information.

So in English that means it is a map to the services provided by the provider. Basically you get a copy of the map, and follow the endpoints to the answers you need.

One basic function of this is that it tells you want methods (services) are available on your fresh client.

When WSDLs go bad

So today one of my providers, without warning, updated their WSDL. This wreaked havoc on my system because I had cached the WSDL on my side for 24 hours (remember, the PHP default). The change was a super simple change, a change in URL for a service, that’s it!

The problem was that since PHP had already read the file within 24 hours it didn’t care that there was a new file, that the URLs had changed, and so it continued to work as it had before.

The real problem wasn’t that the URLs changed, that would have been fine if the provider hadn’t shut off the previously reported URLs.

Turn off the cache

So you are left with two options when something like this happens. First, you can sit and wait up to 24 hours for the problem to fix itself. This was not an option for us. The second is to clear your cache and try again.

Now PHP (on Linux) stores the cache for the Soap WSDL in /tmp and I wasn’t sure which one of the many files was the actual cache for this particular provider so I decided I would have to tell PHP to not cache the WSDL and try again.

Into php.ini I went, located the soap.wsdl_cache_enabled flag. I changed it from 0 to 1


soap.wsdl_cache_enabled=0

I saved the file and restarted Apache.

The service continued to fail. I was perplexed. I thought maybe I flubbed the save so I opened the php.ini again located my line and it was fine. So I looked around there to see if maybe I missed another important flag. The only thing I could find was soap.wsdl_cache_ttl listed as the “(time to live) Sets the number of second while cached file will be used instead of original one” I promptly changed this to 0 as well


soap.wsdl_cache_ttl=0

Restarted Apache, and finally my script stopped failing.

I have since re-enabled my cache ttl and enabled flags, as reading the WSDL every time is a resource hog that doesn’t need to be in place.

The Lesson Learned

The lesson learned by all today is be aware of your caching, and be aware of the changes you make to your live/production systems.

PHP and Josso – Transparency Rocks!

Bogdan recently asked in my comments:

I can’t seem to find a call like josso_authenticate($name, $pass), returning an array to be appended to user’s SESSION. I was expecting such a method, because SOAP is already used everywhere, so it shouldn’t be too hard implementing this.

Has anyone had success implementing this kind of “transparent” login?

JOSSO simply doesn’t make it easy to log people in with a single call. Instead you need to make an interface to the API to do so. I just so happen to have an example of such a wrapper function.

Read more

Let’s Get Open!

Being that it is Monday and I am really not feeling it today I thought I would dig up what is happening around the nets and share with you.

Chrome OS Zero

Looks like Hexxeh has been hard at work getting a cleaned up version of Chrome OS called Chrome OS Zero out to the masses. Reading through the Wiki and FAQ things look pretty nice and clean. I may take a stab at playing with this in the next week or so. Thanks Hexxeh!

Droids

Tim at CTRL-ALT-DEL Comic has this silly for us today:

Thanks Tim!

Nexus One

We all know about Google’s latest move in the Android field by now, right? Well it looks like the Nexus One is selling well, and people are having problems with the 3G service that T-mobile is offering on the Nexus One. Being a T-mobile customer myself I can tell you the problems are not limited to the Nexus One. My phone refused to stay on 3G this weekend too, oh well.

PHP

References

Johannes Schluter discusses how references in PHP work, and suggests that maybe we should not use them anymore.

Last year I spoke at eight conferences and attended a few more multiple times at most of them I found myself in discussions about references and PHP as many users seem to have wrong understandings about them. Before going to deep into the subject let’s start with a quick reminder what references are and clear some confusion about objects which are “passed by reference.”

Patterns

Giorgio Sironi has two new blog posts about patterns in PHP. The first is on Abstract Factory patterns:

The major problem that creational patterns try to solve is that objects need collaborators: we often pass them in the constructor of a Client class to aid decoupling, as every class should know only what it really needs to get its job done. With the verb know I mean that they just know that the other part exist at all.

The second is on Builder patterns:

The Builder pattern’s intent is to encapsulate the details (the new operators and other wiring) of the object creation process under a common interface. Though, the Builder can actually change the internal representation of an object, as it is not a black box.

Both blog posts were great reads, and I suggest all my UPHPU buddies hit them up.

PHP Complete Reference and A Beginner’s Guide

The PHP Complete Reference

The PHP Complete Reference

Living up to it’s name the PHP Complete Reference is a tome of PHP knowledge. With great detail about each (and just about every) PHP function, the book easily warrants the space it takes on your desk. If you are like me you will spend most of your time flipping back into the book to remember the correct order of arguments for in_array or another function.

Don’t expect to learn anything fancy or exciting, the book covers the functions and the basics. It is also not exactly where you want to learn PHP, but that is where my second book I am reviewing comes in. The book PHP: A Beginner’s Guide is the perfect companion to the Complete Reference. What you learn in the Beginner’s Guide is backed up and reinforced in the Complete Reference.

PHP: A Beginners Guide

PHP: A Beginner's Guide

The Beginner’s Guide is really a great starter book for PHP developers, or those who think they will be PHP programmers. The information is provided in usable chunks, but fast enough that the book isn’t a boring text book. When paired with the PHP: Complete Reference you will become a better PHP developer in no time at all.

Both books are from McGraw-Hill, and are availble at Amazon.com or directly from McGraw-Hill.

Pro PHP: Patterns, Frameworks, Testing and More

Pro PHP: Patterns, Frameworks, Testing and More

Pro PHP: Patterns, Frameworks, Testing and More

Kevin McArthur is a self-taught entreperneur and opensource developer from Edmonton Alberta. Kevin has been running a very successful PHP application development studio for over 7 years. Additionally Kevin took time to write Pro PHP: Patterns, Frameworks, Testing and More, published by Apress.

The book really lives up to the name. It disucsses in great details the framworks that are most popular including Zend Framework. Kevin has what seems an infinite amount of knowledge on Zend Framework, and a quick Google search will reveal he is quite active is sharing that information.

I was a little let down on the Testing and Code Control sections of the book. I felt like Kevin mearly skimmed, and could have really dug into more detail, espeically about testing.

In talking about patterns for PHP Kevin reinforces some of the basics of good programming, and explains the pros and cons of each style of framework. You can tell he has spent his fair share of time in each of the patterns discussed.

Overall if you are thinking about playing with patterns and frameworks this is the book for you. Don’t look for any golden knowledge about testing, but the rest is gold!

Josso & PHP

I recently had the pleasure, if you would like to call it that, to implement JOSSO with PHP at my work. I quickly learned a few things about the process of setting up Josso with PHP, that aren’t well documented, and as such I am going to share my findings.

Downloading
Let’s start with downloading the PHP library for Josso. Most people will head straight to the SF.net repository for Josso and look for the PHP link. Well, let me save you the headache, it ain’t there. The PHP library is stored inside of the josso main package (currently josso-1.7.zip as of this writing). The main package by-the-way is 80+ MB. You are looking for roughly 30Kb of files inside that zip.

Inside the zip you will find tons of files, and folders . Let me guide you to where the PHP files are: (after unzipping)

\josso-1.7\josso\core\src\plugins\php\php

No, the double php is not a typo. Inside of that directory you will find three class files, a config file, a couple of login/logout views, the security check, and the josso file. Be forewarned there is also a nusoap directory, and this thing will cause some headache if you are not prepared.

Installation
Unless you have full control of your server that you are installing the JOSSO + PHP interface on, you will not have access to add Josso to the php.ini file to make is autoload on every single page. For this I really suggest using a MVC with Front Controller setup. This allows you to force the user through a single point to access any part of your site, and thus eliminates the need to make Josso autoload for every single php file on your site.

SOAP
Finally I want to talk about Josso’s implementation of SOAP. On their PHP site they write:

In case of using PHP5 be sure of disabling the native SOAP support in order to avoid conflicts with the SOAP API used by JOSSO.

That is 100% completely not needed. Here is why. The SOAP class they do use doesn’t touch the SOAP class from PHP, and as such it can simply have all references changed. I simply loaded all the files from Josso into Eclipse and did a find and replace on soapclient (the actual class that conflicts) and renamed it to josso_soapclient. There were only a handful of places to change this, maybe 10. Once they were renamed the Josso + PHP experience was quite simple.

Example Scripts
Lastly I want to quickly mention that when you load the sample scripts provided by Josso, you will get errors. Apparently some of the methods have changed since they wrote the Josso examples. I have submitted my changes to the Josso project, and I hope that they will be included as they only make sense. So if you get errors, just go into the code and look to see if 1) the method exists anywhere, and 2) find a similar method and call it.

Conclusion
I don’t want to sound as though I am bagging on Josso at all, instead I am just letting people know the problems I had, and how I fixed them. The rest of the setup was really quite simple. For Single Sign-on, Josso is great.

Update
So another small annoyance I found while using Josso was that when you send a bad login to Josso, Josso will hijack your user experience. I was passing the “josso_back_to” string, and I couldn’t think why it wouldn’t send the user back. It turns out that Josso only uses the “josso_back_to” string when there is a valid login, and a second parameter of “josso_on_error” is required for bad logins (or errors).

Book Review: PHPEclipse: A User Guide

PHPEclipse: A User Guide is a quick read. Having already worked with Eclipse for PHP development the book didn’t present any drastic or life changing additions to my technique with Eclipse.

The book is greatly geared toward the newest of PHP programmers, ones still looking to make a mark in the world, or those who are looking to escape the claws of another IDE and want to know what Eclipse and PHPEclipse are all about. Shu-Wai does and excellent job of explaining what Perspectives and Views are and how they work together to make up the meat of PHPEclipse.

Each chapter is full of quick information about how to setup a specific aspect of Eclipse to better work with PHP development. Chapters 3 and 4 explain how the IDE displays information about your application to you, and where to look for more information. Chapters 5 through 8 explain different plug-ins (CVS, Debug, Deploying code) and explain how they work integrated with Eclipse. The best part of each of these latter chapters is they explain in detail how to get the particular plugin installed and configured. It was a fresh and welcome look at plugins and how they work.

What peaked my interest in the book were two things. The first was Appendix A which explains more about where to get more plugins and information about the plugins. The second was the overview of Eclipse. A short history and explanation of how Eclipse came to be what it is today. I was particularly interested in the plugin development, which is built right into Eclipse.

Although the book is short, and a quick read, if you are thinking about picking up Eclipse and using it, you should get this book and read through it while you are in the process.