Jump to content
  • Advertisement
Sign in to follow this  
md_lasalle

Passing byte array from C to PHP

This topic is 2350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I've been struggling a bit on this. Basically, I'm building an array of bytes in the C program(can contain '\0' chars in it), which i then send to the PHP script using libcurl.

Here's the sending code from C program (I omitted the usual initing stuff of libcurl) :



curl_formadd( &formpost,
&lastptr,
CURLFORM_COPYNAME, "dataField",
CURLFORM_CONTENTSLENGTH, dataSize,
CURLFORM_COPYCONTENTS, data,
CURLFORM_END);



My data actually gets there, but for some reason, I'm pretty sure I am not handling the data properly on the PHP side of things, and basically, PHP will cut some data because it thinks I am handling strings.

In the example above, here's the content of my char buffer called "data" :


data[0] = '0' // length of the data packed in 4 digits for easier parsing
data[1] = '0'
data[2] = '1'
data[3] = '8'

data[4] = '6' // length of the string "myUUID" written next (doing so to remove '\0' in the byte array)
data[5] = 'm' // the actual string
data[6] = 'y'
data[7] = 'U'
data[8] = 'U'
data[9] = 'I'
data[10] = 'D'

data[11] = 'x' // first byte of a 4byte integer (replaced some weird code by 'x')
data[12] = 'x' // second byte of a 4byte integer (replaced some weird code by 'x')
data[13] = 'x' // third byte of a 4byte integer (replaced some weird code by 'x')
data[14] = 'x' // fourth byte of a 4byte integer (replaced some weird code by 'x')


data[15] = '6' // length of the string "myNICK" written next (doing so to remove '\0' in the byte array)
data[16] = 'm' // the actual string
data[17] = 'y'
data[18] = 'N'
data[19] = 'I'
data[20] = 'C'
data[21] = 'K'



With that information, how can I put the content of $_POST['dataField'] safely in a var on the PHP side, so that i can then iterate over it with a simple FOR loop ? I can normally get the "0018" size to an int without issues, and then I get myUUID, but the rest, I'm not able to parse.

Thanks.

Share this post


Link to post
Share on other sites
Advertisement

My data actually gets there, but for some reason, I'm pretty sure I am not handling the data properly on the PHP side of things, and basically, PHP will cut some data because it thinks I am handling strings.



PHP is not the best language for binary data processing in general. If you're doing a web service, you typically want to use a textual data format, such as JSON or XML or form-encoding, to transfer your data. (Personally, I prefer JSON)

PHP does have the unpack() function. If you get all the binary data you expect in the original request body (say, by reading from php://input), then you can use unpack() to translate between binary data and "safe" PHP data

Share this post


Link to post
Share on other sites
Interesting, I'm very new to PHP development and web stuff in general, I usually go with pure sockets with C programs, but I'm trying to come up with a cheap solution that can be hosted on cheap web hosts. I'm thinking into moving into JSon as well, but I need a few pointers.

First, will I need libcurl to send JSon data from a c++ program ? Do I need to send this as a POST request ?

Second, is reading from php://input the only way to retreive the JSon data from the receiving side ? What's the simplest ?

Thanks hplus.

Share this post


Link to post
Share on other sites

Interesting, I'm very new to PHP development and web stuff in general, I usually go with pure sockets with C programs, but I'm trying to come up with a cheap solution that can be hosted on cheap web hosts. I'm thinking into moving into JSon as well, but I need a few pointers.


As long as you realize that your cheap web host will give you cheap service.


First, will I need libcurl to send JSon data from a c++ program ? Do I need to send this as a POST request ?
[/quote]

No, you can send a proper HTTP request using any kind of library, or raw sockets. There's a library here that is kind-of minimal: http://www.mindcontrol.org/~hplus/http-get.html
If you already have curl working, though, that's a very high-quality library.
If you want to send data, you can send it as a single GET parameter, or as POST data.

1. GET /myscript.php?json=<url-encoded-JSON-goes-here>

2. POST /myscript.php
Content-Type: application/javascript
Content-Length: <strlen(json)>
<json-data>

I would highly recommend going through POST.


Second, is reading from php://input the only way to retreive the JSon data from the receiving side ? What's the simplest ?
[/quote]

php://input is the canonical way to retrieve the post body.


$json = json_decode(file_get_contents("php://input"));
if (!$json) {
// something went wrong
}
print_r($json); // or whatever :-)

Share this post


Link to post
Share on other sites
[color=#1C2837][size=2]As long as you realize that your cheap web host will give you cheap service.[/quote]


Part of my project consists of distributing connecting client over multiple servers, so in that case, another server would be used in case of a failure.


Thanks for the reply, exactly what I needed to know.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!