How To Send JSON Via POST In Cross Domain Using Codeigniter or PHP?

Sometimes, you'll come across web services and APIs that will require you to send JSON via a POST request.
CodeIgniter-cURL is a CodeIgniter library which makes it easy to do simple cURL requests and makes more complicated cURL requests easier too.

Sending JSON Via POST In Codeigniter

The Code:
//load the Curl library
$this->load->library('Curl');

$url = 'http://example.com/';

//The JSON data.
$jsonData = array(
    'username' => 'MyUsername',
    'password' => 'MyPassword'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

// Start session (also wipes existing/previous sessions)
$this->curl->create($url);

// Option			
$this->curl->option(CURLOPT_HTTPHEADER, array('Content-type: application/json; Charset=UTF-8'));

// Post - If you do not use post, it will just run a GET request			
$this->curl->post($jsonDataEncoded);
		
// Execute - returns responce 
echo $result = $this->curl->execute();	

Step by step explanation of the above code:
1. Install the Curl Library from CodeIgniter-cURL and placed it inside 'application/libraries' - application/libraries/Curl.php
2. Load the Curl Library
3. We setup the URL that we want to send our JSON to.
4. We setup a PHP array containing sample data.
5. We encoded our PHP array into a JSON string by using the function json_encode.
6. We started cURL session using $this->curl->create.
7. We set the content-type of our request to application/json. It is extremely important to note that you should always use "application/json", not "text/json". Simply put, using "text/json" is incorrect!
8. We specified that we were sending a POST request and attached our JSON data using $this->curl->post
9. Finally, we used the function $this->curl->execute to execute our POST request.

As you can see, it's not much different than sending a regular POST request. In fact, it's actually pretty simple.

Sending JSON Via POST In PHP

The code:
//API Url
$url = 'http://example.com/api/JSON/create';
 
//Initiate cURL.
$ch = curl_init($url);
 
//The JSON data.
$jsonData = array(
    'username' => 'MyUsername',
    'password' => 'MyPassword'
);
 
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
 
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
 
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
 
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
 
//Execute the request
$result = curl_exec($ch);
Step by step explanation of the above code:
1. We setup the URL that we want to send our JSON to. 2. We initiated cURL using curl_init. 3. We setup a PHP array containing sample data. 4. We encoded our PHP array into a JSON string by using the function json_encode. 5. We specified that we were sending a POST request by setting the CURLOPT_POST option to 1. 6. We attached our JSON data using the CURLOPT_POSTFIELDS option. 7. We set the content-type of our request to application/json. It is extremely important to note that you should always use "application/json", not "text/json". Simply put, using "text/json" is incorrect! 8. Finally, we used the function curl_exec to execute our POST request.

Source: http://thisinterestsme.com/sending-json-via-post-php/

Comments

Popular Posts