8

I have been trying to post a json object to a PHP script for the last 5 hours. I have read all the docs and it seems the code should work BUT it does not. The request is made and received ok, but I can't access the posted json data or dont even know if its been sent correctly. I keep getting an empty php array, instead of the json data.

NSString *jsonString = [self.tableDataSource JSONRepresentation];

NSURL *url = [NSURL URLWithString:@"http://myserver.com/test.php"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];


[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; 
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[jsonString  dataUsingEncoding:NSUTF8StringEncoding]]; 
[request startSynchronous];

here is the php script (test.php)

$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
print_r($decoded);

I am using the lastest ASIHttpRequest code from https://github.com/pokeb/asi-http-request.git

What am I doing wrong?????

Cheers, Trav.

middus
  • 9,103
  • 1
  • 31
  • 33
johnstontrav
  • 339
  • 1
  • 6
  • 14
  • 2
    This calls for basic debugging. I have no experience in iPhone development, but there *must* be monitoring tools that can show you the success status, and response body of a request. On the PHP end, you can activate `error_reporting(E_ALL);` to output any possible problems. – Pekka Mar 17 '11 at 10:53
  • 1
    Won't you need to access the $_POST[] variables? – Lee Armstrong Mar 17 '11 at 10:55
  • Hi Pekka, nothing is returned when i turn on error_reporting(E_ALL). Do you have more ideas? – johnstontrav Mar 17 '11 at 18:22
  • Hi Les, I have tried print_r($_POST) and print_r($_GET); I still can't get any of the json data. anymore ideas? – johnstontrav Mar 17 '11 at 18:23

2 Answers2

4

Found the issue. I had a SSL redirect in the apache vhost config. Used a tcp packet sniffer to find it. once i remove the redirect i was able to receive the json data with:

$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
print_r($decoded);

Thanks to all those who helped.

johnstontrav
  • 339
  • 1
  • 6
  • 14
3

I'm fairly sure that ASIHTTPRequest defaults to a GET request. YOu're trying to POST data so I don't think this will work.

Try using ASIFormDataRequest which is built for POSTing data. Or, at the very least change the request method to POST.

 [request  setRequestMethod:@"POST"];
gnuchu
  • 1,496
  • 13
  • 21
  • 1
    Hi gnuchu, have tried the setRequestMethod "POST" and ASIFormDataRequest and print_r($_POST) and print_r($_GET); I still can't get any of the json data. anymore idea? – johnstontrav Mar 17 '11 at 18:18
  • I've not tried the setRequestMethod but I use the ASIFormDataRequest in a few apps and it works well. I can't diagnose if the issue is on the php side though as I don't know a huge amount about php. – gnuchu Mar 17 '11 at 22:17