how to send access token using CURL PHP

I am new in CURL I need to authenticate user is logged in or not using access token. But how to send x-access-token with post request into header. I am using. 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 answers

  • answered 2018-09-17 06:03 Amir Mohsen

    You can use setHeader method in Curl class.

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

  • answered 2018-09-17 06:06 ABHI

    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 Headers *********************************

    $server_output = curl_exec ($ch);
    
    curl_close ($ch);
    
    print  $server_output ;