5

I am trying to make an API with PHP and JSON, however I am stuck on extracting the JSON on the receiving/API side of the request.

So I am doing this on the client end of my transaction:

$data = array("test" => "test");                                                                    
$data_string = json_encode($data);
$ch = curl_init('https://..../api/v1/functions?  key=TPO4X2yCobCJ633&aid=9&action=add');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$return = curl_exec($ch);

But how do I receive and extract the POSTed data on the server (receiving) side of the transaction (inside my API)?

Summary:

What i mean is i have a client that is sending a JSON file to the API now i what i don't understand is how can i get the API side to see and extract/see the data

4
  • 1
    See what is in $return. That will be your resulting json string. var_dump($return). Add the output to the code to help you out further. Commented Jan 31, 2016 at 19:54
  • also don't forget to close curl = curl_close($ch); Commented Jan 31, 2016 at 20:24
  • Well, then you make your scripts on the server side that reads in the query strings using $_POST just like processing an HTML form. Then use json_encode to convert array to JSON string. Commented Jan 31, 2016 at 20:26
  • Are you handling the server side of the API now? Are you trying to process the data that was posted to the API? Commented Jan 31, 2016 at 20:30

1 Answer 1

5

On the receiving side you need to read from the PHP input stream, then decode:

<?php

// read the incoming POST body (the JSON)
$input = file_get_contents('php://input');

// decode/unserialize it back into a PHP data structure
$data = json_decode($input);

// $data is now the same thing it was line 1 of your given sample code

If you wanted to be more succinct, you could obviously just nest those calls:

<?php
$data = json_decode(file_get_contents('php://input'));
Sign up to request clarification or add additional context in comments.

2 Comments

You sir are awesome!
Nah, just been through the experience of learning that before. :) Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.