install sendmail by command: sudo apt-get install sendmail
check the service whether its started or not by execuring follwing command service sendmail status Note: Output of above command should be something – ‘Active: active (running)’
start the service if it is not running by following command service sendmail start
After the service is started, send a test mail using following command: echo "This is test mail body" | mail -s "Test Mail Subject" "recipient@email.com"
Replace email with your email ID and see if you receive this email, if yes, then your mail setup is fine and now your php email should be working fine.
How To Create A Simple REST API in PHP? Step By Step Guide!
Previously, we learned how to create, read, update and delete database records (CRUD operations) with our PHP, MySQL & OOP CRUD Tutorial.
Today, before we go to JavaScript programming, we will learn how to create a simple REST API in PHP. Enjoy our step-by-step tutorial below!
This post covers the following topics:
1.0 Project Overview 1.1 What is REST API? 1.2 Why do we need REST API? 1.3 Where REST API is used? 1.4 REST API in our tutorials
2.0 File Structure
3.0 Setup the Database 3.1 Create Categories Table 3.2 Dump Data For Categories Table 3.3 Products Table 3.4 Dump Data For Products Table 3.5 Connect to database
To define “REST API”, we have to know what is “REST” and what is “API” first. I’ll do my best to explain it in simple terms because REST has a lot of concepts inside of it that could mean a lot of things.
REST stands for “REpresentational State Transfer”. It is a concept or architecture for managing information over the internet. REST concepts are referred to as resources. A representation of a resource must be stateless. It is usually represented by JSON. This post is worth reading: How I Explained REST to My Wife?
API stands for “Application Programming Interface”. It is a set of rules that allows one piece of software application to talk to another. Those “rules” can include create, read, update and delete operations. If you want to learn more, watch the video below and read the musiccritic’s YouTube camera review if you interested on making some videos.
REST API enable your application to cooperate with one or several different applications using REST concepts. If you want to learn more, watch the video below.
1.2 Why do we need REST API?
In many applications, REST API is a need because this is the lightest way to create, read, update or delete information between different applications over the internet or HTTP protocol. This information is presented to the user in an instant especially if you use JavaScript to render the data on a webpage.
1.3 Where REST API is used?
REST API can be used by any application that can connect to the internet. If data from an application can be created, read, updated or deleted using another application, it usually means a REST API is used.
1.4 REST API in our tutorials
A REST API is needed for our JavaScript programming tutorials. This post will help you a lot with that need. Our JavaScript programming tutorials includes the following topics:
But don’t mind those topics for now. We will do it one step at a time. You don’t need to learn all of it as well. Just choose what you need to learn.
Also, please note that this PHP REST API is not yet in its final form. We still have some work to do with .htaccess for better URLs and more.
But one thing is for sure, this source codes is good enough and works for our JavaScript tutorials.
2.0 FILE STRUCTURE
At the end of this tutorial, we will have the following folders and files. ├─ api/ ├─── config/ ├────── core.php – file used for core configuration ├────── database.php – file used for connecting to the database. ├─── objects/ ├────── product.php – contains properties and methods for “product” database queries. ├────── category.php – contains properties and methods for “category” database queries. ├─── product/ ├────── create.php – file that will accept posted product data to be saved to database. ├────── delete.php – file that will accept a product ID to delete a database record. ├────── read.php – file that will output JSON data based from “products” database records. ├────── read_paging.php – file that will output “products” JSON data with pagination. ├────── read_one.php – file that will accept product ID to read a record from the database. ├────── update.php – file that will accept a product ID to update a database record. ├────── search.php – file that will accept keywords parameter to search “products” database. ├─── category/ ├────── read.php – file that will output JSON data based from “categories” database records. ├─── shared/ ├────── utilities.php – file that will return pagination array.
3.0 SETUP THE DATABASE
Using PhpMyAdmin, create a new “api_db” database. Yes, “api_db” is the database name. After that, run the following SQL queries to create new tables with sample data.
3.1 Create Categories Table
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`description` text NOT NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
3.2 Dump Data For Categories Table
INSERT INTO `categories` (`id`, `name`, `description`, `created`, `modified`) VALUES
(1, 'Fashion', 'Category for anything related to fashion.', '2014-06-01 00:35:07', '2014-05-30 17:34:33'),
(2, 'Electronics', 'Gadgets, drones and more.', '2014-06-01 00:35:07', '2014-05-30 17:34:33'),
(3, 'Motors', 'Motor sports and more', '2014-06-01 00:35:07', '2014-05-30 17:34:54'),
(5, 'Movies', 'Movie products.', '0000-00-00 00:00:00', '2016-01-08 13:27:26'),
(6, 'Books', 'Kindle books, audio books and more.', '0000-00-00 00:00:00', '2016-01-08 13:27:47'),
(13, 'Sports', 'Drop into new winter gear.', '2016-01-09 02:24:24', '2016-01-09 01:24:24');
3.3 Products Table
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`description` text NOT NULL,
`price` decimal(10,0) NOT NULL,
`category_id` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=65 ;
Open “objects” folder. Open “product.php” file. The previous section will not work without the following code inside the Product (objects/product.php) class.
I highly recommend completing this whole tutorial first. But if you want to test the code above, you have to use our JavaScript code. The reason is our JavaScript code is designed to work with this REST API.
Open product.php file in /api/objects/ folder. Add the following method inside product class. This method will return a list of records limited to what we set in “$records_per_page” of the previous section.
Still in the product class (product.php file), add the following method. The total rows are needed to build the pagination array. It is included in the ‘paging’ computation.
// used for paging products
publicfunctioncount(){
$query= "SELECT COUNT(*) as total_rows FROM ". $this->table_name . "";
$stmt= $this->conn->prepare( $query);
$stmt->execute();
$row= $stmt->fetch(PDO::FETCH_ASSOC);
return$row['total_rows'];
}
10.5 Get “paging” array
Create “shared” folder. Open “shared” folder and create “utilities.php” file. Open “utilities.php” file and put the following code.
Open “objects” folder. Open “category.php” file. The previous section’s code will not work without the following code inside the “category.php” file. Add the following method inside the “Category” class.
User Guide to begin learning how to build dynamic PHP applications
Demand of PHP is evident from the fact that the world’s top websites, like Facebook, Google, Wikipedia, and YouTube, are using PHP scripts at the backend. PHP is helpful in developing dynamic websites. It is a server-side scripting language that sends information directly to the server when a user submits a form. Before going towards the step-by-step guide on how to write PHP scripts, I will give you a general overview of PHP.
What is PHP?
First introduced by Rasmus Lerdorf, PHP is an open-source, server-side general scripting language that has now become a de-facto coding standard in the web development industry. It can be learned easily, and if one is from a coding background, he (or she) will find it very simple. This is why many are using PHP to polish up their entry-level coding skills.
PHP runs on different operating systems, like Windows, UNIX, Linux and supports different databases like MySQL, Microsoft Access, and Oracle. PHP can not only collect form data, but it can also create, read, write, delete, and close files on the server.
It can be easily embedded in HTML. PHP code is embedded in HTML with tags <?php ?>.
Example
12345678910
<html><title>GettingStartedWithPHP</title><body><?phpecho”Your first PHP code”;?></body></html>
PHP is different from client-side scripting languages. PHP code is executed on the server side resulting in generation of HTML, which is then sent back to the client-side (for e.g., your browser) for execution.
Where to use PHP code?
You can use PHP to create dynamic web pages, collect form data, and send or receive cookies.
Applications of PHP Scripts
Let us see how many ways PHP scripting is used.
Server-Side Scripting
Server side scripting is the first purpose of PHP. All you need to start working on a desktop PC with PHP is a PHP Parser, a webserver (such as Apache) and a web browser like Google Chrome.
Command Line Scripting
If you want to use PHP on Linux or task scheduler on Windows, then you don’t really need a web server, but only a PHP Parser. This is called “command line scripting”.
Desktop Applications
Although, PHP is not a suitable language for development of desktop applications, but it supports some advanced features like PHP-GTK which is basically an extension of PHP. PHP-GTK provides object-oriented user interface.
PHP enables you to choose not only the operating system of your choice but also allows you to have choices to use a web server that you are familiar with. It also enables beginners and professionals to write scripts in their own ways as it allows procedural as well as object-oriented programming.
PHP not only enables you to output HTML but also lets you include images, PDFs, videos, and sounds. PHP can auto-generate XHTML and XML files.
PHP provides support to protocols like LDAP, HTTP, COM, POP3, etc. It also supports WDDX complex data exchange.
Pre-requisites of PHP
Before you start learning PHP, you need to learn some basics of HTML (Hypertext Markup Language), SS(Cascading Style Sheets) and Javascript.
How to install PHP
Before starting PHP, you need a web host with PHP and MYSQL. For this, you should also install a web server such as Apache. To do it locally on your PC, you may download XAMPP directly from Apache Friends.
Installation of Apache, PHP, MySQL, and PHPMyAdmin
In order to install PHP, MySQL, PHPMyAdmin and Apache in a single attempt, XAMPP should be installed.
Scroll over to XAMPP for Windows and download should begin shortly.
Click the .exe file to start the installation procedure.
Select the components which you want to install and click “Next”.
In the components area, you can view several options. As a beginner, you don’t need all of them. You need to install Apache, which is a very famous web server. It manages client responses. For data storage and view, you need a database such as MySQL. Filezilla FTP server option is not needed for performing operations at localhost. Next option is the Mercury Mail Server option. Its primary function is to deal with emails received by the server. It is needed to enable the flow of emails, which is not a requirement at the moment. Tomcat is also a web server owned by Apache.
Coming down to programming languages, PERL (which is also a high-level programming language) is not a need at the moment. PhpMyAdmin is the admin panel of database and is needed. Webalizer is an application for analysis and you need to install it for monitoring purposes. Fake Sendmail is also an application that will be explained later.
Select your desired location, where you want to install XAMPP and then click “Next”.
Click “Next” on the coming screens to proceed with the installation process.
Now, you will see the final screen. I would suggest that you keep the “start the Control Panel” option checked. Click “Finish” to complete the installation process. A new window will open shortly.
The XAMPP Control Panel has now started. Now, click “Start” button in Apache and MySQL rows to begin.
You are now ready to start writing the code. Now all you need is an editor like Notepad++ or Dreamweaver to write the code.
After downloading Notepad++, you can start writing your code
<?php echo “My first PHP Script”; ?>
Now, save the page as “test.php” in htdocs folder and click “Save” button.
Now, open a web browser and type localhost in the address bar. It will automatically open the index file but if you type localhost/test.php, it will open the page that we have saved.
Consider another example.
<!DOCTYPE html> <html> <head> <title>Getting Started With PHP</title> </head> <body> <h1>Beginners Guide For PHP</h1> <p>Tutorial Series For Learning PHP</p> <?php echo “2+3″.”<br/>”;//It will display the output 2+3 print “2+3”;// print will also display the output 2+3 ?> </body> </html>
In this example, we use echo and print to show the same result. Here is the output we get.
You can see that the two lines of 2+3 are displayed as output by using different statements. Most of the professional programmers prefer to use echo because echo can bring up multiple strings or values at the same time, whereas print displays one statement at a time. Both echo and print can be used with or without parentheses; print() or echo(). Also, it is to be noticed that you can not see the sum of two numbers without using variables. The concept of variables will be introduced along with PHP data types in the next tutorial.
Consider the example below.
<!DOCTYPE html> <html> <head> <title>Getting Started With PHP</title> </head> <body> <h1>Beginners Guide For PHP</h1> <p>Tutorial Series For Learning PHP</p> <?php $a=99; $b=”Calculus”; echo “Numbers you have got in $b are $a”.”<br/>”; echo ‘Numbers you have got in $b are $a’; ?> </body> </html>
In this example, you can see that we have echoed the same string with double quotes and single quotes. Here is the output.
When we use double quotes, it displays the string along with the values assigned to variables $a and $b. However, when we use single quotes, it will treat the whole statement as string and will display variables $a and $b. I will touch upon the concept of variables in detail in the next tutorial as well.
For now, congratulations! You have just executed your very first PHP scripts! In the upcoming weeks, I will be discussing more about PHP; from the most basic tutorials to the most advanced. I hope to see you around for more PHP tutorials.
In the meanwhile, you can sign up and deploy PHP on the revolutionary managed Cloud Hosting Platform. Choose your cloud provider from some of the best infrastructures around, namely Google Compute Engine, DigitalOcean and Amazon Web Services. It will take you less than 6 minutes to sign up, choose the cloud provider and deploy PHP on your selected cloud provider. It is fast and secure. Plus, you are always covered with a 24/7 support team that never keeps you at bay!
ooking for Best PHP based eCommerce Shopping Carts? Following is a list of 10 most popular and widely used PHP based shopping cart systems, softwares which helps you get started in very short time. Magento is the market leader in all PHP based shopping carts.
Most Popular PHP eCommerce Shopping Carts
Following is a list of PHP based eCommerce shopping carts in no particular order.
Magento Templates
Magento eCommerce holds the largest market share in all php based eCommerce shopping carts (softwares). Magento eCommerce Templates and Themes are the bestsellers in the eCommerce world. Magento powered online stores provide your customers with a rich shopping experience and a very user friendly flow from shopping to checkout.
ZenCart Templates
Zen-cart ecommerce templates engine is the oldest player of the e-Commerce market and offer you to start your own online business without any problem. Zen cart e-Commerce store is very user-friendly software and can be easily installed by the users with little experience.
OpenCart Templates
Opencart eCommerce platform that gets more and more popular all over the world, It is affordable and easy to configure with cleaner html, pure css, sliders, image galleries, and much more.
WordPress E-commerce
WordPress E-commerce Templates are clean, super flexible and has a fully responsive design along with high quality plugins for creating WP commerce sites. WordPress commerce themes are easy to configure and offers integration options with some of the most popular and free e-commerce plugins.
PrestaShop Templates
The Shopping – eCommerce Prestashop Template is an open source eCommerce tool very convenient and easy to handle. The incredible administrative interface of Prestashop helps you to manage the complex inventory easily and offers features like Import and export quickly, set attributes, sort products, bulk discounts, and much more.
Joomla Shopping Cart
The Joomla Shopping Cart system is rich and user friendly eCommerce & Shopping Cart tool with full-featured e-commerce component with an easy to use and visually appealing interface. Joomla is also known as one of the best content management systems in the market.
Drupal e-Commerce
Drupal Commerce is an open source e-Commerce framework build truly flexible, It has a solid collection of E-commerce modules and themes that are ready to convert a site to online store.
CubeCart Templates
The CubeCart Templates are packed full of features and beautifully presented with a clean layout and offers high-quality E-Commerce theme and skins.
CS-Cart Templates
CS-Cart templates and skins offers on-line store a professional look and feel with very elegant and functional web design templates.cs-cart e-commerce shopping cart is very easy to install and template management system from user-friendly admin area.
OS-Commerce
OS-Commerce or open source commerce is an e-commerce online shop e-commerce solution and online store-management software program that offers a wide range of out-of-the-box features that allows online stores to be setup quickly.
Company Description: Traitgain is one of the leading Recruitment firm in Noida– they have own clientele in noida and in different different state.Traitgain also provide certification in the field of HR and and training for IT student