Showing posts with label api. Show all posts
Showing posts with label api. Show all posts

Thursday, December 20, 2012

PHP Script Execution Nature, A Problem and Solution

Do you know that a php script cannot be executed simultaneously on a single server. Even another script which is included with a executing script also becomes unavailable for execution or being included with other script. Usually a php script with complex algorithms and multiple mysql operations takes approximately 0.005 seconds for completing execution. For usual scripting with some mysql operations and an limited amount of users, the execution nature of apache server (PHP) is perfect. But, suppose, if a script has single or multiple API request to remote servers, then it will take some extra seconds to accept the reply. By the mean time other requests will be queued and will be processed while the script becomes free.

Recently, I have created a script which has more than 25 API requests and requires around 1-2 minutes for completing the whole execution. As the script has some dependencies over some universal scripts of my application, so I needed to change something to make the system always accessible for other users. First thing came to mind was to divide the whole execution and run it on multiple execution to make sure it frees the related scripts for other users for some while. I have used a Session Variable ($_SESSION) where I have put all information required for requesting API request. For every execution of the script, now it sends only a single API request, and then frees the script and redirect to the same script for rest of the requests.

Thursday, December 1, 2011

How to Create a Text File Using PHP

Once, before invention of relational database, file (text) were the only way to save information. Now, it is more easier to develop an Information System using database and database is more suitable than using text file. But, still it is needed to use the file for different purposes. For, example, it is needed for ftp based banking API integration. For some projects programmers need to create rewritable php or xml scripts also, for these purposes, files are used. Now, we are going to see how to create a text file using PHP.

Step 1:


Create a php script file and codes will remain inside php tag.


Step 2:


Specify your text and save it to a variable.
Suppose, you want to save the text: "Let's make a text file using php." Then, store it to a variable.


Step 3:


Create a blank text file with your expected name and extension. For this example, we will create a example.txt file. Syntax of this code is fopen("file/pathname","permission"). If you want to store the text file into your current directory from which the script is running then just give the name of the file. But, if you want to store it to a specific location, then you have to give complete path with the name of the creating file. Suppose, your script is located at "root/scripts/file/", then with only the name of file will bd created with location: "root/scripts/file/example.txt", but if you want to store it to root directory then give path like "../../example.txt". Permissions are usually write/read. If you want to just create the file as read only mode, keep permission "r". Or, if you want to keep it in write mode, then keep "w" or "r+". We will keep it "w" (write permission mode) for writing a text into it. And we are going to store the file into the same directory of the script's location.


Step 4:


Store the text into the file. Require php syntax for this action is fwrite("file variable", "Text/ text variable"). As we have created the file and stored it's information in $file variable and already stored the text into the variable into $text variable, our syntax will be like fwrite($file,$text);


Step 5:


Close the file and end php tax.


This is a simple tutorial of creating a text file. You will need to do extended coding for fulfilling additional requirements of your project. It will be great pleasure of mine, if this tutorial helps you.

Tuesday, November 29, 2011

ftp Based Banking API Integrations

Lots of financial organizations use ftp as the mean of money transferring gateway to reduce remittance cost. Usually they have a ftp server to which their clients have different user credential to get access. At least two different directories remain as default in every user's ftp directory named as "In" and "Out". Some of these money transferring agencies have their own format of text for getting order of financial activity, some of them doesn't have any, but they usually make contact with their client for understanding each other. For executing a money transfer or any other financial activity client should get access to the ftp directory and add a text order file in ftp "In" directory. Companies simultaneously checks every client's ftp "In" directories for checking new orders manually or automatically and if they find any new file, then they starts to execute the order.

Every text order file has a portion where it saves the current status of the execution of the order. For a new order client keep the status as "Pending" or "0:Pending". When the financial organization, to which this order came, receives the order and starts to execute they changes the status as "Processing" or something like that. After the completion of the order, they copy the file and saves it to ftp "Out" directory with status changed to "Successfully transferred" or something like that.

The client also simultaneously checks the ftp "Out" directory for knowing if the order is completed. They check files manually or by their server's CRON job of script which automatically checks the ftp directory. When they find a new text file at "Out" directory, they checks relevant order text files name to check which order is completed. Ordered organization usually keeps the name same to the text "order" file when they completes the specific order and save a completion text file to "Out" folder.

Please See Also:


1. How to create Text File using PHP
2. How to upload a file to ftp directory using PHP

This is the rough procedure of ftp based banking. I will share some codes that is needed to configure a ftp based banking API integration to a client's system next.