Raised This Month: $32 Target: $400
 8% 

CURL Help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
lexzor
Veteran Member
Join Date: Nov 2020
Old 05-11-2021 , 11:00   CURL Help
Reply With Quote #1

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 ?

Last edited by lexzor; 05-11-2021 at 11:21.
lexzor is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-11-2021 , 11:16   Re: CURL Help
Reply With Quote #2

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:~$
__________________

Last edited by HamletEagle; 05-11-2021 at 11:19.
HamletEagle is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 05-11-2021 , 11:43   Re: CURL Help
Reply With Quote #3

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

thanks 4 ur help

Last edited by lexzor; 05-11-2021 at 11:45.
lexzor is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-11-2021 , 21:51   Re: CURL Help
Reply With Quote #4

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.
__________________
Bugsy is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 05-12-2021 , 03:19   Re: CURL Help
Reply With Quote #5

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?
__________________

Last edited by Napoleon_be; 05-12-2021 at 03:19.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
lexzor
Veteran Member
Join Date: Nov 2020
Old 05-12-2021 , 06:29   Re: CURL Help
Reply With Quote #6

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)


Last edited by lexzor; 05-12-2021 at 06:30.
lexzor is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-12-2021 , 11:34   Re: CURL Help
Reply With Quote #7

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.
__________________
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-12-2021 , 15:13   Re: CURL Help
Reply With Quote #8

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

Please explain what you are trying to do.
__________________
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 05-13-2021 , 11:26   Re: CURL Help
Reply With Quote #9

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?

Last edited by lexzor; 05-13-2021 at 12:07.
lexzor is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-13-2021 , 19:53   Re: CURL Help
Reply With Quote #10

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 ?]
__________________

Last edited by Bugsy; 05-13-2021 at 19:56.
Bugsy is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:52.


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