8 Replies - 3544 Views - Last Post: 17 February 2011 - 06:50 PM Rate Topic: -----

#1 Guest_Ted Copple*


Reputation:

php variable to C++ application

Posted 17 February 2011 - 01:36 PM

I need a little help here. I have a function currently that allows me to send data to a php script from my C++ application. How can I do the opposite?

C++ Code below to send data to the php script:
void SendData()
{
	    char* Navigate = new char[500];
	    memset(( void* )Navigate, 0, 500);
            char* Version = "Version 1.0";
	    sprintf(Navigate, "MYSITE.COM/GetData.php?v=%s",  Version);
	    SendFunction(Navigate);	
}
DWORD SendFunction(char* link)
{
	char* szURL = link;
	
    HINTERNET hSession;  
	HINTERNET hRequest;
	hSession = InternetOpen(                         // Make internet connection.
                  "Microsoft Internet Explorer",   // agent
                  INTERNET_OPEN_TYPE_PRECONFIG,    // access
                  NULL, NULL, 0);                  // defaults
      if(hSession)
	  {
     hRequest = InternetOpenUrl(                 // Make connection to desired page.
             hSession,                        // session handle
             link,                             // URL to access
             NULL, 0, 0, 0); // defaults
	  }
	  if(hRequest)
	  {
	  bool bResult = HttpSendRequest(hRequest, NULL, 0, NULL, 0); 
	  }
	  InternetCloseHandle(hSession);
	  return 0;
}


php script that receives the data from the C++ application:
<? 
$f=fopen('FILE','a');

fwrite($f,$_GET['v'] \n); 

fclose($f); 
?> 

This just simply sends the char* Version = "Version 1.0" text to the php script witch _GETs it and writes it to a file. Simple and impractical, however I just wanted to get something rolling. It works fine, now I need to do the opposite.

I need to do exactly what this function does, but flip it around!. I want store a value in the php script, and get that value with my C++ application, and then log it out. Any help here please!?

php script for what I want to do:
<?php
$dbhost = 'MyHost';
$dbuser = 'MyUserName';
$dbpass = 'MyPassword';

mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
$dbname = 'MyDB';
mysql_select_db($dbname);
$query = mysql_query("SELECT * FROM AppVersion");
while($row = mysql_fetch_array($query))
{
    $version = $row['Version Info'];
}
?>

I need to query the php script from my C++ application and retrieve the value of $version. How can I setup the php script to export that value $version for the C++ app and use the WinInet funcs to retrieve it?


Note: I am just trying to get a feel for how all of this works right now, so none of this really has a practical use at the moment, but is helping me progress in my overall goal, so it will be very helpful to get this worked out.

I realize there are better ways to accomplish something like this witch I will look into later, for now I want to get this worked out if someone can direct me.

Is This A Good Question/Topic? 0

Replies To: php variable to C++ application

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: php variable to C++ application

Posted 17 February 2011 - 01:45 PM

Simply echo it out in PHP and read the returned data from the HTTP request.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: php variable to C++ application

Posted 17 February 2011 - 01:46 PM

If your php script calls your C++ app then it could pass the value as a parameter. (I don't do Windows, so can't help with that.)
Was This Post Helpful? 0
  • +
  • -

#4 Ted Copple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-February 11

Re: php variable to C++ application

Posted 17 February 2011 - 01:50 PM

View PostJackOfAllTrades, on 17 February 2011 - 01:45 PM, said:

Simply echo it out in PHP and read the returned data from the HTTP request.


This is what I was thinking about, but I am not sure how to grab the data with the request. Could help with a small example?

Can I just read it into a variable with the request?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: php variable to C++ application

Posted 17 February 2011 - 01:55 PM

You're using WinInet, so after sending your request, use InternetQueryDataAvailable to get the number of bytes coming back, allocate a buffer large enough to accommodate the data, and use InternetReadFile to get the data into the buffer.
Was This Post Helpful? 2
  • +
  • -

#6 Ted Copple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-February 11

Re: php variable to C++ application

Posted 17 February 2011 - 02:01 PM

View PostJackOfAllTrades, on 17 February 2011 - 01:55 PM, said:

You're using WinInet, so after sending your request, use InternetQueryDataAvailable to get the number of bytes coming back, allocate a buffer large enough to accommodate the data, and use InternetReadFile to get the data into the buffer.


Ok, thanks for the information. I will give it a shot and see what I come up with.
Was This Post Helpful? 0
  • +
  • -

#7 Ted Copple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-February 11

Re: php variable to C++ application

Posted 17 February 2011 - 03:01 PM

View PostTed Copple, on 17 February 2011 - 02:01 PM, said:

View PostJackOfAllTrades, on 17 February 2011 - 01:55 PM, said:

You're using WinInet, so after sending your request, use InternetQueryDataAvailable to get the number of bytes coming back, allocate a buffer large enough to accommodate the data, and use InternetReadFile to get the data into the buffer.


Ok, thanks for the information. I will give it a shot and see what I come up with.



Hmm, still stuck at a wall here. Is "buffer" supposed to hold the value that I need?

This is what I have tried:

 HINTERNET hSession;  
	HINTERNET http;
	HINTERNET hRequest;
	bool bResult;
	hSession = InternetOpen(                         // Make internet connection.
                  "Microsoft Internet Explorer",   // agent
                  INTERNET_OPEN_TYPE_PRECONFIG,    // access
                  NULL, NULL, 0);                  // defaults
      if(hSession)
	  {
         http = InternetConnect(hSession, "MYSITE", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
	  }
	  if(http)
	  {
		  hRequest = HttpOpenRequest(http, "GET", "/Version.php", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0);
	  }
	  if(hRequest)
	  {
	  bool bResult = HttpSendRequest(hRequest, NULL, 0, NULL, 0); 
	  }
	  if(bResult)
	  {
	  DWORD getquerydata;
	  InternetQueryDataAvailable(hRequest, &getquerydata, 0, 0);
      DWORD dwBYTEsRead;
     
	  LPCSTR buffer;
	  buffer = new char[getquerydata];

	  InternetReadFile(hRequest, (LPVOID)buffer, getquerydata, &dwBYTEsRead);
	  }
	  InternetCloseHandle(hSession);

Was This Post Helpful? 0
  • +
  • -

#8 Ted Copple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-February 11

Re: php variable to C++ application

Posted 17 February 2011 - 06:37 PM

Ok here is the problem, not entirely sure what I am doing wrong though.

I re-did the code in a console project and used cout at each stage of the code, here is the output:

Posted Image

This post has been edited by Ted Copple: 17 February 2011 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#9 Ted Copple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-February 11

Re: php variable to C++ application

Posted 17 February 2011 - 06:50 PM

Sorry for all of the extra posts, I fixed it and the buffer is displaying the correct value.

Thanks for the input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1