Tustin / psn-php

A PHP wrapper for the PSN API
https://tustin.dev/psn-php/
MIT License
354 stars 73 forks source link

How Can I Show Every AccountId Searched? #248

Open Argosth opened 1 year ago

Argosth commented 1 year ago

Hello, I'm trying to use this API again, and thanks in advance for this work.

I want to use AccountID of every user instead of "onlineId", and the problem is that when I try to access the object values, I can't go directly to every user Account ID that matched the criteria, how can I show or save every data?

For example: $users = $client->users()->search('tustin25'); foreach($users as $user) { // How can I get every user AccountID? }

Thanks for your help.

Ragowit commented 1 year ago
$users = $client->users()->search('tustin25');
foreach($users as $user) {
  echo $user->accountId() ."<br>";
}
Argosth commented 1 year ago
$users = $client->users()->search('tustin25');
foreach($users as $user) {
  echo $user->accountId() ."<br>";
}

Thank you for your quick response, is there a chance to get also the Country and Language from Search() option? Or should I go for that information with "find('accountId')" method?

Thank you.

Ragowit commented 1 year ago

Country, yes. Look at https://github.com/Tustin/psn-php/blob/master/src/Model/User.php to see all of the methods available.

https://github.com/Tustin/psn-php/blob/5d9a357eed5e478e0b97112498c6cd5d6bac1749/src/Model/User.php#L128-L136

Please note that country is only obtained via search(), and not via find(). This is a limitation from Sony, not this API.

Don't know what you mean about language.

EDIT: Found this, but I have never used it. https://github.com/Tustin/psn-php/blob/5d9a357eed5e478e0b97112498c6cd5d6bac1749/src/Model/User.php#L205-L211 Also: https://tustin.dev/psn-php/#/users?id=languages

Argosth commented 1 year ago

Thank you very much Ragowit, you are always here helping noobs like me hehehe

One question, when I try to get title->platform(), I get this enum error: Fatal error: Uncaught Error: Cannot instantiate enum Tustin\PlayStation\Enum\ConsoleType), I see that consoleType is a enum type and platform() function return an array, but I don't know why I receive that error, I have PHP 8.2.

And final question, I manage to get all games with the trophy summary of an specific player, the question is: How can I get now the trophies associated that the player has earned?

Thank you again Ragowit.

Ragowit commented 1 year ago

For platforms, I believe I did this hack (there's certainly a cleaner way):

$platforms = "";
foreach ($trophyTitle->platform() as $platform) {
    $platforms .= $platform->value .",";
}
$platforms = rtrim($platforms, ",");
echo $platforms;

And for earned trophies:

foreach ($user->trophyTitles() as $trophyTitle) {
    foreach ($trophyTitle->trophyGroups() as $trophyGroup) {
        foreach ($trophyGroup->trophies() as $trophy) {
            $trophyEarned = $trophy->earned();
            // ...and so on
        }
    }
}

Here's the source of all the methods you can use for $trophy: https://github.com/Tustin/psn-php/blob/master/src/Model/Trophy/Trophy.php

Argosth commented 1 year ago

Thank you Ragowit, your Platform method is still giving me the same error, and $trophyGroup->totalTrophyCount(); is giving me this error: Fatal error: Uncaught TypeError: Tustin\PlayStation\Model\Trophy\TrophyGroup::bronze(): Return value must be of type int, null returned

Any ideas how to solve those 2 issues?

Thank you, I'm trying hard to figure it out :)

Ragowit commented 1 year ago

Sorry, I'm at loss. Maybe @Tustin can fill you out when he's around.

Argosth commented 1 year ago

No worries mate, you did well and help me out a lot :)

Argosth commented 1 year ago

One question, is it possible to get the Trophy Group Name? I'm trying and always return a null. I only managed to get the Trophy Group ID (default, 002, 003 etc).

Thanks :)

Ragowit commented 1 year ago

Check https://github.com/Tustin/psn-php/blob/master/src/Model/Trophy/TrophyGroup.php

You have title(), name() and detail(), it should be one of those.

Argosth commented 1 year ago

Check https://github.com/Tustin/psn-php/blob/master/src/Model/Trophy/TrophyGroup.php

You have title(), name() and detail(), it should be one of those.

Hello, about TrophyGroup->name() and detail() are apparently not working, I just managed to get the title() object, but I don't know how to access ["trophyTitleName"] in that object, this is the object returned:

\Tustin\PlayStation\Model\Trophy\UserTrophyTitle::__set_state(array( 'httpClient' => \GuzzleHttp\Client::__set_state(array( 'config' => array ( 'allow_redirects' => false, 'headers' => array ( 'User-Agent' => 'PlayStation/21090100 CFNetwork/1126 Darwin/19.5.0', 'Accept-Language' => 'en-US', ), 'base_uri' => \GuzzleHttp\Psr7\Uri::__set_state(array( 'scheme' => 'https', 'userInfo' => '', 'host' => 'm.np.playstation.com', 'port' => NULL, 'path' => '/api/', 'query' => '', 'fragment' => '', 'composedComponents' => NULL, )), 'handler' => \GuzzleHttp\HandlerStack::__set_state(array( 'handler' => \Closure::__set_state(array( )), 'stack' => array ( 0 => array ( 0 => \Closure::__set_state(array( )), 1 => 'http_errors', ), 1 => array ( 0 => \Closure::__set_state(array( )), 1 => 'allow_redirects', ), 2 => array ( 0 => \Closure::__set_state(array( )), 1 => 'cookies', ), 3 => array ( 0 => \Closure::__set_state(array( )), 1 => 'prepare_body', ), 4 => array ( 0 => \Closure::__set_state(array( )), 1 => '', ), 5 => array ( 0 => \Closure::__set_state(array( )), 1 => '', ), ), 'cached' => \Closure::__set_state(array( )), )), 'http_errors' => true, 'decode_content' => true, 'verify' => true, 'cookies' => false, 'idn_conversion' => false, ), )), 'cache' => array ( 'npServiceName' => 'trophy2', 'npCommunicationId' => 'NPWR21008_00', 'trophySetVersion' => '01.00', 'trophyTitleName' => 'Horizon Forbidden West', 'trophyTitleIconUrl' => 'https://psnobj.prod.dl.playstation.net/psnobj/NPWR21008_00/2b1ab462-7ee0-4415-81d8-bf2632f03850.png', 'trophyTitlePlatform' => 'PS5', 'hasTrophyGroups' => true, 'definedTrophies' => array ( 'bronze' => 67, 'silver' => 10, 'gold' => 2, 'platinum' => 1, ), 'progress' => 84, 'earnedTrophies' => array ( 'bronze' => 58, 'silver' => 10, 'gold' => 1, 'platinum' => 1, ), 'hiddenFlag' => false, 'lastUpdatedDateTime' => '2023-04-28T21:52:57Z', ), 'factory' => \Tustin\PlayStation\Factory\TrophyTitlesFactory::__set_state(array( 'httpClient' => \GuzzleHttp\Client::__set_state(array( 'config' => array ( 'allow_redirects' => false, 'headers' => array ( 'User-Agent' => 'PlayStation/21090100 CFNetwork/1126 Darwin/19.5.0', 'Accept-Language' => 'en-US', ), 'base_uri' => \GuzzleHttp\Psr7\Uri::__set_state(array( 'scheme' => 'https', 'userInfo' => '', 'host' => 'm.np.playstation.com', 'port' => NULL, 'path' => '/api/', 'query' => '', 'fragment' => '', 'composedComponents' => NULL, )), 'handler' => \GuzzleHttp\HandlerStack::__set_state(array( 'handler' => \Closure::__set_state(array( )), 'stack' => array ( 0 => array ( 0 => \Closure::__set_state(array( )), 1 => 'http_errors', ), 1 => array ( 0 => \Closure::__set_state(array( )), 1 => 'allow_redirects', ), 2 => array ( 0 => \Closure::__set_state(array( )), 1 => 'cookies', ), 3 => array ( 0 => \Closure::__set_state(array( )), 1 => 'prepare_body', ), 4 => array ( 0 => \Closure::__set_state(array( )), 1 => '', ), 5 => array ( 0 => \Closure::__set_state(array( )), 1 => '', ), ), 'cached' => \Closure::__set_state(array( )), )), 'http_errors' => true, 'decode_content' => true, 'verify' => true, 'cookies' => false, 'idn_conversion' => false, ), )), 'platforms' => array ( ), 'withName' => '', 'hasTrophyGroups' => NULL, 'user' => \Tustin\PlayStation\Model\User::__set_state(array( 'httpClient' => \GuzzleHttp\Client::__set_state(array( 'config' => array ( 'allow_redirects' => false, 'headers' => array ( 'User-Agent' => 'PlayStation/21090100 CFNetwork/1126 Darwin/19.5.0', 'Accept-Language' => 'en-US', ), 'base_uri' => \GuzzleHttp\Psr7\Uri::__set_state(array( 'scheme' => 'https', 'userInfo' => '', 'host' => 'm.np.playstation.com', 'port' => NULL, 'path' => '/api/', 'query' => '', 'fragment' => '', 'composedComponents' => NULL, )), 'handler' => \GuzzleHttp\HandlerStack::__set_state(array( 'handler' => \Closure::__set_state(array( )), 'stack' => array ( 0 => array ( 0 => \Closure::__set_state(array( )), 1 => 'http_errors', ), 1 => array ( 0 => \Closure::__set_state(array( )), 1 => 'allow_redirects', ), 2 => array ( 0 => \Closure::__set_state(array( )), 1 => 'cookies', ), 3 => array ( 0 => \Closure::__set_state(array( )), 1 => 'prepare_body', ), 4 => array ( 0 => \Closure::__set_state(array( )), 1 => '', ), 5 => array ( 0 => \Closure::__set_state(array( )), 1 => '', ), ), 'cached' => \Closure::__set_state(array( )), )), 'http_errors' => true, 'decode_content' => true, 'verify' => true, 'cookies' => false, 'idn_conversion' => false, ), )), 'cache' => array ( ), 'hasFetched' => false, 'accountId' => '###', )), )), 'hasFetched' => false, 'serviceName' => 'trophy2', ))

Ragowit commented 1 year ago

To get trophy data like title, you can't go via user's TrophyTitles (that will only bring you like trophy earned data for that user).

Let me add more code to my previous reply:

foreach ($user->trophyTitles() as $trophyTitle) {
    // This is for getting data like name and detail, not tied to a user
    foreach ($client->trophies($trophyTitle->npCommunicationId(), $trophyTitle->serviceName())->trophyGroups() as $trophyGroup) {
        // Here we can call $trophyGroup->name(), $trophyGroup->detail(), etc
        foreach ($trophyGroup->trophies() as $trophy) {
            // And here we can reach $trophy->name(), $trophy->detail(), etc
        }
    }

    // This is for getting data tied to a user, like earned date
    foreach ($trophyTitle->trophyGroups() as $trophyGroup) {
        foreach ($trophyGroup->trophies() as $trophy) {
            $trophyEarned = $trophy->earned();
            // ...and so on
        }
    }
}
Argosth commented 1 year ago

Thank you very much Ragowit, I think I have everything now, just last questoin, the "earnedRate()" is specific of every player? I thought that was a global var that indicates the rarity of a trophy, or am I wrong?

Thanks :)

Ragowit commented 1 year ago

I have never used that myself, but I think it's a global var that indicates the rarity of a trophy. Maybe it changes so often so Sony decided to have that data on the user?

Argosth commented 1 year ago

Ok I see, thank you Ragowit :)