Square API以空JSON响应

时间:2017-09-28 05:09:37

标签: square-connect

为什么我从Square服务器收到空响应?

一旦我将位置更改为其中一个测试位置,我似乎无法获得任何响应。我正在使用正确的沙盒令牌。我是否无意中遗漏了必要的信息或触发了某种错误或安全机制?在任何情况下,我都无法想象空洞的回应是恰当的。由于显而易见的原因,敏感的字符串被混淆了。

PHP

$url = 'https://connect.squareup.com/v2/locations/'.$location_id.'/transactions';
$curl_handle = curl_init($url);
$request_headers = array();
$request_headers[] = 'Content-Type: application/json';
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Authorization: Bearer '.$token;
$encodedData = json_encode($jsonData);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $encodedData);
$request_headers[] = 'Content-Length: '.strlen($encodedData);
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl_handle);

cURL请求中使用的JSON

{
"idempotency_key": "1",
"billing_address": {
"address_line_1": "123 Main Street",
"address_line_2": "",
"locality": "New York City",
"administrative_district_level_1": "NY",
"postal_code": "12345",
"country": "US"
},
"amount_money": {
"amount": 10,
"currency": "USD"
},
"delay_capture": false,
"buyer_email_address": "test@example.com",
"card_nonce": "122333444455555666666777777"
}

使用print_r(curl_getinfo($curl_handle))我收到:

Array
(
 [certinfo] => Array()
 [connect_time] => 0.101375
 [content_type] => application/json
 [download_content_length] => 2
 [filetime] => -1
 [header_size] => 372
 [http_code] => 200
 [local_ip] => 66.198.240.51
 [local_port] => 44428
 [namelookup_time] => 0.009074
 [pretransfer_time] => 0.392582
 [primary_ip] => 74.122.190.85
 [primary_port] => 443
 [redirect_count] => 0
 [redirect_time] => 0
 [redirect_url] => 
 [request_size] => 734
 [size_download] => 2
 [size_upload] => 492
 [speed_download] => 3
 [speed_upload] => 979
 [ssl_verify_result] => 0
 [starttransfer_time] => 0.502108
 [total_time] => 0.502167
 [upload_content_length] => 492
 [url] => https://connect.squareup.com/v2/locations/__example__/transactions
)

1 个答案:

答案 0 :(得分:1)

您有没有理由不使用PHP SDK?我认为这会让你更容易,跟踪所有这些卷曲选项看起来有点令人困惑。

您正在向交易终端发送GET请求,要求API列出您的所有交易。对于GET请求,在您的请求中添加正文并不是一个好主意,因为它几乎被所有API(如我们的)所忽略。

API会收到您的GET请求,并将您可能尚未在测试帐户中创建的任何交易返回给您:

{}

这是预期的行为。如果您尝试设置费用而不是列出交易,请发送POST请求,或者只使用SDK让您更轻松,请参阅Charge