Я пытаюсь отправить POST-запрос в PayPal API без внешних библиотек (следовательно, без cURL). Вместо этого я использую file_get_contents()
. При попытке объединить заголовок Basi c Authentication с заголовком Content-Type я получаю 400 (Bad-Request). См. Фрагмент кода ниже.
// The Basic Authentication Credential Encoding
$creds = sprintf('Authorization: Basic %s',
base64_encode($CLIENT_ID . ':' . $CLIENT_SECRET));
// Combining the two headers into an array
$header = array(
"Content-Type: application/json",
$creds
);
// Creating the context to use with file_get_contents
$ctx = stream_context_create(array(
'http' => array(
'display_errors' => true,
'protocol_version' => '1.1',
'method' => 'POST',
'header' => implode("\r\n", $header), // Imploding the header array and adding \r\n
'content' => json_encode($postBody)
)
));
$res = file_get_contents($urlString, FALSE, $ctx);
Интересно, что когда я удаляю переменную $creds
из массива $header
, я получаю 401 (Unauthorized), а когда я удаляю Content-Type из $header
, я получите 416 (неподдерживаемый носитель).
Как успешно объединить два заголовка?