src/Service/Api.php line 28

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.     function printr($data) {
  125.         echo  "<pre>" print_r($data,true) . "</pre>";
  126.     }
  127.     public function relaunchOrder(string $order)
  128.     {
  129.         $queryParams = [
  130.             "token=" $this->params->get('app.orderApiToken'),
  131.             "bkiorder=" $order
  132.         ];
  133.         $url $this->params->get('app.orderApiUrl') . '/log/RelaunchBKOrder?' implode("&"$queryParams);
  134.         $response $this->client->request('GET'$url);
  135.         // Get the result
  136.         $result $response->getContent();
  137.         // Check if the result starts with a valid XML tag
  138.         if (strpos($result'<string') !== false) {
  139.             // Try to load the XML
  140.             $xml simplexml_load_string($result);
  141.             if ($xml !== false) {
  142.                 // Return the XML value as a string, without quotes
  143.                 return trim((string) $xml);
  144.             }
  145.         }
  146.         // If it's not XML, try to extract the value directly and strip any quotes
  147.         return trim(strip_tags($result));
  148.     }
  149.     /***********************************************************************************
  150.      * 2023-03-06 - Implementation mèthode mouhsin
  151.      **********************************************************************************/
  152.     /**
  153.      * Format instagram username
  154.      */
  155.     public function formatInstagramUsername(string $username): string
  156.     {
  157.         $username str_replace('@'''strtolower(trim($username)));
  158.         if (strpos($username'https') !== false && strpos($username'instagram') !== false) {
  159.             // Extraire le nom d'utilisateur
  160.             $parsed_url parse_url($username); // Analyse l'URL
  161.             $path $parsed_url['path']; // Récupère le chemin de l'URL
  162.             $username trim($path'/'); // Supprime les slashs du début et de la fin
  163.         }
  164.         return $username;
  165.     }
  166.     /**
  167.      * Compose instagram api urls
  168.      */
  169.     public function getInstagramApiUrls(string $servicestring $typestring $username,$token=null)
  170.     {
  171.         $domaineName 'https://wizzopp.com';
  172.         $loadMedia = ($type === 'posts' || $type === 'reels') ? 'true' 'false';
  173.         $url "$domaineName/api/fetch/$service/$username?loadMedia=$loadMedia&website=sulu";
  174.         if ($type === 'reels') {
  175.             $url .= '&reels=true';
  176.         }
  177.         if (!empty($token)) {
  178.             $url .= "&token=$token";
  179.         }
  180.         $urls=['url'=>$url];
  181.         return $urls;
  182. //
  183. //        // Format instagram username
  184. //        $username = $this->formatInstagramUsername($username);
  185. //
  186. //        // Read instagram api url from configuration
  187. //        $igApiUrl = $this->params->get('app.igApiUrl');
  188. //
  189. //        // Read instagram api folders from configuration
  190. //        $igRapidApiFolders = $this->params->get('app.inspagramApis');
  191. //
  192. //        // Init instagram api urls list
  193. //        $igRapidApiUrls = [];
  194. //
  195. //        // Compose final instagram api urls
  196. //        foreach ($igRapidApiFolders as $key => $folder) {
  197. //            $igRapidApiUrls[] = $igApiUrl . '/' . $service . '/' . $folder . '/' . $type . '.php?username=' . $username;
  198. //        }
  199. //
  200. //        // Shuffle instagram api urls
  201. //        shuffle($igRapidApiUrls);
  202. //
  203. //        // Return shuffled instagram api urls
  204. //        return $igRapidApiUrls;
  205.     }
  206.     /**
  207.      * Format tiktok username
  208.      */
  209.     public function formatTikTokUsername(string $username): string
  210.     {
  211.         $username str_replace('@'''strtolower(trim($username)));
  212.         if (strpos($username'https') !== false && strpos($username'tiktok') !== false) {
  213.             $parsed_url parse_url($username);
  214.             $path $parsed_url['path'];
  215.             $username trim($path'/');
  216.         }
  217.         return $username;
  218.     }
  219.     public function getTiktokApiUrls(string $servicestring $typestring $username)
  220.     {
  221.         //$domaineName = 'http://socialapi.test';
  222.         $domaineName 'https://wizzopp.com';
  223.         $loadMedia='false';
  224.         if($type=='posts' or $type=='reels'){
  225.             $loadMedia='true';
  226.         }
  227.         $urls=['url'=>"$domaineName/api/fetch/$service/$username?loadMedia=$loadMedia&type=$type&website=sulu"];
  228.         return $urls;
  229.     }
  230.     /**
  231.      * Compose instagram api urls
  232.      */
  233. //    public function getTikTokApiUrls(string $service, string $type, string $username)
  234. //    {
  235. //
  236. //        // Format instagram username
  237. //        $username = $this->formatTikTokUsername($username);
  238. //
  239. //        // Read instagram api url from configuration
  240. //        $tikTokApiUrl = $this->params->get('app.tiktokApiUrl');
  241. //
  242. //        // Read instagram api folders from configuration
  243. //        $tikTokRapidApiFolders = $this->params->get('app.tiktokApis');
  244. //
  245. //        // Init instagram api urls list
  246. //        $tikTokRapidApiUrls = [];
  247. //
  248. //        // Compose final instagram api urls
  249. //        foreach ($tikTokRapidApiFolders as $key => $folder) {
  250. //            $tikTokRapidApiUrls[] = $tikTokApiUrl . '/' . $service . '/' . $folder . '/' . $type . '.php?username=' . $username;
  251. //        }
  252. //
  253. //        // Shuffle instagram api urls
  254. //        shuffle($tikTokRapidApiUrls);
  255. //
  256. //        // Return shuffled instagram api urls
  257. //        return $tikTokRapidApiUrls;
  258. //    }
  259.     public function executeApiUrls(string $servicestring $typestring $username,$token=null)
  260.     {
  261.         $this->logger->info("============================== START executeApiUrls ($service$type$username) ==============================");
  262.         if ($service == 'instagram') {
  263.             $rapidApiUrls $this->getInstagramApiUrls($service$type$username,$token);
  264.         } elseif ($service == 'tiktok') {
  265.             $rapidApiUrls $this->getTikTokApiUrls($service$type$username);
  266.         } else {
  267.             throw new \Exception("Invalid service name"1);
  268.         }
  269.         $validResultFound false;
  270.         foreach ($rapidApiUrls as $key => $rapidApiUrl) {
  271.             $this->logger->info('This is an informational message.');
  272.             set_time_limit(0);
  273.             $ch curl_init();
  274.             
  275.             $this->logger->info('Call api ' $key ' : ', [$rapidApiUrl]);
  276.             curl_setopt($chCURLOPT_URL$rapidApiUrl);
  277.             curl_setopt($chCURLOPT_RETURNTRANSFER1);
  278.             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');
  279.             curl_setopt($chCURLOPT_HTTPHEADER, array(
  280.                 "Origin: https://up-social-media.com",
  281.                 "Content-Type: application/json"
  282.             ));
  283.             curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  284.             curl_setopt($chCURLOPT_TIMEOUT5);
  285.             $response curl_exec($ch);
  286.             $this->logger->info('Response api ' $key ' : ', [$response]);
  287.             if ($response === false) {
  288.                 $errno curl_errno($ch);
  289.                 curl_close($ch);
  290.                 continue;
  291.             }
  292.             $httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
  293.             if ($httpCode != 200) {
  294.                 curl_close($ch);
  295.                 continue;
  296.             }
  297.             curl_close($ch);
  298.             $data json_decode($responsetrue);
  299.             $this->logger->info('Result : '$data);
  300.             if (count($data) == or $data == '[]' or empty($data)) {
  301.                 continue;
  302.             } else {
  303.                 $validResultFound true;
  304.                 $this->logger->info("============================== END executeApiUrls ($service$type$username) ==============================");
  305.                 return $data;
  306.             }
  307.         }
  308.         if (!$validResultFound) {
  309.             $this->logger->info("============================== END executeApiUrls ($service$type$username) ==============================");
  310.             return array();
  311.         }
  312.     }
  313. }