问题描述
我一直试图在最近5个小时内将json对象发布到PHP脚本。我已阅读所有文档,似乎代码应该工作,但它没有。请求已发出并收到,但我无法访问已发布的json数据,或者甚至不知道它是否已正确发送。我一直得到一个空的php数组,而不是json数据。
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];
这里是php脚本(test.php)
here is the php script (test.php)
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
print_r($decoded);
我使用
我做错了什么?????
What am I doing wrong?????
干杯,
Trav。
Cheers,Trav.
推荐答案
发现问题。我在apache vhost配置中有一个SSL重定向。使用tcp数据包嗅探器来查找它。一旦我删除重定向,我就能够收到json数据:
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);
感谢所有帮助过的人。
这篇关于ASIHTTPRequest将json发布到php服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!