Please humour / help me, as I’ve been banging my head against a brick wall
for the last hour on what has to be a really stupid, obvious thing.
I can:
- Send a user to FreeAgent to authenticate using oAuth 2
- Receive an authorization code, and make a request to get access and
refresh tokens - Send a GET request e.g. get all projects
$c = curl_init(‘https://api.sandbox.freeagent.com/v2/projects’);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
‘Accept: application/json’,
‘Content-Type: application/json’,
'Authorization: Bearer '.$accessToken
));
curl_setopt($c, CURLOPT_USERAGENT, ‘n7 Studios’);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, false);
// Get response from server
$response = curl_exec($c);
$body = json_decode($response);
curl_close($c);
What I cannot for the life of me do is create a project through the API:
$params = array(
‘contact’ => ‘837040’,
‘name’ => ‘Test Project’,
‘starts_on’ => ‘2012-05-11’,
‘ends_on’ => ‘2012-05-18’,
‘budget’ => ‘280’,
‘status’ => ‘Active’,
‘budget_units’ => ‘Monetary’,
‘normal_billing_rate’ => ‘10’,
‘hours_per_day’ => ‘8’,
‘currency’ => ‘GBP’
);
$c = curl_init(‘https://api.sandbox.freeagent.com/v2/projects’);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
‘Accept: application/json’,
‘Content-Type: application/json’,
'Authorization: Bearer '.$accessToken
));
curl_setopt($c, CURLOPT_USERAGENT, ‘n7 Studios’);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, json_encode($params));
The result I get is that all fields are empty:
stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[message] => contact can’t be blank
)
[1] => stdClass Object
(
[message] => name can't be blank
)
[2] => stdClass Object
(
[message] => status can't be blank,is invalid
)
[3] => stdClass Object
(
[message] => status can't be blank,is invalid
)
[4] => stdClass Object
(
[message] => currency is not an accepted currency
)
[5] => stdClass Object
(
[message] => budget_units is invalid
)
)
)
I’ve been through the Documentation about 5 times, and whilst it gives examples of how to perform GET requests, there’s nothing for POST requests.
What am I missing that’s so obvious please?