<?php
namespace App\Service;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class Api
{
private $params;
private $client;
private $session;
private $logger;
public function __construct(HttpClientInterface $client, ContainerBagInterface $params, RequestStack $requestStack, LoggerInterface $logger)
{
$this->params = $params;
$this->client = $client;
$this->logger = $logger;
$this->session = $requestStack->getSession();
}
public function getAllServices()
{
// Check if menu is already stored in session
if ($this->session->has("apiMenu")) {
// Return the stored menu
// return $this->session->get("apiMenu");
}
// Call api to get menu
$response = $this->client->request('GET', $this->params->get('app.orderApiUrl').'/services/GetAll');
// Get result
$result = $response->toArray();
// Store the result to session
$this->session->set("apiMenu", $result);
// return the result
return $result;
}
public function getServicesIcons()
{
return [
"Followers" => "fa-solid fa-user-plus",
"Reel Likes" => "fas fa-heart",
"Likes" => "fas fa-heart",
"Automatic Likes" => "fas fa-heart",
"Views" => "fas fa-eye",
"Reel Views" => "fas fa-eye",
"IGTV Views" => "fas fa-eye",
"IG Impressions" => "fas fa-eye",
"Live Stream Views" => "fas fa-eye",
"Post Reach" => "fas fa-eye",
"Story Views" => "fas fa-eye",
"Automatic Views" => "fas fa-eye",
"Custom Comments" => "fas fa-comment",
"Random Comments" => "fas fa-comment",
"Auto Comments" => "fas fa-comment",
"Ig Direct Messages" => "far fa-paper-plane",
"FB Page Likes & Followers" => "far fa-thumbs-up",
"FB Profile Followers" => "fa-solid fa-user-plus",
"FB Post Likes" => "far fa-thumbs-up",
"FB Post Reaction Care" => "fas fa-grin-hearts",
"FB Post Reaction Haha" => "fas fa-smile",
"FB Post Reaction Love" => "far fa-grin-hearts",
"FB Post Reaction Wow" => "far fa-surprise",
"FB Video Views" => "fas fa-eye",
"FB Comments" => "fas fa-comment",
"FB Group Members" => "fa-solid fa-user-plus",
"TikTok Followers" => "fa-solid fa-user-plus",
"TikTok Likes" => "fas fa-heart",
"TIKTOK Auto Likes" => "fas fa-heart",
"TikTok Battle Likes" => "fas fa-heart",
"TikTok Views" => "fas fa-eye",
"TIKTOK Live Views" => "fas fa-eye",
"TIKTOK Auto views" => "fas fa-eye",
"TIKTOK Comments" => "fas fa-comment",
"TikTok Saves" => "far fa-bookmark",
"TikTok Shares" => "fas fa-share-alt",
"Youtube Views" => "fas fa-eye",
"Youtube Ads Views" => "fas fa-eye",
"Youtube Live Stream Views" => "fas fa-eye",
"Youtube Subscribers" => "fa-solid fa-user-plus",
"Youtube Likes" => "fas fa-thumbs-up",
"Youtube Short Likes" => "fas fa-thumbs-up",
"Youtube WATCHTIME" => "fas fa-clock",
"Youtube Short Views" => "fas fa-eye",
"Twitter Followers" => "fa-solid fa-user-plus",
"Twitter likes" => "fas fa-thumbs-up",
"Twitter retweets" => "fa-brands fa-twitter",
"Twitter Comments" => "fas fa-comment",
"Twitter Video Views" => "fas fa-eye",
"Twitter Direct Message" => "fa-solid fa-message",
"Twitch Follower" => "fa-solid fa-user-plus",
"TWITCH Live Views" => "fas fa-eye",
"TWITCH Monthly Live Views" => "fas fa-eye",
"Twitch Clip Views" => "fas fa-eye",
"Spotify Followers" => "fa-solid fa-user-plus",
"Spotify Premium Plays" => "fa-solid fa-circle-play",
"SoundCloud Followers" => "fa-solid fa-user-plus",
"SoundCloud Likes" => "fas fa-thumbs-up",
"SoundCloud Plays" => "fa-solid fa-circle-play",
"Clubhouse Followers" => "fa-solid fa-user-plus",
"Snapchat Followers" => "fa-solid fa-user-plus",
"Snapchat Story Views" => "fas fa-eye",
"Threads Followers" => "fa-solid fa-user-plus",
"Threads likes" => "fas fa-heart",
"Threads Reshare" => "fas fa-share-alt",
"Threads Comments" => "fas fa-comment",
"Telegram Members" => "fa-solid fa-user-plus",
"Telegram Views" => "fas fa-eye",
"Telegram Direct Messages" => "fas fa-comment",
];
}
public function getOrderDetails(string $order)
{
$queryParams = [
"token=".$this->params->get('app.orderApiToken'),
"orderid=".$order
];
$checkoutUrl = $this->params->get('app.orderApiUrl') . '/' . $this->params->get('app.orderApiCheckoutPrefix') . '?' . implode("&", $queryParams);
// Call api to get menu
$response = $this->client->request('GET', $checkoutUrl);
// Get result
$result = $response->getContent();
// return the result
return json_decode(json_decode($result), true)['order'];
}
function printr($data) {
echo "<pre>" . print_r($data,true) . "</pre>";
}
public function relaunchOrder(string $order)
{
$queryParams = [
"token=" . $this->params->get('app.orderApiToken'),
"bkiorder=" . $order
];
$url = $this->params->get('app.orderApiUrl') . '/log/RelaunchBKOrder?' . implode("&", $queryParams);
$response = $this->client->request('GET', $url);
// Get the result
$result = $response->getContent();
// Check if the result starts with a valid XML tag
if (strpos($result, '<string') !== false) {
// Try to load the XML
$xml = simplexml_load_string($result);
if ($xml !== false) {
// Return the XML value as a string, without quotes
return trim((string) $xml);
}
}
// If it's not XML, try to extract the value directly and strip any quotes
return trim(strip_tags($result));
}
/***********************************************************************************
* 2023-03-06 - Implementation mèthode mouhsin
**********************************************************************************/
/**
* Format instagram username
*/
public function formatInstagramUsername(string $username): string
{
$username = str_replace('@', '', strtolower(trim($username)));
if (strpos($username, 'https') !== false && strpos($username, 'instagram') !== false) {
// Extraire le nom d'utilisateur
$parsed_url = parse_url($username); // Analyse l'URL
$path = $parsed_url['path']; // Récupère le chemin de l'URL
$username = trim($path, '/'); // Supprime les slashs du début et de la fin
}
return $username;
}
/**
* Compose instagram api urls
*/
public function getInstagramApiUrls(string $service, string $type, string $username,$token=null)
{
$domaineName = 'https://wizzopp.com';
$loadMedia = ($type === 'posts' || $type === 'reels') ? 'true' : 'false';
$url = "$domaineName/api/fetch/$service/$username?loadMedia=$loadMedia&website=sulu";
if ($type === 'reels') {
$url .= '&reels=true';
}
if (!empty($token)) {
$url .= "&token=$token";
}
$urls=['url'=>$url];
return $urls;
//
// // Format instagram username
// $username = $this->formatInstagramUsername($username);
//
// // Read instagram api url from configuration
// $igApiUrl = $this->params->get('app.igApiUrl');
//
// // Read instagram api folders from configuration
// $igRapidApiFolders = $this->params->get('app.inspagramApis');
//
// // Init instagram api urls list
// $igRapidApiUrls = [];
//
// // Compose final instagram api urls
// foreach ($igRapidApiFolders as $key => $folder) {
// $igRapidApiUrls[] = $igApiUrl . '/' . $service . '/' . $folder . '/' . $type . '.php?username=' . $username;
// }
//
// // Shuffle instagram api urls
// shuffle($igRapidApiUrls);
//
// // Return shuffled instagram api urls
// return $igRapidApiUrls;
}
/**
* Format tiktok username
*/
public function formatTikTokUsername(string $username): string
{
$username = str_replace('@', '', strtolower(trim($username)));
if (strpos($username, 'https') !== false && strpos($username, 'tiktok') !== false) {
$parsed_url = parse_url($username);
$path = $parsed_url['path'];
$username = trim($path, '/');
}
return $username;
}
public function getTiktokApiUrls(string $service, string $type, string $username)
{
//$domaineName = 'http://socialapi.test';
$domaineName = 'https://wizzopp.com';
$loadMedia='false';
if($type=='posts' or $type=='reels'){
$loadMedia='true';
}
$urls=['url'=>"$domaineName/api/fetch/$service/$username?loadMedia=$loadMedia&type=$type&website=sulu"];
return $urls;
}
/**
* Compose instagram api urls
*/
// public function getTikTokApiUrls(string $service, string $type, string $username)
// {
//
// // Format instagram username
// $username = $this->formatTikTokUsername($username);
//
// // Read instagram api url from configuration
// $tikTokApiUrl = $this->params->get('app.tiktokApiUrl');
//
// // Read instagram api folders from configuration
// $tikTokRapidApiFolders = $this->params->get('app.tiktokApis');
//
// // Init instagram api urls list
// $tikTokRapidApiUrls = [];
//
// // Compose final instagram api urls
// foreach ($tikTokRapidApiFolders as $key => $folder) {
// $tikTokRapidApiUrls[] = $tikTokApiUrl . '/' . $service . '/' . $folder . '/' . $type . '.php?username=' . $username;
// }
//
// // Shuffle instagram api urls
// shuffle($tikTokRapidApiUrls);
//
// // Return shuffled instagram api urls
// return $tikTokRapidApiUrls;
// }
public function executeApiUrls(string $service, string $type, string $username,$token=null)
{
$this->logger->info("============================== START executeApiUrls ($service, $type, $username) ==============================");
if ($service == 'instagram') {
$rapidApiUrls = $this->getInstagramApiUrls($service, $type, $username,$token);
} elseif ($service == 'tiktok') {
$rapidApiUrls = $this->getTikTokApiUrls($service, $type, $username);
} else {
throw new \Exception("Invalid service name", 1);
}
$validResultFound = false;
foreach ($rapidApiUrls as $key => $rapidApiUrl) {
$this->logger->info('This is an informational message.');
set_time_limit(0);
$ch = curl_init();
$this->logger->info('Call api ' . $key . ' : ', [$rapidApiUrl]);
curl_setopt($ch, CURLOPT_URL, $rapidApiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Origin: https://up-social-media.com",
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$response = curl_exec($ch);
$this->logger->info('Response api ' . $key . ' : ', [$response]);
if ($response === false) {
$errno = curl_errno($ch);
curl_close($ch);
continue;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
curl_close($ch);
continue;
}
curl_close($ch);
$data = json_decode($response, true);
$this->logger->info('Result : ', $data);
if (count($data) == 0 or $data == '[]' or empty($data)) {
continue;
} else {
$validResultFound = true;
$this->logger->info("============================== END executeApiUrls ($service, $type, $username) ==============================");
return $data;
}
}
if (!$validResultFound) {
$this->logger->info("============================== END executeApiUrls ($service, $type, $username) ==============================");
return array();
}
}
}