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

时间:2018-09-17 05:53:35

标签: php curl php-curl

我是CURL的新手,我需要验证用户是否已登录或未使用访问令牌。但是如何将带有发帖请求的 x-access-token 发送到标头中。 我在用。 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);
}

2 个答案:

答案 0 :(得分:0)

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);

// ************************ END标头******************* **************

$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;

答案 1 :(得分:-1)

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

RTFM plz。

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);
}