Skip to content

Instantly share code, notes, and snippets.

@sdeering
Last active September 12, 2023 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdeering/9bf9bb2ea879ac760cf0dfa19621dbbb to your computer and use it in GitHub Desktop.
Save sdeering/9bf9bb2ea879ac760cf0dfa19621dbbb to your computer and use it in GitHub Desktop.
Connect to Sorare API Using PHP + GraphQL
// This script connects to Sorare API to get perform GraphQL queries using PHP
// Playground: https://api.sorare.com/federation/graphql/playground
/*
Steps:
1. Get user salt from Sorare
2. Hash password with salt
3. Get JWT token with email, password hash & aud from Sorare
4. Perform GraphQL query
*/
<?php
use GuzzleHttp\Client;
class SorareService {
protected $client;
protected $base_endpoint;
public function __construct() {
$this->base_endpoint = 'https://api.sorare.com/federation/graphql';
$this->client = new Client([
'content-type' => 'application/json'
]);
}
public function executeGraphQLQuery($GraphQLQuery) {
$response = $this->client->request('POST', $this->base_endpoint, [
'headers' => [
'Authorization' => 'Bearer '.$this->getJWTToken(),
'JWT-AUD' => $this->getJWTAUD()
],
'json' => [
'query' => $GraphQLQuery
]
]);
$json = $response->getBody()->getContents();
return json_decode($json);
}
public function getJWTToken() {
$email = '<your email here>';
$password = '<your password here>';
$passwordHash = $this->getPasswordHash($password);
$data = [
'input' => [
'email' => $email,
'password' => $passwordHash
]
];
$query = 'mutation { signIn(input: { email: "<your email here>", password: "<your password hash>" }) { currentUser { slug jwtToken(aud: "<your aud here>") { token expiredAt } } errors { message }}}';
$response = $this->client->request('POST', $this->base_endpoint, [
'json' => [
'query' => $query
]
]);
$json = $response->getBody()->getContents();
return json_decode($json);
}
private function getPasswordHash($password) {
$salt = $this->getUserSalt();
return Hash::make($password, ['salt' => $salt]);
}
private function getUserSalt() {
$url = 'https://api.sorare.com/api/v1/users/<your email here>';
$result = $this->api_get($url);
return $result->salt;
}
private function getJWTAUD() {
return '<your aud keyword here>';
}
?>
<?php
// query the service like this
public static function getUserBySlug()
{
$GraphQLQuery = <<<GQL
query {
user(slug: "dogesports") {
id,
nickname,
avatarUrl,
sorareAddress,
cardsCount
}
}
GQL;
return SorareService::executeGraphQLQuery($GraphQLQuery);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment