ASIHTTPRequest post json to php server

混江龙づ霸主 提交于 2020-01-19 07:38:54

问题


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.


回答1:


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.




回答2:


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"];


来源:https://stackoverflow.com/questions/5338001/asihttprequest-post-json-to-php-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!