{"id":1509,"date":"2022-08-31T07:41:57","date_gmt":"2022-08-31T07:41:57","guid":{"rendered":"https:\/\/beproblemsolver.com\/?p=1509"},"modified":"2022-10-14T17:00:31","modified_gmt":"2022-10-14T17:00:31","slug":"develop-an-api-with-php-json-and-postman","status":"publish","type":"post","link":"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/","title":{"rendered":"How to develop an API with PHP, JSON, and POSTMAN in 9 Steps"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Introduction\" >Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#What_is_an_API\" >What is an API?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Why_should_you_develop_an_API_with_PHP\" >Why should you develop an API with PHP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#What_is_JSON\" >What is JSON?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#What_is_POSTMAN\" >What is POSTMAN?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#The_process_to_develop_an_API\" >The process to develop an API<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Establish_Database\" >Establish Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Provide_Data_in_Database\" >Provide Data in Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Connect_Database_with_PHP\" >Connect Database with PHP<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Build_API_%E2%80%93_GET_Request\" >Build API &#8211; GET Request<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Test_API_with_POSTMAN\" >Test API with POSTMAN<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Create_API_%E2%80%93_GET_Single_data\" >Create API &#8211; GET Single data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Develop_an_API_%E2%80%93_POST_Request_for_Insert_Update\" >Develop an API &#8211; POST Request for Insert &amp; Update<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Build_API_%E2%80%93_DELETE_Request\" >Build API &#8211; DELETE Request<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Search_API_%E2%80%93_GET_Request\" >Search API &#8211; GET Request<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/beproblemsolver.com\/develop-an-api-with-php-json-and-postman\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Do you want to develop an API with PHP, JSON, and POSTMAN?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Or are you simply looking for a way to build API for your Android App or IOS app?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Look no further!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, I will show you how to do just that!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll go over the basics of each technology, we are using. Then dive into how to use them together to create API for your need. By the end of this tutorial, you&#8217;ll be able to create your API with <strong>PHP<\/strong> and <strong><a href=\"https:\/\/www.json.org\/\" data-type=\"URL\" data-id=\"https:\/\/www.json.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">JSON<\/a><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving in, might I recommend a fantastic free editor that I love to use? <a href=\"https:\/\/beproblemsolver.com\/why-vs-code-editor-in-2022\/\" data-type=\"URL\" data-id=\"https:\/\/beproblemsolver.com\/why-vs-code-editor-in-2022\/\">Check out this post for more details.<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_an_API\"><\/span>What is an API?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So you want to build API or maybe multiple APIs?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, let&#8217;s understand what is an API and why we rely on them in modern web app development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The official definition you will find is: API stands for <strong>Application Programming Interface<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What does that mean?\ud83d\ude11<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In simple words, <em>without techy jargon<\/em>, an <a href=\"https:\/\/en.wikipedia.org\/wiki\/API\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/API\" target=\"_blank\" rel=\"noreferrer noopener\">API<\/a> is a piece of code you write, that allows <strong>two different pieces of software to communicate with each other<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>API is just way to allow two different software to connect.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Why use API though? Simple. Have you ever used an android app where you are fetching and sending data most likely you have? Then you have already used an API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yeah! API is everywhere on the internet!\ud83e\udd2f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With an API you connect databases like MySQL or <a href=\"https:\/\/www.mongodb.com\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.mongodb.com\/\" rel=\"noreferrer noopener\">MongoDB<\/a> (or any other) to any frontend interface as an Android app or Web app. And foremost, APIs can be incredibly fast and easy to use.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"570\" src=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/What-is-an-API-and-How-it-works-1.jpg\" alt=\"Learning how API works before developing an API is crucial to the understanding of the API build process and working.\" class=\"wp-image-1643\" srcset=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/What-is-an-API-and-How-it-works-1.jpg 1200w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/What-is-an-API-and-How-it-works-1-300x143.jpg 300w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/What-is-an-API-and-How-it-works-1-1024x486.jpg 1024w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/What-is-an-API-and-How-it-works-1-768x365.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption>Learning how API works before developing an API is crucial<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So you understand the basics of API and how it works behind the scene. Let&#8217;s jump into the technology we are going to use in our API build.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_should_you_develop_an_API_with_PHP\"><\/span>Why should you develop an API with PHP?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While there are many languages &amp; frameworks for creating APIs, we are going to use <strong>PHP<\/strong> for this purpose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But why?\ud83e\udd28<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let me answer the question in detail:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PHP-based APIs are extremely easy to deploy. You <strong>don&#8217;t need to host the API on a separate physical<\/strong> server like Java or C. Which reduces overall cost.<\/li><li>Despite being free, PHP is very powerful and you can use it to create very complex APIs.<\/li><li>Even if you haven&#8217;t ever coded a programming language, <strong>PHP is easier to understand and learn<\/strong>. Remember HTML and CSS are structuring\/styling languages, not programming ones.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So now you know why I chose PHP as the backbone of my API. Let&#8217;s look at other technology and tool we need to build API.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_JSON\"><\/span>What is JSON?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you ever worked with App Developer, as I have, you will love how much they appreciate the <strong>JSON<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Why?<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I know I am very fond of <strong><em>why question<\/em><\/strong>. But it&#8217;s vital to understand the reason behind why JSON dominates the API building market. Let me explain:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>JSON, as you must know, stands for <strong>JavaScript Object Notation<\/strong>.<\/li><li>JSON <strong>is not JavaScript<\/strong>. Please be careful not to confuse them.\ud83d\ude21<\/li><li>Yes, it looks similar to JavaScript objects but that&#8217;s because JSON shares a common codebase with ECMAScript, C++, and even C. But<strong> JSON is an<\/strong> <strong>independent and complete language<\/strong> in itself.<\/li><li>It&#8217;s <em>an<\/em> ultra-lightweight data-interchange format.<\/li><li>It&#8217;s language-independent, platform-independent, and most importantly, developer-friendly. The only language that comes close to competing is XML but it has its <em>issues<\/em>.<\/li><li>JSON data can be written in any editor, sent over the network, and parsed by any platform. It may not seem that important in a modern environment where we have high-processing systems available. But in certain situations, JSON <strong>adaptability is a lifesaver<\/strong>. \ud83d\ude07<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">See how vital JSON is for our process to create API. Let&#8217;s jump to the last tool we need to get started.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_POSTMAN\"><\/span>What is POSTMAN?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before you joke, no I am not talking about the Postman who delivers letters. \ud83e\udd23<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Our POSTMAN is an extremely powerful and necessary tool for testing and resolving bugs with our create API in PHP process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So is it an absolute necessity? The answer would be <strong>No<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But other ways of testing your build API process are too time-consuming and not much accurate. POSTMAN is the tool all professionals rely upon.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In past, POSTMAN used to be a browser extension but now it&#8217;s a full-fledged software you can <a href=\"https:\/\/www.postman.com\/downloads\/\" data-type=\"URL\" data-id=\"https:\/\/www.postman.com\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener\">download<\/a>. We only need the free version for our needs.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"486\" src=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/POSTMAN-for-API-build-and-test-process-1024x486.jpg\" alt=\"POSTMAN is the best tool to build API and test it. We will use POSTMAN to test the API we create in PHP.\" class=\"wp-image-1683\" srcset=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/POSTMAN-for-API-build-and-test-process-1024x486.jpg 1024w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/POSTMAN-for-API-build-and-test-process-300x143.jpg 300w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/POSTMAN-for-API-build-and-test-process-768x365.jpg 768w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/POSTMAN-for-API-build-and-test-process.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>POSTMAN is the best tool to build API and test it.<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_process_to_develop_an_API\"><\/span>The process to develop an API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have acquainted with all the tools we will need. Let&#8217;s jump into the process of building API:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Establish_Database\"><\/span>Establish Database<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We build the database first. I will name my database &#8220;<strong>students<\/strong>&#8220;. Feel free to name your database anything. But keep a note of it as you will need it later on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we create a table &#8220;<strong>studentdata<\/strong>&#8221; which will hold our data. To make the table and its field quickly. Run the below SQL command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE `studentdata` (\n  `id` int(11) NOT NULL,\n  `name` varchar(50) NOT NULL,\n  `age` int(5) NOT NULL,\n  `city` varchar(50) NOT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you don&#8217;t know how to run SQL commands in PHPMyAdmin. Read my <a href=\"https:\/\/beproblemsolver.com\/user-login-in-php-with-session\/\" data-type=\"URL\" data-id=\"https:\/\/beproblemsolver.com\/user-login-in-php-with-session\/\">previous article<\/a>, I&#8217;ve explained there how to run SQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now your database has a table and looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"486\" src=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Build-API-database-1024x486.jpg\" alt=\"Process to build API database in phpMyAdmin\" class=\"wp-image-1709\" srcset=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Build-API-database-1024x486.jpg 1024w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Build-API-database-300x143.jpg 300w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Build-API-database-768x365.jpg 768w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Build-API-database.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>The process to build API database in phpMyAdmin.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Provide_Data_in_Database\"><\/span>Provide Data in Database<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have a database and a table let us insert some data by SQL <em>for now<\/em>. <strong>Don&#8217;t worry<\/strong> we will create a process in PHP to insert data through our API. But that comes later.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">INSERT INTO `studentdata` (`id`, `name`, `age`, `city`) VALUES\n(2, 'Suresh', 26, 'Chennai'),\n(3, 'Abhimanyu', 19, 'Kolkata'),\n(4, 'Raj', 25, 'Kanpur'),\n(5, 'Subash', 27, 'Bhopal'),\n(7, 'Amir', 35, 'Imphal');<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For now, we have some data in our database that we can interact with, when we build our API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Connect_Database_with_PHP\"><\/span>Connect Database with PHP<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now we connect to our database with PHP. For this purpose, I created the &#8220;<strong>connect.php<\/strong>&#8221; file. Remember to PHP create API JSON, we need a database to handle our data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> &#8220;<strong>.php<\/strong>&#8221; is our extension for PHP files.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$conn = mysqli_connect('localhost', 'root', '', 'students');\n\nif (!$conn) {\n  die(\"Connection failed: \" . mysqli_connect_error());\n}\n?><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Build_API_%E2%80%93_GET_Request\"><\/span>Build API &#8211; GET Request<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now we start coding with PHP. So bring your favorite code editor as I started up with mine. Let&#8217;s write the first API file that will <strong>fetch everything<\/strong> from our database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Filename:<\/em> &#8220;<strong>api_fetch_all.php<\/strong>&#8220;.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nheader('Content-Type: application\/json');\nheader('Access-Control-Allow-Origin: *');\n\ninclude 'connect.php';\n\n$sql = \"select * from studentdata\";\n$result = mysqli_query($conn, $sql);\n\nif (mysqli_num_rows($result) > 0) {\n\t\/\/mysqli_fetch_all gives us the data in 2D array format.\n\t\/\/ It's second parameter decide whether its assoc array or indexed. Or maybe both\n\t$data = mysqli_fetch_all($result, MYSQLI_ASSOC);\n\n\techo json_encode($data);\n} else {\n\techo json_encode(['msg' => 'No Data!', 'status' => false]);\n}\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let me explain what we wrote and why:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Our first line <strong>&#8220;<\/strong><em>header(&#8216;Content-Type: application\/json&#8217;)<\/em><strong>&#8220;<\/strong> is telling our file that we are using the JSON data format to exchange data. <strong>Without this line of code<\/strong>, our API will never work.<\/li><li>The next header line is simply allowing our API file to access data. Sometimes you will see developers skip this line. And sometimes that&#8217;s okay. But for the best result and to avoid any future error, we did define it.<\/li><li>Now, we ran the standard PHP code to select all data from the database. Then fetched that data and stored it in our &#8220;$data&#8221; variable.<\/li><li>Using the famous PHP method &#8220;<strong><a href=\"https:\/\/www.php.net\/manual\/en\/function.json-encode.php\" data-type=\"URL\" data-id=\"https:\/\/www.php.net\/manual\/en\/function.json-encode.php\" target=\"_blank\" rel=\"noreferrer noopener\">json_encode()<\/a><\/strong>&#8220;, we convert our data into JSON format. And &#8220;<strong>echo<\/strong>&#8221; this JSON data.<\/li><li>Lastly, we encoded an error msg and status, just in case our data failed to be obtained.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If you have reached this point. <strong>Congratulations!<\/strong> \ud83d\ude0e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You are halfway done with learning the basics of how to create API in PHP. We have also built our first API file so let us test it with POSTMAN.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Test_API_with_POSTMAN\"><\/span>Test API with POSTMAN<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Testing API with POSTMAN might seem daunting at first. But no worries, I will guide you through that process.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Open your POSTMAN app and click the &#8220;<strong>+<\/strong>&#8221; icon where it says &#8220;<strong>Untitled Request<\/strong>&#8220;.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/3-1024x375.png\" alt=\"\" class=\"wp-image-1775\" width=\"820\" height=\"300\" srcset=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/3-1024x375.png 1024w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/3-300x110.png 300w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/3-768x281.png 768w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/3.png 1170w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><figcaption>Open first your &#8220;Untitled Request&#8221;<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Enter your API URL in the &#8220;<strong>URL-send<\/strong>&#8221; and set it to &#8220;GET&#8221;<\/li><li> In the lower tab choose the header section. Enter header to content-type: application\/JSON. Then hit Enter button.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"486\" src=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Using-POSTMAN-to-test-build-API-process-1024x486.jpg\" alt=\"\" class=\"wp-image-1784\" srcset=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Using-POSTMAN-to-test-build-API-process-1024x486.jpg 1024w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Using-POSTMAN-to-test-build-API-process-300x143.jpg 300w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Using-POSTMAN-to-test-build-API-process-768x365.jpg 768w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Using-POSTMAN-to-test-build-API-process.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>If done everything, you will see the JSON response in the downward part.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now we have completed our first API. This API will fetch all data for us from the database in JSON format. Let&#8217;s continue the rest of the process of developing an API.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_API_%E2%80%93_GET_Single_data\"><\/span>Create API &#8211; GET Single data<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s code our API file which will fetch single data by id as reference.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Filename:<\/em> <strong>api_fetch_single.php<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nheader('Content-Type: application\/json');\nheader('Access-Control-Allow-Origin: *');\n\n\/\/ This is the data which is being sent to request a single data.\n\/\/ Since we know this request will be in json format we must decode before passing it into our php logic\n\/\/ file_get_contents is reading file into a string. \n\/\/ \"php:\/\/input\" is special key which insure that we can get any format of data. Even if its raw data.\n\/\/ True argument of json decode makes sure that we get response in assoc array\n$data = json_decode(file_get_contents(\"php:\/\/input\"), true);\n\n\/\/ we are getting our passed data in $data array. But to ensure securty we will change name of id to sid. Remember we are being requested data on basis of sid after all.\n$student_id = $data['sid'];\n\ninclude 'connect.php';\n\n$sql = \"select * from studentdata where id = $student_id\";\n$result = mysqli_query($conn, $sql);\n\nif (mysqli_num_rows($result) > 0) {\n\t\/\/mysqli_fetch_all gives us the data in 2D array format.\n\t\/\/ It's second parameter decide whether its assoc array or indexed. Or maybe both\n\t$data = mysqli_fetch_all($result, MYSQLI_ASSOC);\n\n\techo json_encode($data);\n} else {\n\techo json_encode(['msg' => 'No Data!', 'status' => false]);\n}\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike the previous API where we were fetching every data in the table, now we are targeting a specific one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, we provide code &#8220;<strong>file_get_contents<\/strong>&#8221; which allows our reading file into a string. And &#8220;<strong>php:\/\/input<\/strong>&#8221; is a special key that ensures that we can get any format of data. Even if it&#8217;s raw data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, we will create API files for POST and DELETE requests too.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Develop_an_API_%E2%80%93_POST_Request_for_Insert_Update\"><\/span>Develop an API &#8211; POST Request for Insert &amp; Update<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, let&#8217;s learn how to build a POST request API that will insert our data into the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Filename:<\/em> <strong>api_insert.php<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nheader('Content-Type: application\/json');\nheader('Access-Control-Allow-Origin: *');\nheader('Access-Control-Allow-Methods: POST');\nheader('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Methods, Access-Control-Allow-Headers, Authorization, X-Requested-With');\n\n\/\/ Since we are inserting data we pass two extra headers.\n\/\/ 1st allow us to set the method of insert. i.e. POST in rest api\n\/\/ 2nd determines which type of headers can be sent. It's a secuirty header.\n\/\/ 'Authorization' is set for authorizing insert data. While 'X-Requested-With' is set for passing data as json\n\n$data = json_decode(file_get_contents(\"php:\/\/input\"), true);\n\n$sname = $data['sname'];\n$sage = $data['sage'];\n$scity = $data['scity'];\n\ninclude 'connect.php';\n\n$sql = \"insert into studentdata (name, age, city) values ('$sname', '$sage', '$scity')\";\n\nif (mysqli_query($conn, $sql)) {\n\techo json_encode(['msg' => 'Data Inserted Successfully!', 'status' => true]);\n} else {\n\techo json_encode(['msg' => 'Data Failed to be Inserted!', 'status' => false]);\n}\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we are up for Update Requests. Which again will be done with POST Request but with little modifications. \ud83e\uddd0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Filename:<\/em> <strong>api_update.php<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nheader('Content-Type: application\/json');\nheader('Access-Control-Allow-Origin: *');\nheader('Access-Control-Allow-Methods: PUT');\nheader('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Methods, Access-Control-Allow-Headers, Authorization, X-Requested-With');\n\n\/\/ To update is similar to insert. Except in rest api update request is done throough PUT method\n\n$data = json_decode(file_get_contents(\"php:\/\/input\"), true);\n\n$sid = $data['sid'];\n$sname = $data['sname'];\n$sage = $data['sage'];\n$scity = $data['scity'];\n\ninclude 'connect.php';\n\n$sql = \"update studentdata set name = '$sname', age = '$sage', city = '$scity' where id = '$sid'\";\n\nif (mysqli_query($conn, $sql)) {\n\techo json_encode(['msg' => 'Data Updated Successfully!', 'status' => true]);\n} else {\n\techo json_encode(['msg' => 'Data Failed to be Updated!', 'status' => false]);\n}\n\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Remember while updating, we are providing <em>a specific id<\/em> with our &#8220;<strong>update-data<\/strong>&#8220;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Last we have only the Delete and Search API part left. Let&#8217;s keep going &#8230;\ud83d\udcbb<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Build_API_%E2%80%93_DELETE_Request\"><\/span>Build API &#8211; DELETE Request<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When writing <strong>Delete Request<\/strong>, we add two new headers type.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>header(&#8216;Access-Control-Allow-Methods: DELETE&#8217;)<\/em>  &#8211; allow us to set our API to delete files as needed. Without it, you will run into errors. <\/li><li>Next headers allow us to give more control of files to API queries. It&#8217;s recommended to add them.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Filename:<\/em> <strong>api_delete.php<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nheader('Content-Type: application\/json');\nheader('Access-Control-Allow-Origin: *');\nheader('Access-Control-Allow-Methods: DELETE');\nheader('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Methods, Access-Control-Allow-Headers, Authorization, X-Requested-With');\n\n\/\/ To delete in rest api, we use DELETE method\n$data = json_decode(file_get_contents(\"php:\/\/input\"), true);\n\n$sid = $data['sid'];\n\ninclude 'connect.php';\n$sql = \"delete from studentdata where id = '$sid'\";\n\nif (mysqli_query($conn, $sql)) {\n\techo json_encode(['msg' => 'Data Deleted Successfully!', 'status' => true]);\n} else {\n\techo json_encode(['msg' => 'Data Failed to be Deleted!', 'status' => false]);\n}\n\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Before finalizing, do test your API with POSTMAN.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And now let&#8217;s move to the last API file of the day. It&#8217;s quite an important one though, so stay alert. Also if you want to link your <a href=\"https:\/\/beproblemsolver.com\/html-form-to-google-sheet\/\" data-type=\"URL\" data-id=\"https:\/\/beproblemsolver.com\/html-form-to-google-sheet\/\">HTML form to Google Spreadsheet<\/a>, then check out this post.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Search_API_%E2%80%93_GET_Request\"><\/span>Search API &#8211; GET Request<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Filename:<\/em> <strong>api_search.php<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nheader('Content-Type: application\/json');\nheader('Access-Control-Allow-Origin: *');\nheader('Access-Control-Allow-Methods: POST');\n\n$data = json_decode(file_get_contents(\"php:\/\/input\"), true);\n\n$searchterm = $data['search'];\ninclude 'connect.php';\n\n$sql = \"select * from studentdata where name like '%$searchterm%'\";\n$result = mysqli_query($conn, $sql);\n\nif (mysqli_num_rows($result) > 0) {\n\t$data = mysqli_fetch_all($result, MYSQLI_ASSOC);\n\n\techo json_encode($data);\n} else {\t\n\techo json_encode(['msg' => 'No Data Found to search query!', 'status' => false]);\n}\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">See what your search should look like in the below image:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"570\" src=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Search-API-\u2013-GET-or-POST-Request-Method.jpg\" alt=\"Steps to test Search api file development in build an api process with PHP and JSON\" class=\"wp-image-1912\" srcset=\"https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Search-API-\u2013-GET-or-POST-Request-Method.jpg 1200w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Search-API-\u2013-GET-or-POST-Request-Method-300x143.jpg 300w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Search-API-\u2013-GET-or-POST-Request-Method-1024x486.jpg 1024w, https:\/\/beproblemsolver.com\/wp-content\/uploads\/2022\/08\/Search-API-\u2013-GET-or-POST-Request-Method-768x365.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption>Search API with GET\/POST Request<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Search functionality in an API can be developed by either <strong>GET<\/strong> or <strong>POST<\/strong> request method. In <strong>GET method<\/strong> case we pass our search values to the URL. In the <strong>POST<\/strong> <strong>method, <\/strong>we use the normal JSON input. So you see there is nothing different about a PHP create API JSON.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So there you have it! \ud83e\udd73<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A quick and easy guide on how to develop an API with PHP, JSON, and POSTMAN. I hope this has been helpful for you. With this detailed guide at your side, we at Be Problem Solver believe you feel more confident to develop APIs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In our next posts, we show you <a href=\"https:\/\/beproblemsolver.com\/learn-how-to-use-api-with-2-api-examples\/\" data-type=\"URL\" data-id=\"https:\/\/beproblemsolver.com\/learn-how-to-use-api-with-2-api-examples\/\">how to use this API and other third-party APIs<\/a> in projects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have any questions or comments, then feel free to reach out. And do check out my other post about adding jazz to your website with <a href=\"https:\/\/beproblemsolver.com\/6-methods-to-use-page-scroll-animation\/\" data-type=\"URL\" data-id=\"https:\/\/beproblemsolver.com\/6-methods-to-use-page-scroll-animation\/\">page scroll animations<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ta-da! Guys and Gals! Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wanted to develop an API but didn&#8217;t know where to start? In this guide, we&#8217;ll show you how to develop an API with PHP, JSON, and POSTMAN. We&#8217;ll also tips and tricks on how to use and test it. Read on to learn more.<\/p>\n","protected":false},"author":1,"featured_media":1529,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAow6-e6DA:productID":"","footnotes":""},"categories":[15,56,16,14],"tags":[51,54,52,7,6,55,53,35],"class_list":["post-1509","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-api","category-mysql","category-web-development","tag-api","tag-api-development","tag-json","tag-mysql","tag-php","tag-php-api","tag-postman","tag-web-development"],"_links":{"self":[{"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/posts\/1509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/comments?post=1509"}],"version-history":[{"count":0,"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/posts\/1509\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/media\/1529"}],"wp:attachment":[{"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/media?parent=1509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/categories?post=1509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beproblemsolver.com\/wp-json\/wp\/v2\/tags?post=1509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}