как отправить токен доступа с помощью CURL PHP - PullRequest
0 голосов
/ 17 сентября 2018

Я новичок в CURL. Мне нужно аутентифицировать пользователя, вошел в систему или не использует токен доступа.Но как отправить x-access-token с пост-запросом в заголовок.Я использую. библиотека curl

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 голосов
/ 17 сентября 2018
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 ;
0 голосов
/ 17 сентября 2018

Вы можете использовать метод setHeader в классе Curl.

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