Category Archives: PHP

Adding Comments in your Site with the Facebook API

Now that I have jumped almost 2 weeks without a post, as I have been super busy, this should have been a real easy item to post, but I wanted to make sure that this is done correctly. This is probably one of the easiest methods to add some great Facebook functionality in your site. This revolves around comments to a page. In my example, I am posting topics to discuss. This is mainly just a small little blurb that I will enter via an admin form on the site, and then list the different topics for everyone to select one. Once they select it, they can view the details of the topic and then comment on it using the Facebook API/Social plugin. So as always, lets go through a basic plan for this idea.

1. The model is Topic, with a table in the DB labeled “topics”
2. Only the admin has access to add or edit the topics
3. All comments on this topic will be done through the Facebook API/Social Plugin Comments
4. Topics will have a title that will also double as the Unique ID (to be explained later)
5. Topic titles, or themes, will not be allowed to be edited, to be explained why later
6. Administration of the comments will be done by the Facebook Application admins, which differs from the site admins
7. Start Dates will determine if the topic is allowed to be visible yet
8. End dates are optional, and will be built upon later with more advanced FBML/FB JS libraries

And there it is, some basic ideas behind the whole idea. So now lets get into some of the items called out in Numbers 4 and 5
Continue reading Adding Comments in your Site with the Facebook API

Facebook Application on the Site

OK, I finally got my data models set up and working. I have the initial CakePHP set up on the site, it is using v1.3, and now I am ready to set it up for the Facebook integration, and start to add the integration. When we first set up the application on the Facebook side, I chose to do an “iframe” version of the application, as I want Facebook on my site, and be able to have integration with some of the great Facebook tools on the site, and be able to “promote me”. And remember this is just a way to show a possible real world example of how to integrate these things with your site. Actual applications may vary, but this is the base to integrate. At least, as of this posting it is the base, it may change in the future.

So lets go ahead and dive into it. If you do not have the application ID for your Facebook application, you can get it at the following:
http://www.facebook.com/developers/apps.php

The next thing is to grab the API and code from Facebook. This can be found at the following page:
http://developers.facebook.com/docs/

This is the main page, and you will need to scroll to the bottom of the page. This will list different APIs that are available. I am going to be using the PHP and JavaScript SDKs. This will provide the back end that I will want, and will also provide a positive user experience on the front end. So be sure to download both SDKs.

After that, now we need to start getting some stuff set up. In this post, I am just going to explain how to get this set up, and working right now. It is important that we get the correct items working, and so we will be working with the “pages” area for the JS SDK, and creating a very simple controller for the PHP SDK so we can get set up and running. I am just using, for right now, the base CakePHP CSS styles and layouts. All we need is a page to display some of the basic items to ensure that we have installed the SDKs in the proper locations. So lets go.
Continue reading Facebook Application on the Site

Its the Simple Things

I am still working forward on the Facebook integration and resume stuff. And will get that posted when I have completed the data model and the basic set up. I will also post about how to integrate Facebook into a CakePHP application. But in the mean time, there was something that happened, that I thought may help me remember.

The other day I was working on an application with a simple login check and display of an error message. I would test the login function with a correct account and an incorrect account. It was driving me batty as when I logged in with wrong credentials, it would not give me the message that something was wrong. I was checking everything. I checked the controller, the model and the view. Could not pin point the issue. I could not find anything wrong. Here is what I had in the view:

if ($sess->check(errors)) {
    $sess->errors);
}

Basically, if the login is incorrect, it will set an error. The session object will read any errors, set the item in the session to display once, and then kill the error so it does not keep happening. And here is the problem, in another application using the same basic code it works fine. Could not pinpoint why this is a problem here. Until I took a brief walk away from the desk. Then I came back and understood what I needed to do.

The check function does not do any displaying of any kind. It is supposed to check the session for errors, and destroy the session element with the errors. Before it does that, it sets a variable for the errors. However, it does not display. I needed to alter it to this:

if ($sess->check(errors)) {
    echo $sess->errors);
}

And there it was! Wow, the littlest thing. I am reminded of a Simpsons episode in which Hank Scorpio tells Homer:
“Well, you can’t argue with the little things. It’s the little things that make up life.”

Indeed it does.

Facebook Integration Initial Steps

The next few posts will examine how I will integrate a Facebook application into one of my sites: stephenhird.com. If you have browsed to this site, you will see there is already a Facebook platform there. And while it does show some of the great things to do with the API, it is very basic and really does nothing. So In this post I will examine what I want to do with it, how to set up an initial application on Facebook, and then go from there. This will likely happen over the next few weeks/months and will be a little more drawn out depending on how much time I have to document this process. This is step one, and step one will always include planning.

First I need to figure out what I want to have on my site. Since it is my name that is on the URL, I will need to create a way that this site will be an online portfolio, biography and information repository. When doing this for any site, it is important to remember your brand. Even for individual sites like this, it is important for branding, because this is who I am, I do not want it to be a classic case of slop on the web. This site before was mainly just a testing ground and now will need to be more.

A side note here, my name is the same spelling as a famous photographer based in London. His site is well put together with good descriptive links and a great example of combining minimalist ideas with styled presentation. He keeps his brand on the pages and the site does not confuse or mislead with extra peripheral items, or overuse of Flash or other heavy web technologies loading down the page. His site is located at: http://www.stephenhird.co.uk/

OK, now on to the planning and setting up the Facebook Application.
Continue reading Facebook Integration Initial Steps

ORM Designs and Tools

ORM, that magical acronym that can send developers into a flurry of excitement, or the rolling eyes of grief. ORM stands for Object Relational Mapping. Used correctly, this can really help applications convert data into objects ready to use. While it does cause some overhead, the key thing to remember is that used properly, it can be really helpful. But that is also the way it is with anything related to code. Since I focus mainly on the open source areas, examining an ORM tool will be limited to the PHP view and aspect.

When designing the data objects, it is important to understand the data and how it relates to the application and other objects. Usually it is the planning sessions that get overlooked, or hurried, and create a problem for later on. This is why using a tool to help with this is always good. I specifically look for more of an ERD (Entity Relationship Diagram) type of tool that can help me visualize and document the data objects. Never underestimate the power of a visual diagram for data. This is worth everything when coming on a new project, or bringing in new resources to a major project. It can drastically decrease the learning curve. There are different tools out there that can help with ERD and ORM design, with the basic Visio diagrams (a Microsoft product), to more robust ERD tools that include different UML (Unified Modeling Language) tools integrated with it like MagicDraw. Each tool will have its pros and cons, no matter what the toolset is, no matter what the project is. I have found that there is not a single tool that is perfect for every project. But finding a good tool that can help in designing the database is a must. If it can even generate code, that is a lot better. One tool that is available that can help with ERD, and generate code, and help with ORM is ORM Designer. And I will examine this tool as it relates to Symfony, CakePHP and non framework applications.
Continue reading ORM Designs and Tools

Facebook Integration

As you have probably noticed, I have integrated a pseudo-Facebook hook into my little WordPress blog. I am wanting more comments to come thru via Facebook, and am building a full Facebook application that will integrate my posts in a non WordPress format, link to different resources on command and provide a way to search and query those items. All on Facebook. The way I am doing this right now is using the Facebook Application with the iframe setting.

So if you have not had a chance to connect to my Facebook App, here is a shameless plug to go ahead and do this now. I will combining the HirdWeb page with the HirdWeb Application. Since I am on Facebook quite a bit, and doing applications for Facebook API integration, I will start to share some of those techniques.

The application I will be walking through is an application I already have half way set up at my own site: www.stephenhird.com. This will soon be taken down and I will be putting up a better site to show the power of the Facebook API and how to integrate your site with Facebook for different things. As always, I will post the entire code page and any connections it needs to have. I will also post the directory structure of a possible integration of the Facebook API into an external application.

Modifying the NuSOAP Server

OK, the last couple of posts have strayed from the NuSOAP topic. but I wanted to do a quick post before going into to testing the server on a .NET application. The whole point of this post is to modify the result set we are sending back for some of the items. And let’s also change around one of the inputs so it accepts an array of data as the input, instead of strings. Hopefully this example will help others build a NuSOAP server. Which is why I am doing some of these examples. I was tired of seeing example after example of NuSOAP servers with a “Hello World” type of structure. That does not help in the real world. So hopefully these examples will help others.

First, we need to recall our last example we had out there, it was the following files:
Data Used: http://www.hirdweb.com/webservice/items.txt
Server File: http://www.hirdweb.com/webservice/20100720_server.txt
Client File: http://www.hirdweb.com/webservice/20100720.txt

We will be modifying those files, so if you do not already have those, you can grab those there. The data file “items” is ok, and we do not need to change anything there. The server file we will need to change, so go ahead and save that file with a new name. After you do that, you will need to change the line in the server that sets the wsdl address. I named the new file 20100805_server.php.

$wsdl_addr = 'http://www.hirdweb.com/webservice/20100805_server.php';

The first thing to do is change the result set for the showNumbers functions. This is the function that accepts an identifier of 0 thru 5 and returns a list of numbers. It is a pretty simple example. In the real world, the identifier may be a book ISDN, or a product code, or a user id. Based on how you use this, it could return anything, like a list of authors (ISDN), or related products (product code), or friends (user ID). Or this can be used in conjunction with another function. But we need to format this a little better.
Continue reading Modifying the NuSOAP Server

Code Standards

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 Code Standards

PHP 5 News

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 PHP 5 News

Finishing up the NuSOAP server

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 Finishing up the NuSOAP server