尝试执行PayPal付款请求时出现HTTP 400错误。 (file_get_contents)

时间:2019-01-19 19:40:30

标签: php paypal paypal-sandbox payment file-get-contents

尝试发出无cURL的测试Paypal付款请求,但出现错误。 我正在使用来自Paypal Payment API文档的Sample请求。

$data = array (
  0 => 
  array (
    'op' => 'replace',
    'path' => '/transactions/0/amount',
    'value' => 
    array (
      'total' => '18.37',
      'currency' => 'EUR',
      'details' => 
      array (
        'subtotal' => '13.37',
        'shipping' => '5.00',
      ),
    ),
  ),
  1 => 
  array (
    'op' => 'add',
    'path' => '/transactions/0/item_list/shipping_address',
    'value' => 
    array (
      'recipient_name' => 'Anna Gruneberg',
      'line1' => 'Kathwarinenhof 1',
      'city' => 'Flensburg',
      'postal_code' => '24939',
      'country_code' => 'DE',
    ),
  ),
);

$url = 'https://api.sandbox.paypal.com/v1/payments/payment';
$options = array(
    'http' => array(
        'header'  => array(
            "Content-type: application/json", 
            "Authorization: Bearer $access_token"
        ),
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
print_r($options);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

返回:警告:file_get_contents(https://api.sandbox.paypal.com/v1/payments/payment):打开流失败:HTTP请求失败!第48行的/storage/ssd5/910/7954910/public_html/paypal.php中的HTTP / 1.0 400错误请求 bool(false)

1 个答案:

答案 0 :(得分:0)

添加

'protocol_version' => '1.1'

到'http'=> array()。

例如换行

'method'  => 'POST',

'protocol_version'  => '1.1',
'method'  => 'POST',

这会将file_get_contents()使用的HTTP协议从HTTP / 1.0更改为HTTP / 1.1,并且PayPal REST API服务器需要HTTP / 1.1。