CURL request POST and GET [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

CURL request POST and GET [Resolved]

Post by Kwai chang caine »

Hello at all

Thanks to INFRATEC and several members of this forum, i believe i have a little bit understand how use CURL :D
I have try to send a GET request without success, CURL not see the parameter send

For ask at PB forum to search "KCC", i want send that :
But the result is the full search panel of PB, not the list of thread of KCC :shock:

If someone see where is the problem :|

Code: Select all

; Based on INFRATEC code ReceiveHTTPToMemory() http://www.purebasic.fr/english/viewtopic.php?p=490891#p490891

IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl

Global *ReceiveHTTPToMemoryBuffer, ReceiveHTTPToMemoryBufferPtr.i

#Requette_GET = 0
#Requette_POST = 1

ProcedureC ReceiveHTTPWriteToMemoryFunction(*ptr, Size.i, NMemB.i, *Stream)
 
 Protected SizeProper.i  = Size & 255
 Protected NMemBProper.i = NMemB
 
 If *ReceiveHTTPToMemoryBuffer = 0
  *ReceiveHTTPToMemoryBuffer = AllocateMemory(SizeProper * NMemBProper)
 Else
  *ReceiveHTTPToMemoryBuffer = ReAllocateMemory(*ReceiveHTTPToMemoryBuffer, MemorySize(*ReceiveHTTPToMemoryBuffer) + SizeProper * NMemBProper)
 EndIf
 
 CopyMemory(*ptr, *ReceiveHTTPToMemoryBuffer + ReceiveHTTPToMemoryBufferPtr, SizeProper * NMemBProper)
 ReceiveHTTPToMemoryBufferPtr + SizeProper * NMemBProper
 
 ProcedureReturn SizeProper * NMemBProper
 
EndProcedure

Procedure.i RequetteHTTP(Adresse.s, Parametre.s = "", Methode = #Requette_GET, Proxy$="", Entete = #False, Delai = 3)
 
 Protected *Buffer
 
 If Len(Adresse)
  
  curl  = curl_easy_init()
  
  If curl
      
   curl_easy_setopt(curl, #CURLOPT_URL, str2curl(Adresse))
   curl_easy_setopt(curl,#CURLOPT_POST, Methode)  ;'POST' request = 1 // 'GET' request = 0
   curl_easy_setopt(curl, #CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0")
   curl_easy_setopt(curl,#CURLOPT_POSTFIELDS, str2curl(Parametre))
   curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Delai)
   
   If Entete
    curl_easy_setopt(curl, #CURLOPT_HEADER, @"")      
   EndIf 
   
   If Len(Proxy$)
    curl_easy_setopt(curl, #CURLOPT_PROXY, str2curl(proxy$))
   EndIf
   
   curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToMemoryFunction())
   res = curl_easy_perform(curl)
   
   If res = #CURLE_OK
   
    *Buffer = AllocateMemory(ReceiveHTTPToMemoryBufferPtr)
    
    If *Buffer
     CopyMemory(*ReceiveHTTPToMemoryBuffer, *Buffer, ReceiveHTTPToMemoryBufferPtr)
     FreeMemory(*ReceiveHTTPToMemoryBuffer)
     *ReceiveHTTPToMemoryBuffer = #Null
     ReceiveHTTPToMemoryBufferPtr = 0
    EndIf
    
   EndIf
   
   curl_easy_cleanup(curl)
   
  EndIf
  
 EndIf 
 
 ProcedureReturn *Buffer
 
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
 
 Define *Buffer, Proxy$

 InitNetwork()
  
 Proxy$ = "http://xxxxxx.xxx.fr:3128"
 
 *Buffer = RequetteHTTP("http://www.purebasic.fr/english/search.php", "keywords=kcc&terms=all&author=&sc=1&sf=all&sk=t&sd=d&sr=topics&st=0&ch=300&t=0&submit=Search", #Requette_GET, Proxy$, #False,10)
 
 If *Buffer
  Debug PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8|#PB_ByteLength)
  FreeMemory(*Buffer)
 EndIf
 ;  
CompilerEndIf

Have a good day
Last edited by Kwai chang caine on Thu Nov 24, 2016 10:50 am, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
normeus
Enthusiast
Enthusiast
Posts: 470
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: CURL request POST and GET

Post by normeus »

It works for me.
I am not using proxy$

Code: Select all

 Proxy$ = "";  "http://xxxxxx.xxx.fr:3128"
kcc if you don't mind adding one extra file to your program I think you should give "phantomjs" a try.
it will do all that you want to do. The only problem is that you will need to learn JavaScript but for all the stuff you want to do on the web, if you want to make your job easier; then you need to learn some JavaScript.

phanthomjs has compiled binaries ready to be used. I downloaded the windows version to my "G:" drive if you download it make sure you change the directories. I haven't tried the proxy setup but it should be pretty easy to understand and I added the switch there so you can see how the command should look.

Code: Select all

;phantom JS "post.js" file in the examples folder
; use it to post data to a website

; // Example using HTTP POST operation
; 
; "use strict";
; var page = require('webpage').create(),
;     server = 'http://posttestserver.com/post.php?dump',
;     Data = 'universe=expanding&answer=42';
; 
; page.open(server, 'post', Data, function (status) {
;     If (status !== 'success') {
;         console.log('Unable to post!');
;     } Else {
;         console.log(page.content);
;     }
;     phantom.exit();
; });


 pjs = RunProgram("G:/phantomjs-2.1.1-windows/bin/phantomjs.exe","--proxy=xxxxxx.xxx.fr:3128 --ignore-ssl-errors=true G:\phantomjs-2.1.1-windows\examples\post.js -v ","",#PB_Program_Hide|#PB_Program_Open| #PB_Program_Read| #PB_Program_Write)
output$ = ""
line$ = ""
If pjs
  While ProgramRunning(pjs)
    If AvailableProgramOutput(pjs)
      line$ = ReadProgramString(pjs) + Chr(13)
      Debug line$
      output$ + line$
    EndIf
  Wend
  output$ + " Exitcode: "+Str(ProgramExitCode(pjs))
  CloseProgram(pjs)
EndIf
MessageRequester("output",output$)
End


google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: CURL request POST and GET

Post by infratec »

Code: Select all

; Based on INFRATEC code ReceiveHTTPToMemory() http://www.purebasic.fr/english/viewtopic.php?p=490891#p490891

IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl

Global *ReceiveHTTPToMemoryBuffer, ReceiveHTTPToMemoryBufferPtr.i

#Requette_GET = 0
#Requette_POST = 1

ProcedureC ReceiveHTTPWriteToMemoryFunction(*ptr, Size.i, NMemB.i, *Stream)
 
  Protected SizeProper.i  = Size & 255
  Protected NMemBProper.i = NMemB
 
  If *ReceiveHTTPToMemoryBuffer = 0
    *ReceiveHTTPToMemoryBuffer = AllocateMemory(SizeProper * NMemBProper)
  Else
    *ReceiveHTTPToMemoryBuffer = ReAllocateMemory(*ReceiveHTTPToMemoryBuffer, MemorySize(*ReceiveHTTPToMemoryBuffer) + SizeProper * NMemBProper)
  EndIf
 
  CopyMemory(*ptr, *ReceiveHTTPToMemoryBuffer + ReceiveHTTPToMemoryBufferPtr, SizeProper * NMemBProper)
  ReceiveHTTPToMemoryBufferPtr + SizeProper * NMemBProper
 
  ProcedureReturn SizeProper * NMemBProper
 
EndProcedure

Procedure.i RequetteHTTP(Adresse.s, Parametre.s = "", Methode = #Requette_GET, Proxy$="", Entete = #False, Delai = 3)
 
  Protected *Buffer
 
  If Len(Adresse)
   
    curl  = curl_easy_init()
   
    If curl
     
      If Methode = #Requette_GET
        Adresse + "?" + Parametre
        curl_easy_setopt(curl, #CURLOPT_HTTPGET, 1)
      EndIf
     
      curl_easy_setopt(curl, #CURLOPT_URL, str2curl(Adresse))   
      curl_easy_setopt(curl, #CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0")
      curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Delai)
     
      If Entete
        curl_easy_setopt(curl, #CURLOPT_HEADER, @"")     
      EndIf
     
      If Len(Proxy$)
        curl_easy_setopt(curl, #CURLOPT_PROXY, str2curl(proxy$))
      EndIf
     
      If Methode = #Requette_POST
        curl_easy_setopt(curl, #CURLOPT_POST, 1)  ;'POST' request = 1 // 'GET' request = 0
        curl_easy_setopt(curl, #CURLOPT_POSTFIELDS, str2curl(Parametre))
      EndIf
     
      curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToMemoryFunction())
      res = curl_easy_perform(curl)
     
      If res = #CURLE_OK
       
        *Buffer = AllocateMemory(ReceiveHTTPToMemoryBufferPtr)
       
        If *Buffer
          CopyMemory(*ReceiveHTTPToMemoryBuffer, *Buffer, ReceiveHTTPToMemoryBufferPtr)
          FreeMemory(*ReceiveHTTPToMemoryBuffer)
          *ReceiveHTTPToMemoryBuffer = #Null
          ReceiveHTTPToMemoryBufferPtr = 0
        EndIf
       
      EndIf
     
      curl_easy_cleanup(curl)
     
    EndIf
   
  EndIf
 
  ProcedureReturn *Buffer
 
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
 
  Define *Buffer, Proxy$
 
  InitNetwork()
 
  ;Proxy$ = "http://xxxxxx.xxx.fr:3128"
 
  ;*Buffer = RequetteHTTP("http://www.purebasic.fr/english/search.php", "keywords=kcc&terms=all&author=&sc=1&sf=all&sk=t&sd=d&sr=topics&st=0&ch=300&t=0&submit=Search", #Requette_GET, Proxy$, #False,10)
  *Buffer = RequetteHTTP("http://www.purebasic.fr/english/ucp.php?mode=login","username=kcc&password=PASSWORD_OF_KCC&redirect=index.php&login=Login&redirect=.%2Fucp.php%3Fmode%3Dlogin", #Requette_POST, Proxy$, #False, 10)
  
  If *Buffer
    
    EnableExplicit
    
    
    Define.i Event, Exit
    
    
    OpenWindow(0, 0, 0, 800, 600, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
    WebGadget(0, 10, 10, 780, 580, "")
    SetGadgetItemText(0, #PB_Web_HtmlCode, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8|#PB_ByteLength))
    
    FreeMemory(*Buffer)
    
    Repeat
      
      Event = WaitWindowEvent()
      
      Select Event
        Case #PB_Event_CloseWindow
          Exit = #True
          
      EndSelect
      
    Until Exit
    
  EndIf
  ;
CompilerEndIf
Bernd
Last edited by infratec on Thu Nov 24, 2016 8:25 am, edited 1 time in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: CURL request POST and GET

Post by Kwai chang caine »

Hello Normeus :D
That work for you !!!! :shock:
Decidedly, i not understand, perhaps again a proxy problem. ..

PhantomJS yes it's also a good idea.
I have try it they are a long time, and i have not continue with it, because it not works on XP

I have again some machine under XP...

I have also thinking to selenium, with python.
Thanks to Infratec it's possible to use python with pb, it's also another solution. ..

So i keep your code preciously, we never know

Thanks a lot for your help and advice
Have a good night 8)

@INFRATEC
Waoouuuh !!!! You have found the problem ?
Again a time, i'm on android. .. :?
Tommorow at the first time, i test it .
You are really an angel.
Im really a wound for you :oops:

Have a very good night too, mainly without kcc in your nightmare :mrgreen:
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: CURL request POST and GET

Post by infratec »

Hi,

I changed my last code.
Now the result is shown in a webgadget and I added a POST example.
For that you have to change your username and password.

Bernd
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: CURL request POST and GET

Post by Kwai chang caine »

Hello INFRATEC

You are a mother for me 8)
Never you imagine how this code can change my life.
After the 10 years of COM, 2 years of TELNET, this time, thanks to you, perhaps i can talking directly at the hears of the server :shock:
Without passing by a navigator...i dream awake.

Sure i talk the server language, also good that english...then surely they are some misunderstood between him and me :mrgreen:
But have found his hear thanks to you, it's already a big step in the history of human KCC :lol:

I can test your splendid code in few minutes when i'm finally behind a pc, and i'm so much impatient. ..

I give to you some news...
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: CURL request POST and GET

Post by Kwai chang caine »

Image

It's super that works better !!! :D 8)

When i run your code i always a MsgBox "Script error, do you want to continue ?", i don't know why :shock:
Url : about:./mobiquo/tapatalkdetect.js
When i click yes, i'm connected to the forum 8)

Just a little strange thing, after several seconds, the webgadget return alone to the panel of login :shock:
http://www.purebasic.fr/english/ucp.php?mode=login
Surely a security somewhere ???

And when i test my old request

Code: Select all

*Buffer = RequetteHTTP("http://www.purebasic.fr/english/search.php", "keywords=kcc&terms=all&author=&sc=1&sf=all&sk=t&sd=d&sr=topics&st=0&ch=300&t=0&submit=Search", #Requette_GET, Proxy$, #False,10)
All are done 8)

In fact, again thanks to you, i have understand something other
When i connect for the first time of the day, it's not the same PROXY of all the day
And after, i can take the second, and always use it, the first not work, and never work in all the day
It's for this reason, all the code i test not working for me :|
It's strange like behaviour no ?

And like say DJES, i can't send the Login/Pass directly to the PROXY
I'm forced to enter it in a inputrequester, who appears automaticaly when i connect to internet
And only after i can cross the proxy, only if i send again Login/Pass at each request in your code
Surely a double protection....

So you are really too strong, i love you
Long life to you, and the germany 8)


Image

So ...this is the result of France and french people
For GERMANY.....twelve points :mrgreen:


Have the best day of the world 8)
ImageThe happiness is a road...
Not a destination
Post Reply