本文介绍了ASIHTTPRequest 将 json 发布到 php 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去 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);

我正在使用来自 https://github.com/pokeb/的最新 ASIHttpRequest 代码asi-http-request.git

我做错了什么??????

What am I doing wrong?????

干杯,旅行.

推荐答案

发现问题.我在 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 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:44
查看更多