PHP code example of bluepsyduck / factorio-mod-portal-client

1. Go to this page and download the library: Download bluepsyduck/factorio-mod-portal-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bluepsyduck / factorio-mod-portal-client example snippets




use BluePsyduck\FactorioModPortalClient\Constant\ConfigKey;

return [
    ConfigKey::MAIN => [
        ConfigKey::OPTIONS => [
            // Your Factorio username. This username is used to build a full download link, avoiding getting redirected
            // to the login page. 
            // See https://wiki.factorio.com/Mod_portal_API#Downloading_Mods for further details.
            ConfigKey::OPTION_USERNAME => 'your-username',
            // The token to your username.
            ConfigKey::OPTION_TOKEN => 'your-token',

            // The timeout in seconds to use for the request. Defaults to 10 seconds.
            ConfigKey::OPTION_TIMEOUT => 10,
        ],
    ],
];


/* @var \Psr\Container\ContainerInterface $container */

use BluePsyduck\FactorioModPortalClient\Client\Facade;
use BluePsyduck\FactorioModPortalClient\Request\ModRequest; 

/* @var Facade $facade */
$facade = $container->get(Facade::class);

$mod = $facade->getMod((new ModRequest())->setName('FARL'));

// Do something with the received mod.
var_dump($mod->getDownloadsCount());


/* @var \Psr\Container\ContainerInterface $container */

use BluePsyduck\FactorioModPortalClient\Client\ClientInterface;
use BluePsyduck\FactorioModPortalClient\Request\ModRequest; 
use BluePsyduck\FactorioModPortalClient\Response\ModResponse;
use function GuzzleHttp\Promise\all;

/* @var ClientInterface $client */
$client = $container->get(ClientInterface::class);

$request1 = (new ModRequest())->setName('FARL');
$request2 = (new ModRequest())->setName('FNEI');

/* @var ModResponse[] $responses*/
$responses = all([
    'FARL' => $client->sendRequest($request1),
    'FNEI' => $client->sendRequest($request2)
])->wait();

// Do something with the responses
var_dump($responses['FARL']->getSummary());
var_dump($responses['FNEI']->getSummary());

 
/* @var BluePsyduck\FactorioModPortalClient\Client\Facade $facade */
/* @var BluePsyduck\FactorioModPortalClient\Response\ModResponse $response */

$downloadUrl = $facade->getDownloadUrl($response->getReleases()[0]->getDownloadUrl());
$thumbnail = $facade->getAssetUrl($response->getThumbnail());