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?