المشاركات

عرض الرسائل ذات التصنيف json

How to Post JSON Data with PHP cURL & Receive It?

صورة
Hi! Here let's see how to post json data with php curl and receive the raw json sent . You can simply use PHP file_get_contents() to send HTTP GET request . But it's not the same case with HTTP POST. Fortunately PHP cURL makes the process very simple. With curl you can easily send HTTP requests. In case you don't know, 'cURL' is a library that let you communicate with web servers using different protocols such as http, https, ftp, telnet, file etc. To use curl, you must install libcurl package on your server according to the version of PHP you use. In case you are not sure if curl is enabled on your machine or not, use this code to check it. <?php echo (is_callable('curl_init') ? 'cURL is enabled' : 'cURL is disabled'); ?> How to Post JSON with PHP cURL? In order to send HTTP POST request with JSON data, you have to follow some steps and they are, Format JSON data correctly Attach JSON to the body of the POST request Set th...

How to Export MySQL Table to JSON File in CodeIgniter

صورة
Hi! In this tutorial, we will see how to export mysql table to json file in codeigniter . Data portability has become more important than ever and a necessary requirement for modern applications. Almost all web services uses JSON format to migrate data from one location to another. So, as a Web Developer, you must know to handle json and various types of applications like Web, Mobile etc. Exporting mysql into json allows you to easily port data to different platforms. The whole process is quite simple. Just fetch the data from mysql database, covert the query result to json and then write it in a file. CodeIgniter - Export MySQL Table to JSON File: Let's create a demo for exporting the database as json. The steps are easy to follow and we possibly need two files, a model and a controller. Step-1) Create Database We need a dummy database to use in the example, a database, a table and some sample records. Run the below sql file in the mysql environment and create them. ...

How to Merge Two JSON Strings into One with PHP

صورة
Hi! In this tutorial we will see how to merge two json strings into one in php . People often mistakes JSON for object or array but it's the string representation of a JavaScript object. In a recent project, I wanted to combine two jsons. That is, I have to make two API calls and combine their results into one. It is a rest api and returns the response as json. The task is simpler than I thought. It employs a similar technique I used here earlier when dealing with json. Let me show you how to do it. PHP - Merging Two JSON into One: If you are new to working with JSON, don't worry. PHP offers native functions to handle it easily. The idea is to decode the json data, write it to an array and encode it back to json. Below are the steps involved in the process. STEP-1) Let's take two different jsons containing couple of employee details. <?php $json1 = '{"id": "GD01", "name": "Garrett Davidson", "position...

How to Get Country Name from IP Address with PHP

صورة
Hi! In this post, let's see how to get country name from ip address using php . When working on multilingual web applications, it is mandatory to find the geographic location of the visitors, so that you can provide the contents that match the specific location. Some websites even change country flags and themes accordingly. With PHP, you can get the visitor's IP easily, but unfortunately there's no way to get the country details from it. Hence, you have to rely on external web services to provide the location details. Here I'm going to show you how to find the country of visitors with the help of GeoPlugin API and PHP. Getting Country Name from IP Address with GeoPlugin: GeoPlugin is a free web service that provides geographical location of site visitors from IP. It offers various types of services like PHP, JSON etc., and in this tutorial, I'm going to use the JSON API to find the country name . You have to access the service with this url, http://ww...

Get JSON from URL in PHP

صورة
In this tutorial, I’m going to show you how to get json from url in php script . JSON has become a popular way to exchange data and web services outputs in json format. To send a HTTP request and parse JSON response from URL is fairly simple in php but newbies may find how to parse json difficult. Let's see how to build a php json parser script. For this script, I’m going to access Google MAP web service via API and get latitude and longitude co-ordinates for a location. Google map api produces both json/xml output. But for this example I’m going to get the json response and show you how to parse json object to retrieve the geo-metric details. Read more »

How to Read and Parse JSON String in jQuery and Display in HTML Table

صورة
Hi, in today’s post we’ll see How to Read and Parse JSON String in jQuery. JSON is a human readable data exchange format used among web services and it's a necessary skill for modern day web developers (frontend/backend) to deal with JSON data received from various third-party APIs. Keeping that in mind we have discussed about parsing json data using php script sometime back. And now we have come up with this jquery tutorial which walks you through the process of parsing json string using jquery library, convert and display it into an html table. How to Read and Parse JSON String in jQuery? jQuery provides several JSON methods like “getJSON”, “parseJSON”, “stringify” and for this post we’ll pickout and use the method “parseJSON” , which parses through well formed json string and returns javascript object. JSON.parseJSON(JSON STRING) The function parseJSON() takes the string parameter and returns String, Number, Object, Array or Boolean. Using this jQuery method we can ...

How to Convert JSON Data to HTML Table using jQuery DataTables Plug-in

صورة
Hi, this time I have come up with a jquery basics tutorial which discusses about how to convert json to html table. In simple terms we are going to use jquery to display data from a json file in neat table format with added pagination and search options to it. To make our life easier there is an excellent jquery plug-in called ‘DataTables’ which we will use for this task. DataTables is a flexible jquery tool which adds up advanced interaction controls to any HTML table. Using this we can easily add pagination, instant search or multi-column ordering to the html table. Moreover it supports all modern day data sources like DOM, JavaScript, Ajax and server-side processing. Convert JSON Data to HTML Table using jQuery As an example let’s take the below sample json file which contains an array of objects and display it in html table using datatables. Don't Miss: How to Create Responsive Carousel Image Slider Also Read: How to Create Image Lightbox Effect with jQuery empda...

How to Validate JSON String in PHP

صورة
Hi, we'll see how to validate json string in this PHP tutorial. JSON is a light-weight data exchange format among web services and it is very human readable. At times it is required to check if the json output you received is a valid one. PHP programming language does not provide any direct method to validate a json string but sure there is some workaround to determine if a string is a valid json format or not. You need PHP version 5.3 or above for this to work out. There are 4 built-in functions provided by PHP to deal with json format namely json_encode(), json_decode(), json_last_error_msg() and json_last_error(). The methods json_encode() and json_decode() are used to encode and decode json format and the working example of them are already covered in this blog. The other two methods json_last_error_msg() and json_last_error() should be used in conjunction with the json encode or decode methods. How to Validate JSON String in PHP? The php function json_last_erro...

How to Merge Two JSON Objects into One using jQuery

صورة
Hi, at times you may want to merge multiple json objects into a single entity when working with different APIs. Recently I also found myself in such situation and came across a very simple and effective solution with jQuery. In this tutorial, I'll show you how to merge two json objects into one using jQuery library. jQuery provides several utility functions and the extend() method is one among them. It merges two or more objects together into the first object (i.e., first parameter passed to the function). Merging Two JSON Objects using jQuery In order to merge the json objects we have to use jQuery's extend() function which takes up multiple parameters. Here is the syntax for the method. jQuery.extend([deep], target, object1 [, objectN]) Where 'deep' when set as TRUE, makes the merging process recursive (optional). 'target' is the object which consists of the merged contents. 'object1' is the first object to be merged. 'objectN' ...

How to Remove a Property from an Object in JavaScript

صورة
JSON alias 'JavaScript Object Notation' is the string representation of JavaScript Object and is a popular data-exchange format among web-services. Manipulating those data received from various source is an essential skill required for every developer. With that in mind we have published several json tutorials in the past which discusses about working with json data. And today in this tutorial, we'll see how to remove a property from an object in javascript. Removing a Property from JavaScript Object Let's say we have a Java Script Object that contains three properties (details) of an employee namely 'name', 'gender' & 'position'. {"name": "Garrett Winters","gender": "Male","position": "Javascript Developer"} Now we want to remove one of the properties from the above object and to do this, we have to use java script's delete method. Here's the code snippet for rem...

How to Insert Multiple JSON Data into MySQL Database in PHP

صورة
Hi! I have discussed about inserting json into mysql using php a while back. And I got several queries from readers about inserting multiple json objects into DB using the exact method. Inserting multiple objects involves multiple database transactions. There are less elegant ways to do this but using mysqli library’s Prepared Statement is the effective and secure way for this type of job. Prepared Statements has several advantages like utilizing fewer resources, preventing sql injection etc. I’ll show you here with an example, how to use mysqli prepared statement to insert multiple json data into mysql database in php. Create MySQL Database & Table First we need to create mysql database required for our example. Run this below sql command to create it. CREATE DATABASE `employee`; USE `employee`; CREATE TABLE IF NOT EXISTS `emp` ( `id` int(8) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `gender` varchar(10) NOT NULL, `designation` varchar(30) NOT NULL, ...

How to Convert Data from MySQL to JSON using PHP

صورة
In PHP, Converting Data from MySQL to JSON Format is one of the prominent tasks in Web Development . JSON has gained popularity over the years and is preferred over xml as data exchange format between web applications. Using json format has its own advantages like being light weight, ability to store complex data structures in plain text and very human readable. Earlier we have discussed about converting json to mysql data here. Now let us see how to convert mysql result set to json in php. Create MySQL Database Here is the MySQL Database I'm going to use as an example. Run these sql commands to create the Database. Read Also: How to Export MySQL Database to CSV File using PHP Build Simple Search Engine using AJAX, PHP & MySQL Login and Registration Script using PHP, MySQL & jQuery CREATE TABLE IF NOT EXISTS `tbl_employee` ( `employee_id` int(4) NOT NULL AUTO_INCREMENT, `employee_name` varchar(60) NOT NULL, `designation` varchar(30) NOT NULL, `hired...

Insert Array into MySQL Database using PHP

صورة
Hi! Today let's see how to insert array into mysql database using php . A frequently asked question by budding developers but not without reason. Since databases don't support array data types, there is no direct way to store them in db. But you can convert array as string and insert into mysql. There are two ways you can do it, one is by serializing the array and the other is saving it as json string. But before going into the process, I must warn you it's a bad idea to store array in database as it is against the normalization theory of relational database and you cannot easily query the data later. Nevertheless, sometimes I used to store array in database for temporary log and you can also do it. How to Insert Array into MySQL with PHP? The first method we are going to see is about serializing php array to string. In PHP, we have two methods, serialize() and unserialize() , the first one converts array to string and the latter reverts back the string to arra...

Convert JSON to CSV using PHP (JSON Keys as Column Headers)

صورة
Hi! Here's how to convert json to csv in php by using json keys as column headers for csv file . Even though json format is immensely popular for data exchange over web, there are times you may need to work with csv data and have to convert json to csv. Being a modern language, php doesn't have trouble handling both data formats but unfortunately there's no one step solution to convert json to csv using php . But don't worry! I have created a simple php script for json to csv conversion. In case you don't know, json stores data as 'key:value' pairs. And the function I have created will take valid json keys and use it as column headers for the csv file during conversion. I have already discussed about converting csv to json in php and below we'll see about converting json to csv in php. But keep in mind there are so many things can go wrong during this conversion so you need to do proper error handling in the script. How to Convert JSON to CSV ...

Convert CSV To JSON Using PHP (With Header Row As Keys)

صورة
Hi! Here is the php script to convert csv to json with header row as keys. Both csv and json are used to store data but json is little more verbose and human readable and has become popular data exchange format among modern web services. Converting csv data into json format is little tricky especially if you want your json to be properly formatted as key:value pairs. The csv header row should be used as keys and the subsequent rows as values to form json string. Unfortunately there is no direct way to do this but likely there is a workaround to bring out the desired result. The method used here is by far the most effective solution to convert csv to json with header row as keys. Converting CSV to JSON using PHP Let's take a sample csv file with header and some rows and convert it to json using php script. Data.csv Id,Name,Position,Salary 1,Suki Burks,Developer,$114500 2,Fred Zupers,Technical Author,$145000 3,Gavin Cortez,Team Leader,$235500 PHP Function to Convert CSV...