AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   CURL Help (https://forums.alliedmods.net/showthread.php?t=332415)

lexzor 05-11-2021 11:00

CURL Help
 
hello. I'm trying to make an autoupdater for a plugin with curl, but i don t get how to use it. I was looking up on Bugsy's vacbans when i realized that i don't understand a single thing of that curl.

that is the code i was trying to make
PHP Code:

#include <amxmodx>
#include <curl>

#define CURL_BUFFERSIZE 4096

public plugin_init()
{
    
register_plugin("CURL Test""1.0""-");
}

public 
curl_test()
{
    new 
szCURL_url[128]
    new 
szLatestVersion[4]
    new 
CURL:cURLHandle
    
new const szCurlErrorBuffer[CURL_BUFFERSIZE]

    
formatex(szCURL_urlcharsmax(szCURL_url), "https://lexzor.warface.ro/AutoUpdate/Test/version.txt")

    
cURLHandle curl_easy_init()

    if(
cURLHandle)
    {
        
curl_easy_setopt(cURLHandleCURLOPT_URLszCURL_url)
        
curl_easy_setopt(cURLHandleCURLOPT_CAINFOszLatestVersion )
    }

    


i didn't find any info about const CURLOPT_CAINFO in .inc file so i don t know how to use it.

First of all, i tried to read a site: https://lexzor.warface.ro/AutoUpdate/Test/version.txt

From links like this i would like to read latest versions of my plugins.

Can i get some help with this ?

HamletEagle 05-11-2021 11:16

Re: CURL Help
 
You already got a link to documentation in the other thread. https://curl.se/libcurl/c/
Use it.

For example, for CURLOPT_CAINFO you would click on curl_easy_setopt, then search for CURLOPT_CAINFO and finally get here: https://curl.se/libcurl/c/CURLOPT_CAINFO.html

Also google for curl tutorials and examples. They may be in C, but the API is very similar.
If you use linux or WSL you can always play with "curl" from command line to get a better feel for how it works. For example:
Code:

curl https://lexzor.warface.ro/AutoUpdate/Test/version.txt
will give you:

Code:

snip:~$ curl https://lexzor.warface.ro/AutoUpdate/Test/version.txt
999
snip:~$


lexzor 05-11-2021 11:43

Re: CURL Help
 
I don't understand a shit, i need smth like a tutorial, but i m giving up.

thanks 4 ur help

Bugsy 05-11-2021 21:51

Re: CURL Help
 
You will not find an AMX-X tutorial for cURL, but as HamletEagle said, find an example written in C and just code it in AMX-X as the API is pretty much identical.

Napoleon_be 05-12-2021 03:19

Re: CURL Help
 
Have you checked this out? https://github.com/Polarhigh/AmxxCur...s/http_get.sma

And this? https://forums.alliedmods.net/showthread.php?t=285656

What are you trying to achieve?

lexzor 05-12-2021 06:29

Re: CURL Help
 
Every time i get another output, but in console because i can t write the file.

PHP Code:

#include <amxmodx>
#include <curl>

#define CURL_BUFFER_SIZE 4096

public plugin_init()
{
    
register_plugin("CURL Test""1.0""-");

    
set_task(0.1"curl_test")
}

public 
curl_test()
{
    new 
szCURL_url[128]
    new 
CURL:cURLHandle
    
new data[1]
    
data[0] = fopen("addons/amxmodx/data/test.txt""wb")

    
formatex(szCURL_urlcharsmax(szCURL_url), "https://lexzor.warface.ro/AutoUpdate/Test/version.txt")

    
cURLHandle curl_easy_init()

    if(
cURLHandle)
    {
        
curl_easy_setopt(cURLHandleCURLOPT_BUFFERSIZECURL_BUFFER_SIZE)
        
curl_easy_setopt(cURLHandleCURLOPT_URLg_CURL_URL)
        
curl_easy_setopt(cURLHandleCURLOPT_WRITEDATAdata[0])
        
curl_easy_setopt(cURLHandleCURLOPT_WRITEFUNCTION"write" )
        
curl_easy_perform(cURLHandle"complite"datasizeof(data))
        
server_print("%i"data[0])
    }
}

public 
write (data[], sizenmembfile)
{
   new 
actual_size size nmemb;
   
// server_print("%i", actual_size)
   
fwrite_blocks(filedataactual_sizeBLOCK_CHAR)
  
// server_print("%s : %s", file, data)
   
return actual_size
}

public 
complite(CURL:curlCURLcode:codedata[])
{
  if(
code == CURLE_WRITE_ERROR)
     
server_print("transfer aborted")
  else
     
server_print("curl complete")
  
  
fclose(data[0])

  
curl_easy_cleanup(curl)



HamletEagle 05-12-2021 11:34

Re: CURL Help
 
You have to access the data when the callback is called, printing it right after you send the request is useless and prone to race conditions.

Bugsy 05-12-2021 15:13

Re: CURL Help
 
Your above code successfully wrote a text file for me containing '998', what is the problem?

Please explain what you are trying to do.

lexzor 05-13-2021 11:26

Re: CURL Help
 
I m trying to make an auto updater using curl. I want to check the version from a .txt file and if is not the same with plugin verison then download the plugin from webhost.

The problem with the above code is that i get no output data in .txt file.

In first instance i was thinking that i don't install correctly the curl module and that was right, in modules.ini i wrote only "curl" then i saw a post here and modify it in curl_amxx, but i get the same result:
- no data .txt file.
- different output in console
- First map change: 167226000
- Second map change: 167339376
and so on...
every time different output.

I m using amxx 1.9. Does it matter if i'm using hlds or rehlds?

Bugsy 05-13-2021 19:53

Re: CURL Help
 
No need to write to a local file
PHP Code:


#include <amxmodx>
#include <curl>

#define CURL_BUFFER_SIZE 4096

new g_iNewVersion;

public 
plugin_init()
{
    
register_plugin("CURL Test""1.0""-");
    
    
IsUpgradeNeeded997 );
    
//IsUpgradeNeeded( 998 );
}

public 
IsUpgradeNeedediCurrentVersion )
{
    new 
szCURL_url[128]
    new 
CURL:cURLHandle
    
new data[8]
    
    
num_to_striCurrentVersion data charsmaxdata ) );
    
    
copy(szCURL_urlcharsmax(szCURL_url), "https://lexzor.warface.ro/AutoUpdate/Test/version.txt")
    
    
cURLHandle curl_easy_init()
    
    if(
cURLHandle)
    {
        
curl_easy_setopt(cURLHandleCURLOPT_BUFFERSIZECURL_BUFFER_SIZE)
        
curl_easy_setopt(cURLHandleCURLOPT_URLszCURL_url)
        
curl_easy_setopt(cURLHandleCURLOPT_WRITEDATAdata[0])
        
curl_easy_setopt(cURLHandleCURLOPT_WRITEFUNCTION"WriteFunction" )
        
curl_easy_perform(cURLHandle"CurlCallback"datasizeof(data))
    }
}

public 
WriteFunction (data[], sizenmembfile)
{
    new 
actual_size size nmemb;
    
    
g_iNewVersion str_to_numdata );
    
    return 
actual_size
}

public 
CurlCallback(CURL:curlCURLcode:codedata[])
{
    if(
code == CURLE_WRITE_ERROR)
    {
        
server_print("transfer aborted")
    }
    else
    {
        
server_print("curl complete")
    
        new 
iCurrentVersion str_to_numdata );
    
        
server_print"Upgrade %s needed [Is %d > %d ?]" , ( g_iNewVersion iCurrentVersion ) ? "is" "not" g_iNewVersion iCurrentVersion );
    }
    
    
curl_easy_cleanup(curl)


IsUpgradeNeeded( 997 );
Output:
Code:

Upgrade is needed [998 > 997]
IsUpgradeNeeded( 998 );
Output:
Code:

Upgrade not needed [Is 998 > 998 ?]


All times are GMT -4. The time now is 02:29.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.