2

Not sure if anyone can help me out with a question.

I had to write some php for the company I work for that lets us integrate with an API that accepts a JSON body. I used the cUrl method, and the script is working great.

If I wanted to build another php page that would accept the request im sending, how would I go about this?

Say I wanted to allow someone to send this same request to me, and then wanted the info they sent to go into my database, how would turn their request into php strings?

Here is the code im sending.

<?
$json_string = json_encode(array("FirstName" => $name, "MiddleName" => " ", "LastName" => $last));;
// echo $json_string;
// jSON URL which should be requested
$json_url = 'http://www.exampleurl.com';
// jSON String for request
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
    'Accept: application/json;charset=utf-8',
    'Content-Type: application/json;charset=utf-8',
    'Expect: 100-continue',
    'Connection: Keep-Alive') ,
CURLOPT_POSTFIELDS => $json_string
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result =  curl_exec($ch); // Getting jSON result string
echo $result;
$myArray = json_decode($result);
$action = $myArray->Action;
?>
2
  • And what's the problem? Just look for the json_string on your $_POST array. Commented Dec 10, 2012 at 20:13
  • @Hast doesn't look like he's using a $_POST array, just the JSON. In that case he's going to have to get the raw data out of the POST body and/or go looking for the individual JSON object that he wants from within that body to get what he needs. He can do an http query formatted version, but I do not believe this is what the example shows. Commented Dec 10, 2012 at 21:44

2 Answers 2

3

To get the raw data from the POST that you would be receiving you would use $postData = file_get_contents('php://input');

http://php.net/manual/en/reserved.variables.post.php

Then you would json_decode() the contents of that POST back into JSON.

http://php.net/manual/en/function.json-decode.php

Sign up to request clarification or add additional context in comments.

Comments

0

Not really good understood your question. May be you are looking for the way to read raw POST data? In that case open and read from php://stdin stream.

$stdin = fopen('php://stdin', 'r');

By the way read here ( http://php.net/manual/en/function.curl-setopt.php ) how to use CURLOPT_POSTFIELDS. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.

2 Comments

It isn't necessary that CURLOPT_POSTFIELDS be a key-value string, only that it be properly URL encoded!
I understand that. The implication by citing the documentation is that the OP is misusing it. But, the usage in the OP is perfectly acceptable.

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.