src/Service/Api.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Contracts\HttpClient\HttpClientInterface;
  7. class Api
  8. {
  9.     private $params;
  10.     private $client;
  11.     private $session;
  12.     private $logger;
  13.     public function __construct(HttpClientInterface $clientContainerBagInterface $paramsRequestStack $requestStackLoggerInterface $logger)
  14.     {
  15.         $this->params $params;
  16.         $this->client $client;
  17.         $this->logger $logger;
  18.         $this->session $requestStack->getSession();
  19.     }
  20.     public function getAllServices()
  21.     {
  22.         // Check if menu is already stored in session
  23.         if ($this->session->has("apiMenu")) {
  24.             // Return the stored menu
  25.             return $this->session->get("apiMenu");
  26.         }
  27.         // Call api to get menu
  28.         $response $this->client->request('GET'$this->params->get('app.orderApiUrl').'/services/GetAll');
  29.         // Get result
  30.         $result $response->toArray();
  31.         // Store the result to session
  32.         $this->session->set("apiMenu"$result);
  33.         // return the result
  34.         return $result;
  35.     }
  36.     public function getServicesIcons()
  37.     {
  38.         return [
  39.             "Followers" => "fa-solid fa-user-plus",
  40.             "Reel Likes" => "fas fa-heart",
  41.             "Likes" => "fas fa-heart",
  42.             "Automatic Likes" => "fas fa-heart",
  43.             "Views" => "fas fa-eye",
  44.             "Reel Views" => "fas fa-eye",
  45.             "IGTV Views" => "fas fa-eye",
  46.             "IG Impressions" => "fas fa-eye",
  47.             "Live Stream Views" => "fas fa-eye",
  48.             "Post Reach" => "fas fa-eye",
  49.             "Story Views" => "fas fa-eye",
  50.             "Automatic Views" => "fas fa-eye",
  51.             "Custom Comments" => "fas fa-comment",
  52.             "Random Comments" => "fas fa-comment",
  53.             "Auto Comments" => "fas fa-comment",
  54.             "Ig Direct Messages" => "far fa-paper-plane",
  55.             "FB Page Likes & Followers" => "far fa-thumbs-up",
  56.             "FB Profile Followers" => "fa-solid fa-user-plus",
  57.             "FB Post Likes" => "far fa-thumbs-up",
  58.             "FB Post Reaction Care" => "fas fa-grin-hearts",
  59.             "FB Post Reaction Haha" => "fas fa-smile",
  60.             "FB Post Reaction Love" => "far fa-grin-hearts",
  61.             "FB Post Reaction Wow" => "far fa-surprise",
  62.             "FB Video Views" => "fas fa-eye",
  63.             "FB Comments" => "fas fa-comment",
  64.             "FB Group Members" => "fa-solid fa-user-plus",
  65.             "TikTok Followers" => "fa-solid fa-user-plus",
  66.             "TikTok Likes" => "fas fa-heart",
  67.             "TIKTOK Auto Likes" => "fas fa-heart",
  68.             "TikTok Battle Likes" => "fas fa-heart",
  69.             "TikTok Views" => "fas fa-eye",
  70.             "TIKTOK Live Views" => "fas fa-eye",
  71.             "TIKTOK Auto views" => "fas fa-eye",
  72.             "TIKTOK Comments" => "fas fa-comment",
  73.             "TikTok Saves" => "far fa-bookmark",
  74.             "TikTok Shares" => "fas fa-share-alt",
  75.             "Youtube Views" => "fas fa-eye",
  76.             "Youtube Ads Views" => "fas fa-eye",
  77.             "Youtube Live Stream Views" => "fas fa-eye",
  78.             "Youtube Subscribers" => "fa-solid fa-user-plus",
  79.             "Youtube Likes" => "fas fa-thumbs-up",
  80.             "Youtube Short Likes" => "fas fa-thumbs-up",
  81.             "Youtube WATCHTIME" => "fas fa-clock",
  82.             "Youtube Short Views" => "fas fa-eye",
  83.             "Twitter Followers" => "fa-solid fa-user-plus",
  84.             "Twitter likes" => "fas fa-thumbs-up",
  85.             "Twitter retweets" => "fa-brands fa-twitter",
  86.             "Twitter Comments" => "fas fa-comment",
  87.             "Twitter Video Views" => "fas fa-eye",
  88.             "Twitter Direct Message" => "fa-solid fa-message",
  89.             "Twitch Follower" => "fa-solid fa-user-plus",
  90.             "TWITCH Live Views" => "fas fa-eye",
  91.             "TWITCH Monthly Live Views" => "fas fa-eye",
  92.             "Twitch Clip Views" => "fas fa-eye",
  93.             "Spotify Followers" => "fa-solid fa-user-plus",
  94.             "Spotify Premium Plays" => "fa-solid fa-circle-play",
  95.             "SoundCloud Followers" => "fa-solid fa-user-plus",
  96.             "SoundCloud Likes" => "fas fa-thumbs-up",
  97.             "SoundCloud Plays" => "fa-solid fa-circle-play",
  98.             "Clubhouse Followers" => "fa-solid fa-user-plus",
  99.             "Snapchat Followers" => "fa-solid fa-user-plus",
  100.             "Snapchat Story Views" => "fas fa-eye",
  101.             "Threads Followers" => "fa-solid fa-user-plus",
  102.             "Threads likes" => "fas fa-heart",
  103.             "Threads Reshare" => "fas fa-share-alt",
  104.             "Threads Comments" => "fas fa-comment",
  105.             "Telegram Members" => "fa-solid fa-user-plus",
  106.             "Telegram Views" => "fas fa-eye",
  107.             "Telegram Direct Messages" => "fas fa-comment",
  108.         ];
  109.     }
  110.     public function getOrderDetails(string $order)
  111.     {
  112.         $queryParams = [
  113.             "token=".$this->params->get('app.orderApiToken'),
  114.             "orderid=".$order
  115.         ];
  116.         $checkoutUrl $this->params->get('app.orderApiUrl') . '/' $this->params->get('app.orderApiCheckoutPrefix') . '?' implode("&"$queryParams);
  117.         // Call api to get menu
  118.         $response $this->client->request('GET'$checkoutUrl);
  119.         // Get result
  120.         $result $response->getContent();
  121.         // return the result
  122.         return json_decode(json_decode($result), true)['order'];
  123.     }
  124.     /***********************************************************************************
  125.      * 2023-03-06 - Implementation mèthode mouhsin
  126.      **********************************************************************************/
  127.     /**
  128.      * Format instagram username
  129.      */
  130.     public function formatInstagramUsername(string $username): string
  131.     {
  132.         $username str_replace('@'''strtolower(trim($username)));
  133.         if (strpos($username'https') !== false && strpos($username'instagram') !== false) {
  134.             // Extraire le nom d'utilisateur
  135.             $parsed_url parse_url($username); // Analyse l'URL
  136.             $path $parsed_url['path']; // Récupère le chemin de l'URL
  137.             $username trim($path'/'); // Supprime les slashs du début et de la fin
  138.         }
  139.         return $username;
  140.     }
  141.     /**
  142.      * Compose instagram api urls
  143.      */
  144.     public function getInstagramApiUrls(string $servicestring $typestring $username)
  145.     {
  146.         // Format instagram username
  147.         $username $this->formatInstagramUsername($username);
  148.         // Read instagram api url from configuration
  149.         $igApiUrl $this->params->get('app.igApiUrl');
  150.         // Read instagram api folders from configuration
  151.         $igRapidApiFolders $this->params->get('app.inspagramApis');
  152.         // Init instagram api urls list
  153.         $igRapidApiUrls = [];
  154.         // Compose final instagram api urls
  155.         foreach ($igRapidApiFolders as $key => $folder) {
  156.             $igRapidApiUrls[] = $igApiUrl '/' $service '/' $folder '/' $type '.php?username=' $username;
  157.         }
  158.         // Shuffle instagram api urls
  159.         shuffle($igRapidApiUrls);
  160.         // Return shuffled instagram api urls
  161.         return $igRapidApiUrls;
  162.     }
  163.     /**
  164.      * Format tiktok username
  165.      */
  166.     public function formatTikTokUsername(string $username): string
  167.     {
  168.         $username str_replace('@'''strtolower(trim($username)));
  169.         if (strpos($username'https') !== false && strpos($username'tiktok') !== false) {
  170.             $parsed_url parse_url($username);
  171.             $path $parsed_url['path'];
  172.             $username trim($path'/');
  173.         }
  174.         return $username;
  175.     }
  176.     /**
  177.      * Compose instagram api urls
  178.      */
  179.     public function getTikTokApiUrls(string $servicestring $typestring $username)
  180.     {
  181.         // Format instagram username
  182.         $username $this->formatTikTokUsername($username);
  183.         // Read instagram api url from configuration
  184.         $tikTokApiUrl $this->params->get('app.tiktokApiUrl');
  185.         // Read instagram api folders from configuration
  186.         $tikTokRapidApiFolders $this->params->get('app.tiktokApis');
  187.         // Init instagram api urls list
  188.         $tikTokRapidApiUrls = [];
  189.         // Compose final instagram api urls
  190.         foreach ($tikTokRapidApiFolders as $key => $folder) {
  191.             $tikTokRapidApiUrls[] = $tikTokApiUrl '/' $service '/' $folder '/' $type '.php?username=' $username;
  192.         }
  193.         // Shuffle instagram api urls
  194.         shuffle($tikTokRapidApiUrls);
  195.         // Return shuffled instagram api urls
  196.         return $tikTokRapidApiUrls;
  197.     }
  198.     public function executeApiUrls(string $servicestring $typestring $username)
  199.     {
  200.         $this->logger->info("============================== START executeApiUrls ($service$type$username) ==============================");
  201.         if ($service == 'instagram') {
  202.             $rapidApiUrls $this->getInstagramApiUrls($service$type$username);
  203.         } elseif ($service == 'tiktok') {
  204.             $rapidApiUrls $this->getTikTokApiUrls($service$type$username);
  205.         } else {
  206.             throw new \Exception("Invalid service name"1);
  207.         }
  208.         $validResultFound false;
  209.         foreach ($rapidApiUrls as $key => $rapidApiUrl) {
  210.             $this->logger->info('This is an informational message.');
  211.             set_time_limit(0);
  212.             $ch curl_init();
  213.             
  214.             $this->logger->info('Call api ' $key ' : ', [$rapidApiUrl]);
  215.             curl_setopt($chCURLOPT_URL$rapidApiUrl);
  216.             curl_setopt($chCURLOPT_RETURNTRANSFER1);
  217.             curl_setopt($chCURLOPT_USERAGENT'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36');
  218.             curl_setopt($chCURLOPT_HTTPHEADER, array(
  219.                 "Origin: https://up-social-media.com",
  220.                 "Content-Type: application/json"
  221.             ));
  222.             curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  223.             curl_setopt($chCURLOPT_TIMEOUT5);
  224.             $response curl_exec($ch);
  225.             $this->logger->info('Response api ' $key ' : ', [$response]);
  226.             if ($response === false) {
  227.                 $errno curl_errno($ch);
  228.                 curl_close($ch);
  229.                 continue;
  230.             }
  231.             $httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
  232.             if ($httpCode != 200) {
  233.                 curl_close($ch);
  234.                 continue;
  235.             }
  236.             curl_close($ch);
  237.             $data json_decode($responsetrue);
  238.             $this->logger->info('Result : '$data);
  239.             if (count($data) == or $data == '[]' or empty($data)) {
  240.                 continue;
  241.             } else {
  242.                 $validResultFound true;
  243.                 $this->logger->info("============================== END executeApiUrls ($service$type$username) ==============================");
  244.                 return $data;
  245.             }
  246.         }
  247.         if (!$validResultFound) {
  248.             $this->logger->info("============================== END executeApiUrls ($service$type$username) ==============================");
  249.             return array();
  250.         }
  251.     }
  252. }