1

Im trying to send my access_token to oauth2.0 in POST header, while at the same time POSTing json in the body.

So I already acquired my access_token, and now want to use it to submit some data to the API.

$token = array(
 "access_token" =>$test->access_token);

$data = array(
  "Action" =>"updateUser",
  "data" =>array('somedata' => 'somemoredata')
  );

// Submit some data
$test2=curl_req2($pageURL.$webroot."/api/v1/User.php",$token, $data , "POST");
print_r($test2);

The curl_req2() function looks like this:

function curl_req2($path,$token,$data,$req)
{
    $ch = curl_init($path);
    $data = json_encode($data);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token['access_token']));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    //$result = json_decode($result);
    curl_close($ch);

    return $result;
}

If I do it this way, it seams like the json code is not recieved. When I print_r($_REQUEST) on server side, it simply returns an empty array. If I don't json_encode the $data, my array gets returned fine.

I then tried adding 'Content-Type: application/json','Content-Length: ' . strlen($data), to the HTTPHEADER array in curl, but then the authorization in HTTP header does not work.

Is there a way I can both authorize the token in POST headers, while at the same time sending json data in POST body?

1
  • Adding the content type header should not interefere with the Authorization header unless that is the intended functionality of the service. Please can you show us how you added the Content-Type headers by updating the code in the question? Commented Nov 25, 2014 at 23:02

1 Answer 1

2
+50

The server's $_REQUEST superglobal works with query strings, not with JSON data.

If you want to access the request body, you can use:

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

Comments

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.