CURL Request with PHP through GraphQL

Viewed 201

I know some PHP but I don't know a thing about GraphQL, so I'm trying to do an CURL HTTP request to Smash.gg with their API, but I keep getting "Invalid authentication token" back even though I keep using it on the GraphQL tester they have here and it works like it's supposed to. If someone could tell me how to get this working, that'd be great. Thanks.

<?php

$query = '
query TournamentsByOwner($perPage: Int!, $ownerId: ID!) {
    tournaments(query: {
      perPage: $perPage
      filter: {
        ownerId: $ownerId
      }
    }) {
    nodes {
      id
      name
      slug
    }
  }
}
';
$variables = '
  "ownerId": 161429,
  "perPage": 4
';

$json = json_encode(['query' => $query, 'variables' => $variables]);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.smash.gg/gql/alpha');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);    
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_HTTPHEADER,
     array(
            'User-Agent: PHP Script',
            'Content-Type: application/json;charset=utf-8',
            'Authorization: bearer XXXXXXXXXXXXXXXXX' 
        )
    ); 

$response = curl_exec($curl);
echo $response;

?>
0 Answers
Related