Monday, December 26, 2011

SOAP/WSDL based Banking API

Secured banking systems use SOAP/WSDL to communicate third party systems. It's completely platform independent and secured. This communication uses XML data transfers with secured gateway. cURL library (libcURL) allows PHP developers to develop SOAP transactions features between servers/systems. PHP has a built-in library for SOAP communication. But, nuSOAP is a perfect choice to develop SOAP client/server in PHP.

Download nuSOAP Library: Sourceforge

SOAP Client Development:


1. Include nuSOAP library first
It's like including a php script to other. If the nuSOAP library is stored in a directory "lib" with the filename "nusoap.php", then it could be include as follows:



2. Set Parameters
It is needed to set required parameters to call a wsdl server to communicate. Developer should give parameters based on the subjected WSDL server's specific function's requirement.

Suppose, developer has intended to communicate with a wsdl "https://webservicetest.webserviceurl.com:5555/.wsdl" and calls a function "testConnection". "testConnection" function of this wsdl requires parameters "id", "username" and "password".

Save parameters in a variable


3. Certificate Issue
It also requires specific SSL certificate of .pfx or .p12 format. PHP doesn't process .pfx formatted, so it is needed to convert it into set of files like "key.pem" and "cert.pem" with a pass phrase "pass" and save them in the same directory of the scripts (saving to same directory is not mandatory). (See how to covert .pfx or .p12 SSL into .pem certificate).

Better to store certificate and wsdl information into variables.
$wsdl="https://webservicetest.webserviceurl.com:5555/.wsdl";
$sslkey = getcwd()."/key.pem";
$sslcert = getcwd()."/cert.pem";
$passphrase= "pass";


3. Call soap functions
Now, it is time to initialize a soap call.

$client = new nusoap_client($wsdlurl, 'wsdl', $proxyhost, $proxyport, $proxyusername, $proxypassword);

$client->authtype = 'certificate';
$client->decode_utf8 = 0;
$client->soap_defencoding = 'UTF-8';

$client->setCredentials("","","certificate",
array(
"sslcertfile" => $sslcert,
"sslkeyfile" => $sslkey,
"passphrase" => "pass",
"verifypeer" => 0, //OPTIONAL
"verifyhost" => 0 //OPTIONAL
)
);

After initializing, it's time to call specific function of the wsdl. We are going to call "testConnection" function.



4. Recieve and use response
Now the response from the WSDL is stored in $result variable. It's an array, it's possible to use information from that array as developer need.

Friday, December 2, 2011

How to upload a file to ftp directory using PHP

For some projects, we need to upload a file into another ftp server. In this post we will show a simple procedure and description about uploading a file to a ftp using PHP.

Prerequisites


You should have the ftp locations and credentials.

Step 1:


Start PHP script with tag ()


Step 2:


Specify the ftp location in a variable. Location could be a domain like "ftp.domain.com" or also could be a IP address like "121.233.45.12".


Step 3:


Store user name of ftp server in a variable.


Step 4:


Store password of ftp server in a variable.


Step 5:


Store the destination location of the file you want to upload. If you want to store the file into the root directory of the ftp, then just mention the name of uploaded file only. But if you want to upload it to a specific folder then, mention the full path and filename. For example, we will use the "In" directory to upload the file with name "example.txt". (Remember, if you upload two files with same name, then second one will replace the first file.)


Step 6:


Store the source location in a variable. If you want to upload a file which is located at the directory from where the upload script is located, then mention only the file name. But, if your file is in another directory, then you have to mention the full path with file name. For example, We will upload a file situated in "files/example.txt". "files" is the name of the directory where "example.txt" file is located.


Step 7:


Establish the connection, store the information of the connection in a variable. We will use ftp_connect($server) function. As we stored server location in the variable "$server".


Step 8:


Login into the ftp server and store information into a variable.


Step 9:


This is portion is not obligatory. But, I always use this line to my code for successful ftp file transfer. I suggest you to add this line to your script.


Step 10:


Upload the file now. There are two different format of file to upload. They are "FTP_ASCII" and "FTP_BINARY". If you upload a text file, any of them you can use based on your text format "ASCII" or "UTF". But, if you upload a file like "zip" or other, use "FTP_BINARY" format.


Step 11:


Now upload is complete, close the ftp connection if you don't have any other ftp actions. If you have other tasks with this same ftp server just after the upload of the file, then you should not establish connection again.


Step 12:


End the PHP tag.


This is a simple tutorial of ftp file upload. But, you have to extend codes based on the requirements of your project. Every steps show lines of a single script.

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.