Send token Jwt from php

3

I am developing an OpenId integration from php and I have to send a jwt token to an endpoint.

I use Jwt firebase to generate and validate the jwt:

$key = 'xxxx';

$data = array(
    "tenantId" => self::TENANT_ID,
    "clientId" => self::CLIENT_ID,
    "objectId" => $oid,
    "iss" => "issuer",
    "aud" => "audience",
    "exp" => 1474980478,
    "nbf" => 1474976878,
    "state" => $state,
    "message" => $msg,
    "notifierAppID" => self::B2C_ID
);

$jwt = JWT::encode(
    $data,
    $key,
    'HS256'
);

The structure of the Endpoint is something like:

https://dominio/{prametro}/app/{parametro}/estado

The idea is that I have to send the token I generate for uqe to return data about an object.

Once the jwt is generated, how can I send the jwt to the endpoint from PHP?

I'm trying with this code, but it does not seem to work:

    // Create a stream
    $opts = array(
        'http'=>array(
            'method'=>"GET",
            'header'=>
                "Authorization: bearer ".$jwt
        )
    );

    $context = stream_context_create($opts);

    // Open the file using the HTTP headers set above
    $result = file_get_contents($endpoint, false, $context);
    
asked by Krleza 06.10.2016 в 22:06
source

1 answer

1

Have you tried sending the request by curl?

$crl = curl_init();

$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: bearer '.$jwt;

curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POST, true);
$rest = curl_exec($crl);

curl_close($crl);

print_r($rest);
    
answered by 13.10.2016 / 11:01
source