Entrepreneur Geek

Nirav Mehta on life, technology and future

Archive for the ‘PHP’ tag

Handling Unicode with PHP

without comments

Unicode characters and webservices always create one or the other problem for me ;-) Working on PlannerX backend was not any different. Spent some good hours fixing Unicode / UTF-8 related issues.

And while I was searching for some solutions, I found an excellent “PHP UTF-8 Cheat Sheet” by Nick Nettleton of DropSend. I highly recommend it if you are going to do anything with PHP and Unicode!

And BTW, don’t use Base64 encoding with UTF-8. It will not work!

Written by Nirav

March 27th, 2009 at 4:32 pm

Posted in PHP, Recommended Reading

Tagged with , ,

WSDL to PHP – Generate PHP code from a WSDL file

with 3 comments

There are many classes available to generate a WSDL file from PHP. But I wanted to create PHP classes out of a WSDL file. SOAP allows passing complex data around and to write a SOAP client, I need to have classes defined in PHP for these complex data. I did not want to manually go through WSDL and create different data types based on SOAP declarations. As a matter of fact, I wanted something that will create a SOAP Server stub for me out of the WSDL (not just a client!)

Now I did not find something that will create a SOAP Server, but I found two implementations that create a SOAP Client out of WSDL. This is still a good start, and I can do some further hacks to create a SOAP server.

Here are these two approaches:

  • WSDLInterpreter – WSDLInterpreter is a library that creates PHP 5 classes based on a WSDL document. It creates a SOAPClient via XSLT transformations. Unlike other wsdl2php solutions, WSDLInterpreter utilizes the WSDL document as its source of information, as opposed to the native SoapClient interpretation of the WSDL document. This allows for proper class inheritance, naming convention correction, and method overloading with proper method signature verification using strict type checking.
  • wsdl2php – wsdl2php is a very simple tool for PHP 5 to generate client code against a WSDL-file. It uses DOM to parse the WSDL and generates PHP code out of it.

Both approaches generate similar output. And are a great start if you want to use a third party web service in your PHP code.

Written by Nirav

January 28th, 2009 at 4:19 pm

Posted in PHP

Tagged with , ,

XPlanner PHP SOAP Client

without comments

I am working on a project management system and want to build a Mylyn plugin such that all tasks from the PMS show up in Eclipse / Aptana. I want to adapt Extreme Programming model of Iterations – User Stories and Tasks. XPlanner does this really well, and Mylyn already has an XPlanner connector.

So I want to keep the same XPlanner API and hook that up with my PMS! (Don’t have time / skills to build a new Mylyn connector yet!)

XPlanner has a SOAP client in PHP, but that’s written in NuSOAP. PHP’s SOAP support uses a different convention for web services. Here’s a simple XPlanner SOAP client for PHP (right after the jump!)
Read the rest of this entry »

Written by Nirav

December 26th, 2008 at 6:40 pm

Posted in PHP

Tagged with , , ,

40 Tips for Optimizing PHP Performance

without comments

Reinhold Weber has written 40 Tips for Optimizing PHP performance. Many of them were new to me – e.g. $row['id'] is 7 times faster than $row[id]. I don’t know how he has measured these numbers, but the tips are good overall!

Here are the first 10 tips:

  1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
  2. echo is faster than print.
  3. Use echo’s multiple parameters instead of string concatenation.
  4. Set the maxvalue for your for-loops before and not in the loop.
  5. Unset your variables to free memory, especially large arrays.
  6. Avoid magic like __get, __set, __autoload
  7. require_once() is expensive
  8. Use full paths in includes and requires, less time spent on resolving the OS paths.
  9. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
  10. See if you can use strncasecmp, strpbrk and stripos instead of regex

Read the full list of 40 tips here.

Read comments on the post as well, they too contain useful information.

Written by Nirav

November 28th, 2008 at 3:01 pm

Posted in PHP

Tagged with ,

Removing Word special characters from text / XML in PHP

without comments

Microsoft Word converts certain characters into "smart characters". Double quotes, dashes (em dash / en dash), bullets and so on.

These characters break PHP's XML handling. (or at least they broke it for me - using simplexml_load_string!).

How do you clean them?

There is an old post that suggests using ereg_replace on a set of characters - essentially converts them to html entities.

Unfortunately, that did not work with me. Since the text is UTF8, the replace logic replaced alphabets too.

I tried a lot to get a solution, but could not find something that would work. Finally, just stripped out all non printing characters except line breaks and tabs.

PHP:
  1. private function cleanWordSpecialCharacters($body)
  2. {
  3.   $body = preg_replace( '/[^[:print:]|\n|\r|\t]/', '', $body );
  4.   return $body;
  5. }

This too breaks with non English characters. Any suggestions?

Written by Nirav

November 15th, 2008 at 5:58 pm

Posted in PHP

Tagged with , ,