1

Okay, so I got a game, that needs to send some stuff from C++ to a PHP page using a post request.

My questions are:
How can I send a post request? What kind of data can I send over to PHP? (just strings and ints or also entire collections?)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DijkeMark
  • 1,284
  • 5
  • 19
  • 41
  • I guess you want to take a look at libcurl: http://curl.haxx.se/libcurl/ - even though it can handle so much more, its the easiest library to do those things in c++ – Najzero Dec 20 '12 at 10:41
  • cURL will help you to send post requests from C (and also C++). You should read more about HTTP and GET/POST, start with Wikipedia. Basically you can send whatever you like via POST, but you will need to serialize your data. – Zane Dec 20 '12 at 10:42

4 Answers4

2

How can I send a post request?

This is normally done using an HTTP library. I've not used one in C++ myself, but cURL is popular across platforms and has [C++ bindings][1] (although the documentation seems to be undergoing migration at the moment).

There is more information on the subject in this question.

What kind of data can I send over to PHP?

Pretty much any data you like - but it has to be a data format rather then a data structure. If you want to send a collection you would need to serialise it to some format. JSON is a popular one. Binary data can be encoded using base64.

If you do use JSON you could either then add a second layer of encoding to send it application/x-www-form-urlencoded and access the raw JSON via $_POST or you can make the JSON the whole body of the POST request and get the body of the post in PHP.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

The fact that you say you're sending via POST request tells me that you're using a HTTP server to serve your PHP script. In which case, you want a library that allows you to send HTTP requests. A very popular choice is libcURL. What can you send? You can send anything that a HTTP request would allow you to send. So anything you like.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

You can use JSON or SOAP for sending the data. When passing small amount of data JSON is preferred. More info on JSON/SOAP

Community
  • 1
  • 1
stamhaney
  • 1,246
  • 9
  • 18
0

To build HTTP request consider using libcurl and eventually its c++ wrapper curlpp

You can send whatever you want to php but don't forget that all the data comming from HTTP will be string for php. PHP can cast to some type like int with intval() or (int) but binary data will be more complex to handle.

Consider using some standars like json and REST to build a robust communication.

grunk
  • 14,718
  • 15
  • 67
  • 108