如何使用 CURL PHP 发送访问令牌

how to send access token using CURL PHP

我是 CURL 的新手,我需要使用访问令牌验证用户是否已登录。但是如何将带有 post 请求的 x-access-token 发送到 header。 我在用。 curl library

require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
$curl = new Curl();
$curl->post('http://localhost:3011/user/reset-password',array('x-access-token'=>$user['token']), array(
    'newPassword' => $_POST['newPassword'],
    'confirmPassword' => $_POST['confirmPassword']
));
if ($curl->error) {
    $response = array(
        'status' => $curl->errorCode,
        'message' => $curl->errorMessage,
        'response' => $curl->response
    );
    echo json_encode($response);
} else {
    $response = array(
        'status' => $curl->httpStatusCode,
        'message' => 'Successfylly Login',
        'response' => $curl->response
    );
    echo json_encode($response);
}

您可以在 Curl 中使用 setHeader 方法 class。

RTFM 请。

require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
$curl = new Curl();

// check the following line
$curl->setHeader('x-access-token', 'YOUR-TOKEN-HERE');


$curl->post('http://localhost:3011/user/reset-password',array('x-access-token'=>$user['token']), array(
    'newPassword' => $_POST['newPassword'],
    'confirmPassword' => $_POST['confirmPassword']
));
if ($curl->error) {
    $response = array(
        'status' => $curl->errorCode,
        'message' => $curl->errorMessage,
        'response' => $curl->response
    );
    echo json_encode($response);
} else {
    $response = array(
        'status' => $curl->httpStatusCode,
        'message' => 'Successfylly Login',
        'response' => $curl->response
    );
    echo json_encode($response);
}
You can do this way also.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.abctest.com/abc.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//********************Check these lines for headers******************* 
$headers = [
    'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
    'x-access-token':'sdsdgdsggrtyrtghf'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//************************ 结束 Headers ************* *********************

$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;