Monday, August 19, 2013

PHP – CURL – JSON

PHPIt is a web development language used on server side. PHP stands for Hypertext Preprocessor.

CURLwhat it stands for according to me is  Capture URL  It is one of the methods in php to reterive contents from remote websites on the backend itself.

JSON Javascript Object Notation, It is one of the what i would say is STRUCTURED data transfer between client side as well as from server.

You must have heard about graph api of facebook. What it do is it provides the required data in json.

I have been studying about interaction between various languages. What all can we use to share data from one language to another because an application cannot completely made in one language. Few of which i studied are XML, JSON etc.

I would tell about using XML later. For Today I am going to explain in short how to create and read JSON in PHP in javascript.

JSON in PHP

To create a JSON in php there is very simple function json_encode(hash)
  • hash is a kind of associative array in which you can access elements not only by index but also by name like $a['id']
  • since JSON is properly structured, there fore data to be passed in proper format
  • and each of the hash must have title, no title can result in error.
Sample PHP program to conver associative array in JSON




















<?php

 $a = array(

'Name' => 'Ashutosh Agrawal' ,

'Branch ' => 'Software Engineering' ,

'Subjects' => array(

'Sub1'=>'Software Design',

'Sub2'=>'Software Arch'

)

);

 echo json_encode($a);
?>
will output following json









{
"Name":"Ashutosh Agrawal",
"Branch ":"Software Engineering",
"Subjects":
    {
    "Sub1":"Software Design",
    "Sub2":"Software Arch"
    }
}

To Convert JSON back to Associative array use json_decode($json)
pass a valid json to the function and it would return the associative array for the json.

Now there comes a point that how are you going to get the json. well over here comes curl in the game. it is going to help you to get the JSON from remote sites. Not only json but also anycontent.
 
so how to use it…
 
first let me introduce some of the functions in curl which you require to access the json from a remote site.
  • $ch = curl_init() : this function intializes a curl session and returns the object to that session
  • curl_setopt($ch , $optionname , $optionvalue) : this function is used to set various options in curl session such as
    • CURLOPT_HTTPHEADER : Header of request
    • CURLOPT_RETURNTRANSFER : Whether to return the json received to the script or just display it on the page.
    • CURLOPT_URL : This option is used to set the remote fetch URL for CURL.
    • There are many other CURL options available You can check here
  • curl_exec($ch) : this would execute thCURL_RETURNTRANSFER is set to true, it will return the curl data else it would display and return 1.
Following Sample Program would fetch a json and display it as associative array















<?php

$json_url = "localhost/thepro/abc.json";
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
 curl_setopt($ch, CURLOPT_URL, $json_url);

 $str = curl_exec($ch);

 $ar = json_decode($str,true);

 echo $ar['Subjects']['Sub1'];

?>

PHPIt is a web development language used on server side. PHP stands for Hypertext Preprocessor.
CURLwhat it stands for according to me is  Capture URL  It is one of the methods in php to reterive contents from remote websites on the backend itself.
JSON Javascript Object Notation, It is one of the what i would say is STRUCTURED data transfer between client side as well as from server.
You must have heard about graph api of facebook. What it do is it provides the required data in json.
I have been studying about interaction between various languages. What all can we use to share data from one language to another because an application cannot completely made in one language. Few of which i studied are XML, JSON etc.
I would tell about using XML later. For Today I am going to explain in short how to create and read JSON in PHP in javascript.

JSON in PHP

To create a JSON in php there is very simple function json_encode(hash)
  • hash is a kind of associative array in which you can access elements not only by index but also by name like $a['id']
  • since JSON is properly structured, there fore data to be passed in proper format
  • and each of the hash must have title, no title can result in error.
Sample PHP program to conver associative array in JSON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
 
 $a = array(
 
'Name' => 'Ashutosh Agrawal' ,
 
'Branch ' => 'Software Engineering' ,
 
'Subjects' => array(
 
'Sub1'=>'Software Design',
 
'Sub2'=>'Software Arch'
 
)
 
);
 
 echo json_encode($a);
?>
will output following json
1
2
3
4
5
6
7
8
9
{
"Name":"Ashutosh Agrawal",
"Branch ":"Software Engineering",
"Subjects":
    {
    "Sub1":"Software Design",
    "Sub2":"Software Arch"
    }
}
To Convert JSON back to Associative array use json_decode($json)
pass a valid json to the function and it would return the associative array for the json.

Now there comes a point that how are you going to get the json. well over here comes curl in the game. it is going to help you to get the JSON from remote sites. Not only json but also anycontent.
so how to use it…
first let me introduce some of the functions in curl which you require to access the json from a remote site.

  • $ch = curl_init() : this function intializes a curl session and returns the object to that session
  • curl_setopt($ch , $optionname , $optionvalue) : this function is used to set various options in curl session such as
    • CURLOPT_HTTPHEADER : Header of request
    • CURLOPT_RETURNTRANSFER : Whether to return the json received to the script or just display it on the page.
    • CURLOPT_URL : This option is used to set the remote fetch URL for CURL.
    • There are many other CURL options available You can check here
  • curl_exec($ch) : this would execute thCURL_RETURNTRANSFER is set to true, it will return the curl data else it would display and return 1.
Following Sample Program would fetch a json and display it as associative array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
$json_url = "localhost/thepro/abc.json";
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
 curl_setopt($ch, CURLOPT_URL, $json_url);
 
 $str = curl_exec($ch);
 
 $ar = json_decode($str,true);
 
 echo $ar['Subjects']['Sub1'];
 
?>
I
- See more at: http://theprogrammer.in/blog/2012/04/php-curl-json/#sthash.6mWOCIno.dpuf
PHP – CURL – JSON

No comments:

Post a Comment