I am able to use the api token and the command below to receive a json response that I need. The issue is when I try to transition into php. Here is the working curl example (obviously minus the string api token).
curl -v -D - -H 'Authorization: Token token="[private_api_token_here]"' -H "Accept: application/json" -H "Content-type: application/json" -X GET -d ' {"status":"collected,pending","transaction_type":"sale,credit","amount_min":"50.00"}' "https://app.mobilecause.com/api/v2/reports/transactions.json?"
Here is my latest attempt in php.
$json = '{
"status": "collected",
"transaction_type": "sale",
"amount_min": "50.00"
}';
$assoc_array = json_decode($json);
$urlEncodedString = http_build_query($assoc_array);
$URL = "https://app.mobilecause.com/api/v2/reports/transactions.json?" . $urlEncodedString;
$ch = curl_init();
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer [private_api_token_here]';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$info = curl_getinfo($ch);
$json = json_decode(curl_exec($ch));
curl_close($ch);
var_dump($json);
It seems to me that I am not quite getting the proper headers submitted. I kept the api token as a plain string. In previous attempts I submitted the token as base 64 encoded. In addition, I tried the below as plain string and base 64 to no avail:
'Authorization: Token [private_api_token_here]'
and
'Authorization: Token token=[private_api_token_here]'
I appreciate any ideas.
Please try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.mobilecause.com/api/v2/reports/transactions.json?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, " {\"status\":\"collected,pending\",\"transaction_type\":\"sale,credit\",\"amount_min\":\"50.00\"}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = [];
$headers[] = "Authorization: Token token=\"[private_api_token_here]\"";
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
var_dump(json_decode( $result ));