php-domain-information

How To Get Domain, Subdomain, TLD, CTLD & Path from URL In PHP

Recently for one of our project i was supposed to get Country Name, Sub Domain, Top Level Domain (TLD), Country Level Domain (CTLD) and Path from the given URL which will further be inserted into the database. Many functions which we used to test different scenarios, failed to perform on some logic or another. While searching for a better approach i ended up finding one answer in Stackoverflow by “Whatever Kitchen” which did the job perfectly. Here is a modified version of the function

Code Explaination

This function expects three parameters:

  1. URL
  2. Get What (default is everything)
  3. Output Format (default is an Array)

Calling the Function In Your App

You can call this function like url_information($myUrl, 'ALL', 'array'); within your application by sending those 3 parameters. Default output format will be an Array. Following are examples of how this function can be called to get the value.

I hope this function will help you get the information you need by providing proper parameters. Please feel free to suggest any further enhancements if you like. Subscribe our RSS to receive latest updates.

Integrating BrainTree Payment Gateway With PHP

BrainTree Payment Gateway API is cool enough for you to understand, but you need to setup the prerequisites first. This article assumes that you have installed the dependencies on your server. BrainTree API expect the followings:

  1. A Developer Sandbox Account – Create one here
  2. BrainTree Client Framework within your application – Download from here

Now, lets create a php file called _environment.php and place the following code in it

BrainTree Control Panel API Key

Now, login to your Sandbox account. You will see your merchant Id, Public Key and Private Key. Replace those values in this code.

BrainTree Transparent Redirect

We will use BrainTree Transparent Redirect to accept one time transactions. Create a file called single_transactions.php and place the following code in it.

Code Explanation

Above code is simple to understand. First we are showing a form to customers, taking their information and passing to BrainTree without actually redirecting our customers to BrainTree Payment Gateway. You can use these test credit card information:
Credit Card #: 5105105105105100
Expiry: 05/2015
CVV: 123

This is the most important piece of code, notice the transaction array in which we are passing amount and type to BrainTree PG.

$result holds the processor responses from BrainTree.

Now, we have the full information in $transaction and you can display the transaction information like

This is how you can use BrainTree Payment Gateway’s Transparent Redirect Method to make one time transactions. In the next part we are going to explain how to subscribe customers for recurring payments.

Creating Transactions from the Vault

BrainTree Payment Gateway expects Secure Credit Card Storage before actually initiating recurring billing to any customers. This is how it is expected.

  1. Secure Credit Card Storage
  2. Creating Transactions from the Vault

To develop recurring billing option first we will setup Secure Credit Cart Storage. This is fairly easy just use the following code.

Above code is straightforward, all commented code is optional but you can specify additional information you want to attach to a customer. First we can taking customers information on a form and then passing all the information to this function via AJAX or POST. The function calls BrainTree Payment Gateway and creates a record in BrainTree vault.

While creating a transaction we will need this token to tell BrainTree which credit card is going to be used for recurring billing. Finally, you will see your customer record in your sandbox like this

BrainTree Customer Record in Vault

If you can see your newly created customer in BrainTree Sandbox Vault, you are good to create Transactions (recurring in our case).

Creating BrainTree Recurring Billing Using PHP

To create a recurring billing option first, you need to create recurring billing plan under your sandbox account. Login to your sandbox account => Recurring Billing => Plans => Add New Fill the form and create a valid billing plan. Once you plan is created note the Plan ID as this is what we are going to associate our customers to. Following is the function to subscribe customers already in BrainTree vault to recurring billing plan we have just created.

Congratulations, you have just created your first recurring billing transaction. Should you need to cancel a subscription, you will need the $subscriptionID, simply use the following code

BrainTree on CVV

BrainTree’s stand on storing CVV

We can’t store the CVV according to PCI guidelines, but you can send it in with the Vault tokens. CVV rules will be applied to these transactions if the CVV is sent in with the tokens. However, CVV is not required when creating a transaction from Vault tokens. If the CVV is not passed CVV rules will not be applied.

I hope these methods will help you create BrainTree Payment Gateway one time or recurring billing transactions. I would love to hear your own experiences of using BrainTree Payment Gateway with PHP.

How to create XML with PHP Mysql

How To Create XML With PHP MySQL

Nobody can deny the power and benefits of XML which is mostly used in Java and .Net environment extensively. Using XML in PHP is also very popular and you will notice many PHP based web applcations using XML to it’s extent. A popular PHP/Zend software Magento ecommerce is also using XML a lot which tells that XML is here to stay with PHP as well.

I am going to introduce you to using PHP and MySQL to manipulate XML. I’ll execute a MySQL query and format the data into well-formed XML. Finally, I’ll explain how to write XML to a file and examine the system setup before diving into the code.

To use the code I’ve included in this article, you must have PHP and MySQL running, and you need to know your MySQL host name, username, and password to fully use the samples. The sample MySQL database has the format shown in Figure A. Let’s look at how to connect to the sample database with PHP.

Create XML With PHP MySQL

Establishing a database connection with PHP
The following sample PHP script connects to the database and executes a query:

If necessary, you can add attributes later. For now, execute the query like so:

PHP.net is a great source to view all PHP and even MySQL functions.

Create and write the XML

At this point, you’re ready to create a new XML document. There are many ways to approach it, but I think the approach used in Listing A suffices for most purposes:

Here’s a breakdown of what’s happening. Variable num represents the presence of row data from your query, measurable using MySQL’s mysql_num_rows function. This leads us into your procedural output of the XML. Variable $file contains a pointer to the file object produced when PHP successfully reads the file system in search of results.xml. If results.xml is found, your PHP file object, named file, is created and made writeable. Now you can print the contents of a variable to it, which is what you’ll do because your directory permissions are set up to allow PHP to do this.

Keep in mind that for security reasons, this is a dumb thing to do in real-world Web applications. To make sure your implementation of the concepts covered in this article are secure, you should provide a full path to a directory containing the files you wish to open for writing, and make sure it’s in a directory above your Web root.

Next, PHP’s mysql_fetch_array function converts the query variable $result to an array, and loops through its keys. If pageTitle was among the columns returned in the query, for each row returned, some XML-formatted text is written to string variable $_xml.

Note that the operator “.=”, which is being used to append the XML-formatted strings as values, is read from $row. When the loop is finished, the root XML node is printed to variable $_xml, and the whole variable is written to file.xml using PHP’s fwrite function.

At this point, a link appears on the screen. Make sure this link points to the path of your XML file, or you won’t be able to see the formatted XML produced by PHP from your MySQL query.

A test drive

You can easily test the code by running it in a browser. If all goes well, you will see a new file in your directory called results.xml. This is the XML file created with the PHP. It should look like this:

This is just a very basic of XML created with PHP and MySQL. The resulting code from this article would be useful as an include file in the context of a larger application. It is a tried-and-true technique for creating and updating XML documents, but it’s not the only solution: There are other technologies that extend PHP—PEAR, for instance—that present some exciting alternatives.

I would love to hear your experiences of using XML with PHP and MySQL. Please leave us a comment and let us know. Subscribe our RSS to receive latest PHP, MySQL and XML Updates.

RBS WorldPay Integration With PHP

Integrating RBS WorldPay XML Direct Method with PHP

RBS Worldpay XML Direct model enables you to capture payment details on your own secure server and send the captured data to WorldPay for processing in preference to using WorldPay’s hosted payment pages. This connection model provides you with complete control over the look-and-feel of the payment experience for your customers.

This model supports cardholder authentication, so using it you can comply with MasterCard’s mandate that all Maestro payments are processed using MasterCard’s SecureCode security protocol – the legacy ‘invisible’ connection method still used by some of our customers cannot support this.

Unfortunately, you can’t simply test this unless your server is scanned by RBS and you make your site PCI compliant. While working for one of our customer i indulged into RBS worldpay payment gateway and found it too complex to start with but with time things started looking good.

Basically i was never so dependent on PEAR simply because many of my customers run their sites on shared server where we don’t have access to PEAR libraries often. In this particular case i had to use PEAR to make HTTP_REQUESTS.

Steps to connect to RBS Worldpay

1. RBS Worldpay access only HTTP_REQUESTS from an authenticated servers only so make sure you are posting HTTP_REQUEST from a PCI compliant server.
2. Make sure you have PEAR installed on your server with support to Net libraries containing two main files “Socket.php” and “URL.php”.
3. Now make a folder called HTTP and place the following code in Request.php file, so it looks like “HTTP/Request.php”

Now, we should create a test order which will be posted to RBS Worldpay server for authentication and processing payment.

The above code will authenticate, and process the payment in backend. I will write how to recieve payment notifications and responses in my other post.

wordpress Posting Content via XML RPC

WordPress XMLRPC – Posting Content From Outside WordPress Admin Panel

Many WordPress blog owners these days prefer to post their blog content from outside. This facilitates the site owners to post blog posts without even loging into their WordPress admin panel. WordPress supports XMLRPC which can take requests and perform specific operations i.e. ‘Add Post’, ‘Edit Post’, ‘View Recent Posts’ or ‘Save a draft’.

Lets start coding now, but before we go into any details lets ensure that our WordPress installation is allowing XMLRPC (default is turned off). Go to WordPress Admin => Settings => Writing and ensure the XML-RPC is enabled. Click save after checking the enable box. We are good and our WordPress is ready to take XML-RPC commands.

XML-RPC is by default enabled in WordPress 3.5+ that’s cool. On the other hand what if you are not using XMLRPC you can anytime disable it like this. Just place the following line of code in your themes functions.php file and you are all set.

But for the sake of our article, i assume you will not disable XMLRPC.

We will use IXR Library to incorporates both client and server classes, as it is designed to hide as much of the workings of XML-RPC from the user as possible. A key feature of the library is automatic type conversion from PHP types to XML-RPC types and vice-versa. This should enable developers to write web services with very little knowledge of the underlying XML-RPC standard.

Download IXR_Library.php.inc (rename it to IXR_Librabry.php.inc) or download the latest version from Incutio

Add a WordPress Blog Post via php

Get Recent 10 WordPress Blog Posts via php

Edit WordPress Blog Post via php