1

how will I accept a json data

{
"ccToken": "75676576765",
"cardType": "CC",   
}

which will be posted in my url and store the values in variables,like

$ccToken="756765676765" cardType="CC"

I may sound dump..Im just new to this,so I dont know how I will accept a json string posted on a url..Usually if its variables,I can accept it like,

$ccToken = $_POST['ccToken'];

but for this thing. and how will be able to sent a response like..200 in return?

1
  • Your question doesn't include how the data is posted. Is the request body a JSON formatted string or form data? Or, more specifically, application/json or application/x-www-form-urlencoded? Commented Feb 27, 2014 at 20:03

2 Answers 2

1

Assuming your entire POST body is a JSON string, you'll need to pull in and decode the entire JSON from the body:

 $raw_post_body = file_get_contents('php://input'); 
 $data_object = json_decode($raw_post_body);

Then you can access the json decoded $data_object like:

 echo  $data_object->ccToken;
Sign up to request clarification or add additional context in comments.

Comments

1

use the javascript JSON.stringify() method

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

and the php json_decode() function

https://www.php.net/json_decode

1 Comment

it might be easier if you posted the code you have so far but in your javascript you could use var json_str = JSON.stringify(obj); to get your javascript object into a json string. You could then post via ajax or whatever then in php (assuming you posted it as variable called data) you could do something like the following: $data = json_decode($_POST['data']); then you could output it with print_r($data) to make sure everything's workind as expected.

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.