0

I'm trying to make a post call like this to return a jwt bearer token. But in php using cURL. So I can save the bearer token to a variable for use in the API calls.

$.ajax({
  type: 'POST',
  url: 'http://www.your-site.com/siteguard/api/prepare-public-jwt',
  data: { api_key: 'YOUR_PUBLIC_API_KEY' },
  dataType: 'json',
  success: function (data) {
    //Returns encoded JWT
    console.log(data);
  }
});

Php Code Im using

 $url = $this->getTokenUrl();

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $headers = array(
            "Content-Type: application/json",
        );
        
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        $data = ["api_key" => "xOMjVpz83rxDKjJUX9qNClB2BwadcRWjm09YSCdasdabdasdasdasdgTR8fuvR7jQHP8ZVpbOOmdXqKEt0AVX"];

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        //for debug only!
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        $resp = curl_exec($curl);
        curl_close($curl);
        var_dump($resp);

The Return is this

{"code":401,"status":"error","response":"Invalid Public API key","link":"http:\/\/localhost\/siteguard\/api\/prepare-public-jwt"}"
5
  • 1. Download postman, and create the post with all its required settings. 2. Click the Code button, located (in Windows) on the top right corner (about 2PM from the SEND button) 3. Select the desired programming language (PHP- cURL in your case). 4. ??? 5. Profit! Commented Aug 23, 2021 at 22:36
  • 1
    Have you confirmed that the token returns using Javascript? It may be an issue with your Siteguard install. Commented Aug 23, 2021 at 23:15
  • Also, try json_encode($data) before curl_setopt($curl, CURLOPT_POSTFIELDS, $data); Commented Aug 23, 2021 at 23:34
  • @mulquin it does return the token using the java in the console. Will try it now and return with the results Commented Aug 24, 2021 at 14:14
  • did not work, it still returns the same error. Commented Aug 24, 2021 at 14:18

1 Answer 1

1

I was able to achieve it use this

        $url = $this->getTokenUrl();

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => 'xOMjVpz83rxDKjJUX9qNClB2BwadcRWjm09YSCdasdabdasdasdasdgTR8fuvR7jQHP8ZVpbOOmdXqKEt0AVX',
            CURLOPT_HTTPHEADER => array(
              'Content-Type: application/x-www-form-urlencoded'
            ),
          ));
          
          $response = curl_exec($curl);
          $obj = json_decode($response);

          curl_close($curl);

          return $obj->response;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.