向php curl脚本添加基本授权

时间:2019-01-24 22:22:46

标签: php

如何在下面的php脚本中添加基本授权?我正在尝试将数据发送到API

$ch = curl_init();
$token = 'eyJpZCI6MTExOCwiZW52IXXXXXXXXJzaWQiOjYsImFsZyI6IkhTMjU2In0.eyJzdWIiOiJhZG1pbkBtaW5leDXXXXXXXTAsImV4cCI6MTU0ODk0ODgxMH0.owTm-ItzXnpqVSFbXXXXXXXXXX';
$authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token

$curlConfig = array(
    CURLOPT_URL            => "https://api.endpoint.com",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => array(
        'From' => 'HI',
        'To' => '024XXXXXXX',
        'Content' => 'hello there'
    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result

1 个答案:

答案 0 :(得分:0)

我想这会起作用(我不确定,因为我从未使用过授权)...

$ch = curl_init();
$token = 'eyJpZCI6MTExOCwiZW52IXXXXXXXXJzaWQiOjYsImFsZyI6IkhTMjU2In0.eyJzdWIiOiJhZG1pbkBtaW5leDXXXXXXXTAsImV4cCI6MTU0ODk0ODgxMH0.owTm-ItzXnpqVSFbXXXXXXXXXX';

$curlConfig = array(
    CURLOPT_URL            => "https://api.endpoint.com",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => array(
        'From' => 'HI',
        'To' => '024XXXXXXX',
        'Content' => 'hello there'
    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: Bearer ".$token
));
curl_close($ch);
echo $result

就这样吗?