Anyone knows how to call a PHP script on a server and pass parameters to it?

 
This is really out of my field... Saw something about HttpSendRequest from Wininet. dll, but didn't make much sense to me...
 
First call HttpOpenRequest. ;)
 
10x, managed to find example ;)
 
TheEconomist:
10x, managed to find example ;)

Ive been trying to find the same, can anyone send me in the right direction? Could not find it in the documentation.



Thanks

 
//+------------------------------------------------------------------+
//|                                                         http.mq4 |
//|                                Copyright © 2008, Berkers Trading |
//|                                                http://quotar.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Berkers Trading"
#property link      "http://quotar.com"
 
//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
 
#import "wininet.dll"
int InternetOpenA (
   string lpszAgent,
   int dwAccessType,
   string lpszProxyName,
   string lpszProxyBypass,
   int dwFlags
);
 
int InternetConnectA(
   int hInternet,
   string lpszServerName,
   int nServerPort,
   string lpszUsername,
   string lpszPassword,
   int dwService,
   int dwFlags,
   int dwContext
);
 
int HttpOpenRequestA(
   int hConnect,
   string lpszVerb,
   string lpszObjectName,
   string lpszVersion,
   string lpszReferer,
   string lplpszAcceptTypes,
   int dwFlags,
   int dwContext
);
 
bool HttpSendRequestA(
   int hRequest,
   string lpszHeaders,
   int dwHeadersLength,
   int lpOptional,
   int dwOptionalLength
);
 
bool InternetReadFile(
   int hFile,
   string lpBuffer,
   int dwNumberOfBytesToRead,
   int &lpdwNumberOfBytesRead[]
);
 
bool InternetCloseHandle(
   int hInternet
);
 
#import
 
//#include <stdfunctions.mqh>
/*
InternetOpen 
 
dwAccessType
INTERNET_OPEN_TYPE_DIRECT
INTERNET_OPEN_TYPE_PRECONFIG
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY
INTERNET_OPEN_TYPE_PROXY
 
dwFlags
INTERNET_FLAG_ASYNC
INTERNET_FLAG_FROM_CACHE
INTERNET_FLAG_OFFLINE
 
*/
 
//InternetConnect - nServerPort
#define INTERNET_DEFAULT_FTP_PORT      21
#define INTERNET_DEFAULT_GOPHER_PORT   70
#define INTERNET_DEFAULT_HTTP_PORT     80
#define INTERNET_DEFAULT_HTTPS_PORT    443
#define INTERNET_DEFAULT_SOCKS_PORT    1080
#define INTERNET_INVALID_PORT_NUMBER   0
 
//InternetConnect - dwService
#define INTERNET_SERVICE_FTP     1
#define INTERNET_SERVICE_GOPHER  2
#define INTERNET_SERVICE_HTTP    3
 
//HttpOpenRequest - dwFlags
/*#define INTERNET_FLAG_CACHE_IF_NET_FAIL
#define INTERNET_FLAG_HYPERLINK
#define INTERNET_FLAG_IGNORE_CERT_CN_INVALID
#define INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
#define INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP
#define INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS
#define INTERNET_FLAG_KEEP_CONNECTION
#define INTERNET_FLAG_NEED_FILE
#define INTERNET_FLAG_NO_AUTH
#define INTERNET_FLAG_NO_AUTO_REDIRECT*/
#define INTERNET_FLAG_NO_CACHE_WRITE            0x04000000
/*#define INTERNET_FLAG_NO_COOKIES
#define INTERNET_FLAG_NO_UI*/
#define INTERNET_FLAG_PRAGMA_NOCACHE            0x00000100
#define INTERNET_FLAG_RELOAD                    0x80000000
/*#define INTERNET_FLAG_RESYNCHRONIZE
#define INTERNET_FLAG_SECURE*/
 
void StringExplode(string sDelimiter, string sExplode, string &sReturn[]){
   
   int ilBegin = -1,ilEnd = 0;
   int ilElement=0;
   while (ilEnd != -1){
      ilEnd = StringFind(sExplode, sDelimiter, ilBegin+1);
      ArrayResize(sReturn,ilElement+1);
      sReturn[ilElement] = "";     
      if (ilEnd == -1){
         if (ilBegin+1 != StringLen(sExplode)){
            sReturn[ilElement] = StringSubstr(sExplode, ilBegin+1, StringLen(sExplode));
         }
      } else { 
         if (ilBegin+1 != ilEnd){
            sReturn[ilElement] = StringSubstr(sExplode, ilBegin+1, ilEnd-ilBegin-1);
         }
      }      
      ilBegin = StringFind(sExplode, sDelimiter,ilEnd);  
      ilElement++;    
   }
}
 
string wget(string sURL){
   
   string sURLexplode[];
   StringExplode("/",sURL, sURLexplode);
   int hInternetOpen = InternetOpenA ("mql4",1,"","",0);
   int hInternetConnect = InternetConnectA(hInternetOpen, sURLexplode[2], INTERNET_DEFAULT_HTTP_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
   int hHttpOpenRequest = HttpOpenRequestA(hInternetConnect, "GET", sURLexplode[3], "", "", 0, 
              INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD , 0);
   bool bHttpSendRequestA = HttpSendRequestA(hHttpOpenRequest, "", 0, 0, 0);
   string mContent = "";
   int lpdwNumberOfBytesRead[1];
   string sResult;
   while (InternetReadFile(hHttpOpenRequest, mContent, 255, lpdwNumberOfBytesRead) != FALSE) {
        if (lpdwNumberOfBytesRead[0] == 0) break;
        sResult = StringConcatenate(sResult , StringSubstr(mContent, 0, lpdwNumberOfBytesRead[0]));
   } 
   bool bRes = InternetCloseHandle(hInternetOpen); 
   return(sResult);
}
hth
 
Russel,

First I want to thank you... your little bit of code helped me find a bug I have been chasing for days. The bug basically was that InternetConnectA variable lpszServerName is looking for just the "server" name sans the "http://".

When I was working with the code, I learned a few things that should be helpful to others:

1. Here are two routines to get the server and path from a URL:
string fsGetServer(string psURL){
int liServerStart = StringFind(psURL,"//",0)+2;
return( StringSubstr(psURL,liServerStart,StringFind(psURL,"/",liServerStart)-liServerStart));
}
string fsGetPath(string psURL){
return(StringSubstr(psURL,StringFind(psURL,"/",StringFind(psURL,"//",0)+2)));
}

2. The output buffer for InternetReadFile, lpBuffer, can be troublesome. The api is expecting bytes set aside for the output value. At least for large values of dwNumberOfBytesToRead, if the buffer is not declared, then it can crash terminal.exe. I went through much trouble of creating a method to allow a large buffer with the thought that a large buffer would mean fewer reads and faster response... not ... however, after much ado, I found a simple 128 byte string worked best as a buffer and was faster. Either less bytes or more bytes than 128 was slower. Here is how I declared the string buffer...
string lsReadBuffer = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//128

3. There are three handles created in the above routine, according to microsoft, all three should be closed.

4. To answer the original question, here is how I pass parameters to HttpSendRequestA:
First, tell the server a "POST" is coming...
if(psPostFields != ""){
lsConnectionType = "POST";
}else{
lsConnectionType = "GET";
}
liInternetFileHandle = fiHttpOpenRequest(liInternetConnectHandle, lsConnectionType, lsPath);
Then send the Post fields...
string psPostFields = "name=John+Doe&userid=hithere&other=P%26Q"; //name = "John Doe", userid = "hithere", other = "P&Q"
lbRetVal = fbHttpSendRequest(liInternetFileHandle, psPostFields);//Send Data - Request Web Page
...
bool fbHttpSendRequest( int piInternetFileHandle, string psPostParameters){
return(HttpSendRequestA(piInternetFileHandle,"",0,psPostParameters,StringLen(psPostParameters)));
}

5. Note the Post fields must be URL Encoded.

Ron
Reason: