SendNetworkString in PHP loop [Resolved]

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

SendNetworkString in PHP loop [Resolved]

Post by Kwai chang caine »

Hello at all

I have a PHP code with loop for watching evenement in the server
For the moment, for break the loop i use a simple empty file "Stop", when the PHP loop see this file, it break the loop

But i try to sending data to this PHP code during this loop and that not works with POST or GET :|
I begin by automaticaly PostEvent a POST method, and apparently the PHP not read the next manual $_POST or $GET variable in the loop :|
I have try to unset the $_POST variable, but that not works too

Someone have a way for do this ?

PB code

Code: Select all

InitNetwork()

OpenWindow(0, 200, 200, 300, 100, "")
ButtonGadget(1, 10, 10, 100, 60, "POST")
ButtonGadget(2, 150, 10, 100, 60, "GET")

MySite$ = "www.mysite.fr"
MyPath$ = "/MyPath/index.php"

Con = OpenNetworkConnection(MySite$, 80)

If Con
 
 PostEvent(#PB_Event_Gadget, 0, 1, #PB_EventType_LeftClick)
 *Buffer = AllocateMemory(1024)
 
 If *Buffer
  
  Repeat
  
   Evenement = WaitWindowEvent(1)
   
   Select NetworkClientEvent(Con)
   
    Case #PB_NetworkEvent_Data
    
     Len = ReceiveNetworkData(Con, *Buffer, MemorySize(*Buffer))
     
     If Len
      Buffer$ = PeekS(*Buffer, Len, #PB_UTF8|#PB_ByteLength)
      Debug Buffer$
     EndIf
          
    Case #PB_NetworkEvent_None
    
     Delay(10)
     
   EndSelect
    
   If Evenement = #PB_Event_Gadget
    
    If EventType() = #PB_EventType_LeftClick
    
     If EventGadget() = 1 ; POST
      
      Post$ = "PostOrder=Hello POST"
      Request$  = "POST /" + MyPath$ + " HTTP/1.1" + #CRLF$
      Request$  + "Host: " + MySite$ + #CRLF$
      Request$  + "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66" + #CRLF$
      Request$  + "Content-Type: application/x-www-form-urlencoded" + #CRLF$
      Request$  + "Content-Length: " + Str(StringByteLength(Post$, #PB_UTF8)) + #CRLF$
      Request$  + #CRLF$
      Request$  + Post$
      
      Debug SendNetworkString(Con, Request$, #PB_UTF8)
      
     ElseIf EventGadget() = 2 ; GET
     
      Header$  = "GET " + MyPath$ + "?GetOrder=Hello GET HTTP/1.1" + #CRLF$
      Header$ + "Host: " + MySite$ + #CRLF$
      Header$ + "Content-Type: application/x-www-form-urlencoded" + #CRLF$   
      Header$ + #CRLF$
     ;  
      Debug SendNetworkString(Con, Header$, #PB_UTF8)
     
     EndIf 
     
    EndIf
  
   EndIf
  
  Until Evenement = #PB_Event_CloseWindow
  
  CloseNetworkConnection(Con)
  FreeMemory(*Buffer)
  
 EndIf
 
Else

 Debug "Impossible to connect server" 
 
EndIf
PHP code

Code: Select all

<?php
	
	header("Content-Type: text/html; charset=utf-8");
	$NumRequest = $NumRequest + 1;
	
	while (!file_exists("Stop"))
	{
		
		if (isset($_POST['PostOrder']))
		{
		
			$PostOrder = $_POST['PostOrder'];
						
			// unset($_POST['RefCommande']);
			
			$LoopPost = $LoopPost + 1;
			echo "Request POST number " . $NumRequest . " Loop number " . $LoopPost . " : \r\n". $PostOrder . "\r\n\r\n";
			
		}
		
		else if (isset($_GET['GetOrder']))
		{
		
			$LoopGet = $LoopGet + 1;
			$GetOrder = $_GET['GetOrder'];
			echo "Request GET number " . $NumRequest . " Loop number " . $LoopGet . " : \r\n". $GetOrder . "\r\n\r\n";
						
		}
				
		sleep(2);
		
	}
		
?>
Have a good day
Last edited by Kwai chang caine on Sat Oct 31, 2020 3:28 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SendNetworkString in PHP loop

Post by infratec »

I think you misuse php.

You can write standalone codes and you can write web pages.
If you write web pages, I think the content of the environment variables are only set once at start of the php script.
So they will never updated in your 'while' loop.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: SendNetworkString in PHP loop

Post by Kwai chang caine »

Aaaah !!!! ok ... A way in less :|
Furthermore, it's impossible to have variables between sessions
It's really difficult to quickly communicate between PB and PHP and in the other direction :oops:

My project works now in the first try 8) but as i expected it's a little bit slow :cry:
So, for fix a part of the slowness, i search better ways for communicate in both ways :idea:

And by dint of searching, i have found the WebSocket...it's the reason of my other question where you have also answered 8)
viewtopic.php?p=561254#p561254
But WebSocket is really not simple to use between PHP and PB, and i have search all the afternoon yesterday and not found examples really working for me, even between PHP and HTML :|

Thanks for your precious help 8)
ImageThe happiness is a road...
Not a destination
Post Reply