Thread: [Solved] CURL: Downloanding an page ?
View Single Post
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 05-01-2022 , 08:30   Re: CURL: Downloanding an page ?
Reply With Quote #20

The Code works Fine Adi, sometimes it takes longer to update the rank, situation where you change your name or a new Player enters the server, but i'll let a message for the players to know, also i removed score p.m. was pointless for deathrun and i've changed iRank from Int to String, also i had to insert the server IP manualy, heres the working code:

PHP Code:
#include <amxmodx>
#include <cromchat>
#include <curl>
 
#if !defined MAX_IP_WITH_PORT_LENGTH
#define MAX_IP_WITH_PORT_LENGTH 22
#endif
 
enum _:PlayerData
{
    
iScore,
    
iMinutes,
    
iRank[14]
}
 
new 
g_ePlayerData[MAX_PLAYERS 1][PlayerData]
 
public 
plugin_init()
{
    
register_plugin"GameTracker Stats""xxx""Shadow_adi" );
 
    
register_clcmd("say /playedtime""SendRequest")
    
register_clcmd("say .playedtime""SendRequest")
    
register_clcmd("say /ore""SendRequest")
    
register_clcmd("say .ore""SendRequest")
    
register_clcmd("say /rank""SendRequest")
    
register_clcmd("say rank""SendRequest")
}
 
public 
SendRequest(id)
{
    new 
CURL:curl curl_easy_init();
 
    new 
szTemp[100];
 
 
    new 
iID[1]
    
iID[0] = id
 
    
new szName[32];
    
get_user_nameidszNamecharsmax(szName) );
 
    new 
curl_slist:header
 
    header 
curl_slist_append(header"text/html; charset=UTF-8");
    
header curl_slist_append(header"User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.126 Mobile Safari/537.36");
 
    
formatex(szTempcharsmax(szTemp), "https://www.gametracker.com/player/%s/MY_SERVER_IP_HERE/"szName );
 
    if(
curl)
    {
        
curl_easy_setopt(curlCURLOPT_URLszTemp);
        
curl_easy_setopt(curlCURLOPT_COOKIE1);
        
curl_easy_setopt(curlCURLOPT_HTTPHEADERheader)
        
curl_easy_setopt(curlCURLOPT_CUSTOMREQUEST"GET");
        
curl_easy_setopt(curlCURLOPT_FOLLOWLOCATIONtrue);
 
        
curl_easy_setopt(curlCURLOPT_WRITEFUNCTION"ReceiveData");
        
curl_easy_setopt(curlCURLOPT_WRITEDATAid);
        
curl_easy_setopt(curlCURLOPT_BUFFERSIZE1028);
 
        
curl_easy_setopt(curlCURLOPT_SSL_VERIFYPEER1);
        
curl_easy_setopt(curlCURLOPT_SSL_VERIFYHOST2);
        
curl_easy_setopt(curlCURLOPT_CAINFO"cstrike/addons/amxmodx/cacert.pem");
        
curl_easy_perform(curl"CurlCallback"iIDsizeof(iID) );
    }
}
 
public 
ReceiveData(data[], sizenmembid)
{
    
// The position of the "Score: " div
    
new iPos containi(data"Score:")
 
    if(
iPos != -1)
    {
        
g_ePlayerData[id][iScore] = str_to_num(data[iPos 23])
    }
 
    
iPos containi(data"Minutes Played:")
 
    if(
iPos != -1)
    {
        
g_ePlayerData[id][iMinutes] = str_to_num(data[iPos 32])
    }
 
    
iPos containi(data"Rank on Server:")
 
    if(
iPos != -1)
    {
        
//g_ePlayerData[id][iRank] = str_to_num(data[iPos + 93])
    
formatexg_ePlayerData[id][iRank], charsmaxg_ePlayerData[] ), data[iPos+93] );
    }
 
    return 
size nmemb
}
 
public 
CurlCallback(CURL:cURLHandle CURLcode:codeiID[])
{
    if(
code != CURLE_OK)
    {
        new 
szError[CURL_ERROR_SIZE]
        
curl_easy_strerror(codeszError,charsmax(szError))
        
log_amx("cURL error: %d ^"%s^""codeszError);
    }
 
    new 
id iID[0]


    if( 
g_ePlayerData[id][iScore] >= )
        
CC_SendMessageid" &x01[ &x03Gametracker Stats&x01 ] Score: &x07%d&x01 , Hours: &x07%d&x01 , Rank: &x07%s&x01."g_ePlayerData[id][iScore] ,  g_ePlayerData[id][iMinutes] / 60, \
         
g_ePlayerData[id][iRank] );
    else
        
CC_SendMessageid" &x01[ &x03Gametracker Stats&x01 ] Your stats haven't been updated yet, try later while gametracker works on it ." );

 
    
curl_easy_cleanup(cURLHandle);



Last edited by Craxor; 05-01-2022 at 08:31.
Craxor is offline
Send a message via ICQ to Craxor