Posts Tagged ‘PHP’
Keeping Your LAMP Server Up To Date With Dotdeb
I have been maintaining Debian based Linux servers now for some years and at times I find it frustrating that the latest versions of my favourite packages are not available yet because of the delay in getting the newest version into the selected repository. This leaves you needing to build the program from source.
I recently discovered a project designed to get around this problem. Dotdeb is a repository for Debian systems that have the latest versions of PHP, MySQL, Redis, Apache, Nginx and other common web type packages ready to install or upgrade. With doing little more than adding the Dotdeb repository URL to your sources.list file and updating, the newest version of each package are immediately available.
I love now that I can easily have the most up to date packages with minimal fuss and leaving me with time to get back to development.
To find out more visit the project’s website.
Four Ways To Ease Facebook Application Development
For the last two months I have been developing a couple of Facebook applications for clients. Developing new apps for Facebook can be difficult and very time consuming in the fact that the applications need to be hosted on a publicly available server rather than in your standard dev environment. This is a pain for serveral reasons including the need to upload files each time a change is made and that you don’t want php or debugging messages being displayed.
Therefore you will want to send the debug and error messages elsewhere that you can easily watch. These tips are not complicated and I would hope that they are used by most developers at least some of the time for all projects.
1. Custom logger
Rather than printing debug messages to the screen I suggest that you send them to a custom log file which you can watch as new entries are added. Create a function similar to the following:
function logger( $msg ) { file_put_contents( 'log.txt', date( 'Y-m-d H:i:s' ) ." $msg\n", FILE_APPEND ); }
Once this function has been defined, you can easily send debugging messages to the log like:
logger( 'name is set: '. $name );
The new string and the set variable will be appended to the end of the log.
2. PHP error logs
When working in your own dev environment it is a must that you have errors sent directly into the browser. This way any warnings or fatal errors are immediately shown to you and you can fix and move on. This is undesirable (for several reasons) for a publicly available site so you need to log these to a file which you should also watch.
There are several methods to set PHP logging:
Enabling PHP error logging through Apache config
This in itself can be added in two places. The first option should only be available if you have root privileges to the server. Find the Apache virtualhost record (apache2ctl -S is handy for this) and set add the following:
php_value error_reporting 6143 php_flag log_errors on php_value error_log /var/log/apache2/vhosts/yourdomain-php_error.log
The second option is to create a file named .htaccess in the web root directory and add the same options. This may require AllowOverride to be set to All in the virtual host record for this to work.
Enabling PHP error logging with PHP
The same options can be added directly into your scripts with like this:
ini_set( 'error_reporting', 6143 ); ini_set( 'log_errors', 'on' ); ini_set( 'error_log', '/var/log/apache2/vhosts/yourdomain-php_error.log' );
If you plan to develop for a long period it would be best to set the log file to go into the /tmp directory so it doesn’t cause hard disk issues on your server.
3. Apache logs
Apache logs are also very useful in developing Facebook applications. By watching these files you can see when and what Facebook is downloading from your server. I found this extremely useful when making Ajax calls to the server to see what $_GET variables were being sent.
These log files can usually be found somewhere in /var/log/apache2 but it may be easier again to check with apache2ctl -S to see exactly where the log is being saved.
Watching the logs
The best way to watch these log files is to SSH into the server and follow the logs with the tail command with it’s -f follow parameter.
tail -f /var/log/apache2/vhosts/yourdomain-php_error.log
By following the file you don’t need to keep closing and re-opening the file to see new entries.
4. Rsync
The best tool to upload any files that hae been changed is to use the rysnc command. This tool will compare your local files with the remote ones and upload any changes found. This beats the hell out of using FTP. I usually create a script which I run with looks like this:
rsync -r --verbose ./public_html/* username@hostname:/var/www/yourdomain.com/public_html
This will continually prompt for a password but this can be overcome by setting ssh keys. Follow this tutorial on how to set-up ssh keys.
Conclusion
I hope this helps others develop and debug Facebook applications. If you have further hints or ideas, I would love to hear them.
Website Build With Flash/HTML Integration
Last week, we at Sputnik Agency pushed live the site we had been building for our parent company Kit Digital. What I like most about this website is that it required us to include dynamic Flash navigation that had little overhead and to have the flash headers and html update seamlessly without reloading the page.
Project platform
We decided to go with WordPress as the project required a good CMS that was easy for the client to use and was quick and easy to develop.
Flash navigation with little overhead
There are two main flash headers that include navigation. This navigation needed to be dynamic in the way that if new pages were created in WordPress, the flash navigation needed to include these also. Having the navigation work dynamically this way can create an undesirable overhead as generally this would require mulitple calls to the database.
I overcame this by creating WordPress plugins that used hooks to create XML files when pages were added, updated or removed. These XML files included hierarchical page information required by the navigation. Therefore when a page is loaded in the front-end, the Flash would just read the XML file rather than force PHP to make database query requests. This resulted in the pages loading faster and reducing server overhead.
One other benefit of using SWFAddress is that although page loading does not occur, you can still click back through the pages of content you have loaded. The browser will not behave this way with standard content replacement using AJAX.
Seamlessly update flash header and update content
The next task was to allow links (whether clicked from the Flash navigation or HTML navigation) to update the Flash navigation and HTML without reloading the page. This seemed like a very difficult task and something I had not seen before but we managed this using SWFAddress.
Using the SWFAddress Javascript library we could update the URL in the address bar which would trigger both the HTML and Flash to change their behaviour. Once the the change was caught I used jQuery to make a request to another custom WordPress plugin that would pull page content from the database and then update the HTML without needing to reload the page. Clicking on the Flash links did the same.
The end result is a very sleek and fast loading website where the content is completely CMS driven.
To see these pages in action, visit these links: VX Platform, Global.
Yii PHP Framework Presentation.
Each month someone from the PHP Melbourne user group will present a PHP framework to the rest of the group. The November meeting gave me an opportunity to present the Yii PHP framework and why I think it’s a great platform to build PHP projects from.
During the presentation I described the main qualities I like about the framework and demonstrated how quick and easy it is to begin a new project.
I recommend for anyone looking for a new php framework to give Yii a try. It’s a great framework that allows rapid application development. The best qualities I find in the framework include:
- Componenet based
- MVC
- DOA/Active record
- Scaffolding
- Form validation/re-population
- Theming
- Authentication and role-based access control
- Strict OOP (PHP5 only to make full use of PHP object functionality)
- Great documentation
- Active community
- Example project
As with learning anything new there is always a learning curve. I suggest when starting out to try the Yii blog tutorial which takes you through installing a new project, building controllers and models from the database structure to extending Yii.
Quick Tutorial On Getting Started With SOAP In PHP.
Understanding what SOAP is and learning how to use it can be very confusing. There is much talk of SOAP on the Internet but finding an easy to understand tutorial on how to get started with it are hard to find. Therefore I have written this article for those new to SOAP.
This article assumes you have a good understanding of PHP and XML.
You use SOAP just the same way that you would any PHP class. However, in this case the class does not exist in the local applications file system, but at a remote site accessed over http.
A SOAP service is expressed as a URL. For this example I am using one I found with Google: terraservice.net/TerraService.asmx?WSDL. If we think of using a SOAP service as just another PHP class then the WSDL document is a list of all the available class methods and properties. If you open that URL in your browser you will see an XML document that lists these methods (also known as operations) and properties. If you load the page without the WSDL in the URL you will generally just get a list of the available methods: terraservice.net/TerraService.asmx. Clicking one of the operations will show the XML the service expects and the response a successful response will return. This is a typical example of how I would learn about how to use the SOAP service.
For this example I will use the GetPlaceFacts operation.This can be down by the following code:
$wsdl = 'http://terraservice.net/TerraService.asmx?WSDL'; $trace = true; $exceptions = true; $client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
The new object is created with the PHP5 function SoapClient(). When first testing a service it’s best to set trace and exceptions to true so that any errors can be discovered more easily. We should now have created an object of the service and are ready to send a request. For this example we will use the GetPlaceList operation. In this case we need to send data along with the request.
$xml_array['placeName'] = 'Pomona'; $xml_array['MaxItems'] = 3; $xml_array['imagePresence'] = true;
The data here is supplied in an array rather than XML like the service expects. This is automatically converted by SoapClient.
To better understand where how to determine how these values should be entered into the array, we should check with the request format:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetPlaceList xmlns="http://terraserver-usa.com/terraserver/"> <placeName>string</placeName> <MaxItems>int</MaxItems> <imagePresence>boolean</imagePresence> </GetPlaceList> </soap:Body> </soap:Envelope>
You can see the first element after the body element is the operation that we want to use. Following is the data that we need to send. If there were nested elements then we would need to use nested arrays to match.
Now that we have set the data we can make our request which follows:
$wsdl = 'http://terraservice.net/TerraService.asmx?WSDL'; $trace = true; $exceptions = false; $xml_array['placeName'] = 'Pomona'; $xml_array['MaxItems'] = 3; $xml_array['imagePresence'] = true; try { $client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions)); $response = $client->GetPlaceList($xml_array); } catch (Exception $e) { echo "Error!"; echo $e -> getMessage (); echo 'Last response: '. $client->__getLastResponse(); } var_dump($response);
I also wrap the request in a try/catch pair to prevent any fatal errors. Also, from habit I dump the response with var_dump() so I can inspect the response in detail.
Sometimes you will find that the response values are quite nested. In this case I can trim back the response like this:
$response = $response->GetPlaceListResult->PlaceFacts;
Then we can loop through the results:
foreach($response as $key => $value) { echo '<br />Place: '. $key .'<br />'; echo 'City: '. $value->Place->City .'<br />'; echo 'State: '. $value->Place->State .'<br />'; echo 'Country: '. $value->Place->Country .'<br />'; }
This is a basic example of how to get started using SOAP. There is much more to it. By looking closely at the WSDL you will find which data must be supplied. This is shown by an attribute of minOccurs where equal to at least one.
For a definitive guide on SOAP I suggest going to www.w3schools.com/soap/default.asp.
Adding Captcha To Your Form
It wasn’t until this week that I required a form to be protected from spam. Captcha was the obvious choice but I did not know what was involved to add it to an existing site/form. For anyone who is thinking of doing the same I can assure you it is very quick and easy to do.
You can download the code and follow the quick start guide for PHP at phpcaptcha.org.
After downloading the files, move them into the sites path. You then just need to add some some short code segments into you existing form to include the captcha image and input field. Some code also needs to be added to the back-end including adding session_start() (If you’re not already using sessions) to validate the input once the form is submitted.
One dependency is that GD2 is installed which I believed is with most php5 installs these days on Linux anyway.
This shouldn’t take longer than 20 minutes. Had I known it was so easy I would have used it in previous jobs.