我能够使用api令牌和下面的命令来接收我需要的json响应。问题是当我尝试过渡到php时。 这是工作卷曲示例(显然减去字符串api令牌)。
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?"
这是我最近的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);
在我看来,我并没有提交正确的标题。我将api令牌保持为一个简单的字符串。在之前的尝试中,我将令牌作为base 64编码提交。另外,我尝试使用以下作为普通字符串和base 64无效:
'Authorization: Token [private_api_token_here]'
和
'Authorization: Token token=[private_api_token_here]'
我很感激任何想法。
答案 0 :(得分:0)
请试试这个:
$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 ));