Archive for category PHP

AMF,XMLRPC,JSON,REST Zend Web Services Tutorial

I’ve been using the Zend for the last few months and I’m loving it. Here at work we were having a discussion about implementing some web services in the future so I decided to see what it takes to create some web services in PHP using Zend. I was pleasantly surprise (well not really surprised) that it was extremely quick and easy to get things up and running.

In this post, I’m going to explain how to create web services that can be accessed via AMF, XMLRPC, JSON, and REST using the Zend Framework. Hopefully these should cover most uses out there. I left SOAP out since I don’t really see myself using it any time soon. I will be communicating with a database as well to make the tutorial more informative. The db will consist of one table with information about courses such as mathematics courses.

I’m going to show you the final product first, hopefully to catch your attention so you’ll read the rest :) . This is what the final product will look like. If you look at the following links in Chrome, do a view source to see the formatted response.

XMLRPC

One course:  http://www.joeyrivera.com/ZendWebServices/xml-rpc/course-info/abbr/math101/
All courses:  http://www.joeyrivera.com/ZendWebServices/xml-rpc/courses-info/

JSON

One course:  http://www.joeyrivera.com/ZendWebServices/json/course-info/abbr/math101/
All courses: http://www.joeyrivera.com/ZendWebServices/json/courses-info/

REST

One course:  http://www.joeyrivera.com/ZendWebServices/rest/course-info/?method=getCourseInfo&abbr=math102
All courses:  http://www.joeyrivera.com/ZendWebServices/rest/courses-info/?method=getCoursesInfo

AMF

One course:  http://www.joeyrivera.com/ZendWebServices/amf/course-info/abbr/math101
All courses:  http://www.joeyrivera.com/ZendWebServices/amf/courses-info/ Read the rest of this entry »

Tags: , , , , , , ,

Calling a DLL with PHP

So here’s a quick post on calling dll’s in Windows using php. I have a dll that encrypts data in a certain format that we need for another process. So I need to pass the dll a string and it returns the encrypted string back.

I tried calling the dll using the COM class in code and was having issues until I realized I have to register the dll in windows first before I can call it using the COM class. To register a dll in windows you do the following in your command line:

REGSVR32 MyStuff.dll

Now that the dll is registered you can do the following to start accessing the dll:

$my_dll = new COM(‘MyStuff.Functions’);

MyStuff is the dll name an/or id and Functions is the object inside the dll that we want to use. Now I call the method I need and pass the parameters:

$encrypted_text = null;
$input = ‘This needs to be encrypted.’;
$my_dll->EncryptString($input, $encrypted_text );

This is pretty much it. We instantiate the COM class with the dll and function I want. Then I call the method in the dll passing my text and it returns into my $encrypted_text var the encrypted text. I can now do my next process with the encrypted text like:

print $encrypted_text;

Tags: , , ,

Dynamic PHP Images Tutorial

So I decided to play around with creating a dynamic php image to see how much work was involved and it’s actually pretty easy to do. In my case I wanted to solve the following problem.

I have a trip planned for a weekend coming up and I keep wanting to see what the weather is going to be like for that weekend. It seems everyday the weather.com forecast changes so I keep checking it on a daily basis. My options are

  1. go to the weather.com website, search for the zip code, then select view 10 day forecast or 
  2. create a quick and easy automated way of looking for this information as a graphic. 

Of course, I picked the latter. Here’s what we are about to create:


Read the rest of this entry »

Tags: , , , ,

Using MySQL Stored Procedures with PHP mysql/mysqli/pdo

Wondering how to use stored procedures with PHP and MySQL? So was I and here’s what I’ve learned. In this tutorial I’ll explain how to use PHP (I’m using 5.2.6) to call MySQL (I’m using 5.0.2) stored procedures using the following database extensions:

First we need to setup our enviroment which consists of a new database with one table and two stored procedures. In your db tool of choice (I’ll be using the MySQL Query Browser) create a new database named test. After you create the new database, make sure to add a user called example with password example to the database and give it read access.

CREATE DATABASE `test`;

Now create the table users:

DROP TABLE IF EXISTS `test`.`users`;
CREATE TABLE  `test`.`users` (
`users_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(100) NOT NULL,
`last_name` VARCHAR(100) NOT NULL,
PRIMARY KEY  (`users_id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

Read the rest of this entry »

Tags: , , , , , ,

Random image from folder – rotating banner in php

So the other day someone was looking for a quick php script that would load a random image from a folder (rotating banner script) and display it on their website. There are already a ton of scripts out there that do this but of course, this was an excuse to build something for fun. So, I decided to create a script that would do just this but with the least amount of code lines still keeping it efficient (again, just for fun). This is what I came up with:

<?php
$files = @opendir($dir=trim($_REQUEST[‘dir’])) or die(‘Not Valid’);
while($file = readdir($files)) preg_match(‘/(.png)|(.gif)|(.jpg)/i’,$file) ?
        $images[] = array($file,substr($file,strlen($file)-3)) : null;
count($images) ? header(‘Content-type: image/’.$images[$id=rand(0,count($images)-1)][1]) :
        die(‘Not Valid’);
print file_get_contents($dir.(substr($dir,strlen($dir)-1)!=‘/’?‘/’:null).$images[$id][0]);
?>

Read the rest of this entry »

Tags: , , , , , , ,

Getting list of members from an event in meetup.com with PHP

So, last week I attended our monthly Atlanta PHP meet and had a good time. At the end of the session some prizes were awarded “randomly”. Why “randomly”? Because a member of the group wrote down two names on a piece of paper and started asking the other members for a number. The two to get the numbers correct won an award. Now, this was a quick last minute thing for fun, so I am definitely over engineering this – but hey, that’s what makes programming fun!

The problems were:

  • (and I apologize for my honesty in case any member that attended the event reads this) I could see the persons hand movement while writing down the numbers so I already knew one of the numbers before starting. 
  • The paper was lifted at some point before he started calling out for numbers and I could see the second number written on the paper.
  • It’s not truly random

Read the rest of this entry »

Tags: , , , , , , , ,

ob_start(), ob_flush(), flush(), set_time_limit() – Give user feedback during execution

So every once in a while I want/need to write some code that may take some time to execute. The problem is because it takes so long to execute, I need to make sure to give the user some feedback during this process so they know the page is working. If I hit submit in a page and nothing changes in my screen after 20-30 seconds I assume something is wrong. For example, I wrote a php page that would query multiple users in one db, then for each user load their xml data from another db and parse out the information to return a report for all users in the date range. Depending on the date range, this could take a few minutes to complete. This is where the php output buffer comes into play. Read the rest of this entry »

Tags: , , , , , ,

Atlanta PHP December Meeting – Singleton Design Pattern and Moodle

So I attended the Atlanta PHP meeting last week and it was fun just like the previous time. I find this particular group fun and knowledgeable making me look forward to the next meeting. The topics covered this time were the singleton design pattern and the Moodle course management system. It seems like we are going to start covering a design pattern plus another topic during each future meeting which I think is a great idea. You can never know enough about design patterns.

Singleton Pattern

So there wasn’t much said that was new to me about the singleton pattern. It’s pretty standard, the objective of this pattern is to prevent creating multiple instances of a class. The code example was something like this: Read the rest of this entry »

Tags: , , , , , ,

PHP – Copying x Files to n Folders

So a co-worker needed to find a way of coping a few files (10 files) to a bunch of folders if the files weren’t already present. Instead of doing a manual copy paste in Windows I created this code to do the above. If anyone needs to do something similar feel free to use this code. Pretty much all is does is create an array with all the folders where this PHP file is being run (this is the root where the bunch of folders reside). Then it creates another array of files – these are the files that we need to copy to all the folders. This array is created by looking in the templates folder and adding each file to the array.

The ‘foldersOnly’ function gets the array from scandir() and cleans it up. It removes the ‘.’, ‘..’, and a couple Dreamweaver folders like ‘_notes’ and another one I can remember. The new array returns only the folders we want to work with. The ‘filesOnly’ gets the array from the PHP function scandir() and returns only the files in that folder. Once we have our arrays we are ready to start.

We are going to loop for each folder and inside each folder look for each file. We check to see if the file is in the folder, if so we do nothing. If the file is not there, we copy it from the templates folder. This is pretty much it, once all the loops are done every folder should have the missing files. The code is below: Read the rest of this entry »

Tags: , , , ,

Uploader Phase 2 complete!

Here is a screen shot of the Uploader 0.1.0

Uploader 0.2.0

So today after work I decided it was about time to start working on phase two of my Uploader tool. Phase two consists of allowing me to drag and drop an image URL into the app so the app can then download the image to a folder in my server, create a thumbnail of this image, and update the WordPress database with information about the new image. The new image would then show up the next time a person visits my blog. If you notice, there is now a “Pics of Interest” section on the sidebar to the right of the page (similar to what people are doing with the flickr plugin). Every time I drag an image URL into the Uploader, the new image thumbnail will show in that area and if you click on the thumbnail you’ll see the full size image.

It was actually much easier than I originally thought to make this work. Technically, the Uploader already has the drag/drop functionality and already takes in a URL. The only extra AS3 code was a check to see if the URL was an image and if so call the PHP page with a code (I’m passing –@IMAGE@– to know it’s an image – it was just the first thing that came to my head) so then PHP knows this URL should be treated as an image not just a URL. If you remember, phase 1 for the Uploader allows a person to drag/drop a URL into the app so it creates a new link under the “Links of Interest”. Read the rest of this entry »

Tags: , , ,