开发者

PHP turning a POST request into a GET

开发者 https://www.devze.com 2023-03-07 23:03 出处:网络
I am trying to create an in-app purchase store on the iPad. As part of that system, I use a serve开发者_StackOverflow社区r to host the IAP content and serve the appropriate assets when given a valid r

I am trying to create an in-app purchase store on the iPad. As part of that system, I use a serve开发者_StackOverflow社区r to host the IAP content and serve the appropriate assets when given a valid receipt. The way I do this is to have the iPad send an HTTP POST to my server with the body of the post being the receipt data.

Here's my problem: for some reason, the server interprets the POST request as a GET and for this reason, the php://input is always empty. What's really weird is that it works on my local development environments (Windows, Apache, PHP 5.3) but not on my shared hosted production server (Linux, Apache, PHP 5.2).

Here is the relevant code from the iPad:

NSURL* url = [[NSURL alloc] initWithScheme:@"http" host: host path: path]; 
NSMutableURLRequest* theRequest = [[NSMutableURLRequest alloc] initWithURL: url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0f];
[url release];

[theRequest setHTTPMethod: @"POST"];
[theRequest setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
NSMutableData* postBody = [[NSMutableData alloc] init];
NSString* receiptString = [self newEncodedString:transaction.transactionReceipt];
[postBody appendData:[receiptString dataUsingEncoding:NSUTF8StringEncoding]];
[receiptString release];
NSString* postLength = [[NSString alloc] initWithFormat: @"%d", [postBody length]];
[theRequest setValue: postLength forHTTPHeaderField: @"Content-Length"];
[postLength release];
[theRequest setHTTPBody: postBody];
[postBody release];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
...

And here's the PHP server code:

$httpRequestBody = '';
$fh = @fopen('php://input', 'r');
if ($fh)
{
    while (!feof($fh)) {
        $s = fread($fh, 1024);
        if (is_string($s)) {
            $httpRequestBody .= $s;
        }
    }
    fclose($fh);
} else {
    $statusCode = 1;
}

The $httpRequestBody ends up being empty on the production server. When I call phpinfo(), the REQUEST_METHOD is a GET. Any help is greatly appreciated!


Shouldn't have to do the loop, just do

$httpRequestBody = file_get_contents('php://input')

But since, as you say, it's showing up as GET, there will never be any data available by this method, as GET requests do not have a body to read from. There's only the URL with its query parameters.

Check that your phone is actually sending a POST, then check if your server is somehow rewriting/redirecting the request into a GET instead. This should all be easily visible in the server's access log.


Use $HTTP_RAW_POST_DATA to read raw POST data.


Would something as simple as using $_REQUEST instead of $_GET/$_POST do the trick?

0

精彩评论

暂无评论...
验证码 换一张
取 消