Выполнение вызовов PayPal API в PHP без cURL

0

Я не могу использовать cURL, поскольку Google App Engine не поддерживает его.

Я пытаюсь получить токен доступа на шаге 2 из: https://developer.paypal.com/docs/integration/direct/make-your-first-call

Я не уверен, как я должен структурировать свой призыв. У меня это до сих пор:

$data = array(
    'grant_type' => 'client_credentials',
    'client_id' => $sandbox_client_id,
    'secret' => $sandbox_secret

);


$data = json_encode($data);

$context = [
  'http' => [
    'method' => 'POST',
    'header' => 'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
        'Accept: application/json' . "\r\n" .
        'Accept-Language: en_US' . "\r\n"
    ,
    'content' => $data
  ]
];
$context = stream_context_create($context);

$response = file_get_contents('https://api.sandbox.paypal.com/v1/oauth2/token', false, $context);

echo $response;

Что возвращает это:

Warning: file_get_contents(https://api.sandbox.paypal.com/v1/oauth2/token): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\folder\php\paypal\test.php on line 28

Любое понимание оценено!

Теги:
google-app-engine
paypal

1 ответ

0

Я понял.

client id и secret client id должны быть помещены в строку, разделенную : затем base64 encoded. Затем он отправляется в header.

Это работает:

$auth_string = $sandbox_client_id . ':' . $sandbox_secret;
$encoded_auth = base64_encode($auth_string);

$data = array(
    'grant_type' => 'client_credentials'


);

$http_query = http_build_query($data);
$context = [
  'http' => [
    'method' => 'POST',
    'header' => 'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
        'Accept: application/json' . "\r\n" .
        'Accept-Language: en_US' . "\r\n" .
        'Authorization: Basic ' . $encoded_auth
    ,
    'content' => $http_query
  ]
];


$context = stream_context_create($context);

$response = file_get_contents('https://api.sandbox.paypal.com/v1/oauth2/token', false, $context);

echo $response;

Ещё вопросы

Сообщество Overcoder
Наверх
Меню