如何从 pHp 服务器脚本返回值到 NSMutableData (Objective C, iOS)
好的,我无法找到不仅仅使用 JSON 或 ASIHTTPRequest 的答案。但我认为我的问题更具体一些。我只是将编程作为一种爱好,所以我不是专家。话虽如此,我对此的抽象理解可能完全没有根据,所以这个问题可能无法回答。
基本上,我想做的是将 HTTP 请求发送到 Web 服务器上的 pHp 脚本,然后让 Web 服务器返回一个值,比如说一个简单的整数值或布尔值(想想返回是/否的登录脚本) )。目前我并不担心安全性,因为我正在努力轻松地理解这一点。
根据我收集的信息,我会使用一些东西来达到“This is from the Apple document”的效果
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
,我通常理解这一点。假设它通过 URL 将信息发送到 pHp 文件,然后:
//pHp file gets information
//pHp file uses that information to check against a mySQL database
//pHp now has the variable $result with a value of 1 in it
另外,假设我可以很好地完成所有这些工作。我的问题是,如何将 pHp 中的值返回到 iOS 'NSMutableData' 变量。我会“回声”出来吗?例如:
echo "$result";
然后这就是放入 NSMutableData 的信息(变量 $result)?我知道如果我严格通过 pHp/HTML 等进行操作,我可能会使用会话变量之类的东西来允许更多页面访问该变量。
或者是否有其他一些我不知道的 pHp 函数将数据返回到 NSMutabalData?基本上,这是我遇到问题的两者之间的链接,即从 pHp 脚本返回数据 - 我真的不明白 NSMutableData 正在“获取”什么。
我已尽力尽可能详细,很抱歉问了这么长的问题。请假设这就是我想做的全部,返回一个值。安全性并不重要(我试图在最简单的层面上做到这一点,以理解它然后在此基础上进行构建)。我试图避免任何模糊的答案,例如“编写网络服务”或“使用 _”来做到这一点,并且我确信有很多更好的方法可以做到这一点,但我我并不急于编写一个程序 - 我只是想学习这个以便更好地理解 NSURLConnection、pHp、NSMUtableData 以及所有齿轮如何组合在一起。如果我所问的问题在技术上是不可能的并且没有意义,我也很欣赏这个答案。我尝试过搜索,但没有找到解决这一难题的令人满意的答案。
提前致谢。
编辑:
我已经尝试过
...
NSMutableData *receivedData = [[NSMutableData alloc] init];
receivedData = [[NSMutableData data] retain];
NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
label.text = receivedDataString;
...
Which 调用服务器上的 pHp 文件,基本上是:
<?pHp
$result = "TRUE";
var_dump($result);
?>
Which 不起作用。我认为我的问题出在 pHp 方面,是否有一些特殊命令可以将值返回到 NSMutableData 对象? (只是一些额外的信息,也许可以更多地澄清我的问题)。
编辑二:
我似乎无法直接回应,可能是因为我以前只匿名使用过该网站并且我有一个全新的帐户:)谢谢你的回答查尔斯,我尝试过使用 echo 和 var_dump,两者都没有似乎将数据发送回(我的 NSMutableData 对象在转换为 NSString 时似乎是空的 - 除非我的目标 C 是错误的,但我已将其解析为几乎最低限度,所以我不相信是这样) 。
Ok, I'm having trouble finding an answer to this that isn't just use JSON or ASIHTTPRequest. But my question is a little more specific, I think. I only program as a hobby, so I'm no expert. With that said, my abstract understanding of this could be completely off base, so the question might not be answerable.
Basically, what I would like to do is send an HTTP request to a pHp script on a web server an have the web server return a value, lets say a simple integer value or bool value (think a login script that returns a yes/no). I'm not worried about security at this point as I'm trying to ease my way into understanding this.
From what I gather, I'd use something to the effect of
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
This is from the Apple documentation, and I generally understand this. So lets say it sends information through the URL to a pHp file which then:
//pHp file gets information
//pHp file uses that information to check against a mySQL database
//pHp now has the variable $result with a value of 1 in it
Also, assume I can do all of that fine. My question becomes, how do I return a value from pHp back to the iOS 'NSMutableData' variable. Do I 'echo' it out? For example:
echo "$result";
And then thats the information put into the NSMutableData (the variable $result)? I know if I were doing it strictly through pHp/HTML etc., I might use something like a session variable to allow further pages to access the variable.
Or is there some other pHp function that returns the data to NSMutabalData that I'm unaware of? Basically this is the link between the two that I'm having trouble with, the returning of data from the pHp script - I don't really understand what it is that NSMutableData is 'getting'.
I've tried to be as detailed as possible, so sorry for the long question. Please just assume this is all I'm trying to do, return a value. Security isn't important (I'm trying to do this at the most simple level to understand it then build on it). I'm trying to avoid any vague answers such as 'write a web service' or 'use _' to do it, and I'm sure there are lots of better ways to do it, but I'm not rushing out a program - I'm just trying to learn this for a better understanding of NSURLConnection, pHp, NSMUtableData and how al the cogs fit together. If what I'm asking is technically impossible and doesn't make sense, I appreciate that answer too. I've tried searching and haven't found a satisfactory answer for this single piece of the puzzle.
Thanks in advance.
EDIT:
I've tried
...
NSMutableData *receivedData = [[NSMutableData alloc] init];
receivedData = [[NSMutableData data] retain];
NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
label.text = receivedDataString;
...
Which calls a pHp file on the server that is basically:
<?pHp
$result = "TRUE";
var_dump($result);
?>
Which doesn't work. I think my problem is on the pHp side, is there some special command that returns a value to the NSMutableData object? (Just some extra info to maybe clarify my question more).
EDIT II:
I can't seem to respond directly, probably because I've only ever used the site anonymously before and I have a brand new account :) Thanks for your answer Charles, I've tried to use echo and var_dump, neither seem to send the data back (my NSMutableData object when converted to an NSString seems to be empty - unless my objective C is wrong, but I've parsed it down to pretty much the bare minimum so I don't believe that's the case).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我相信我已经成功了,所以如果其他人也在寻找同样的答案,我将发布答案。
服务器上的 PHP 文件如下:
我的 h 文件包含
我的 m 文件包含相关部分(其他所有内容都只是 Apple 在 SDK 中创建 m 文件时放入 m 文件的标准内容
NSLogs 输出如下:
This1: ' <>'
This2:
This3: '<>'
This4: Works
This5: '<24726573 756c74>'
要回答我原来的问题,
echo 'Works';
是将单词 Works 返回到 NSMutableData 对象。建立连接后,我需要调用 didRecieveData: 方法将回显的信息实际放入数据文件中。Ok, I believe I got it to work and so I'm going to post an answer in the event anyone else is looking for the same.
The PHP file on the server is as follows:
My h file contains
My m file contains in relevant part (everything else is just the standard stuff Apple puts into an m file when you create it in SDK
The NSLogs output as follows:
This1: '<>'
This2:
This3: '<>'
This4: Works
This5: '<24726573 756c74>'
To answer my original question,
echo 'Works';
is the PHP code to return the word Works to the NSMutableData object. After setting up the connection, I needed to call thedidRecieveData:
method to actually put the echoed information into the data file.如果它像其他看起来只是发出 HTTP 请求的东西一样工作,您将获得一个包含所请求 URL 的输出的字符串,或者您将获得一个可以从中获取该输出的对象(包括标头等) 。我对 Objective C 或 iOS 一无所知,但我打赌你只会得到脚本的输出。
换句话说,
大概!
为什么不尝试一下代码并看看会得到什么?如果它有效,那么恭喜您:您已经创建了第一个 Web 服务。
If it works like every other thing that looks like it's just making an HTTP request, you will either get a string containing the output from the requested URL, or you will get an object from which you can get that output (including headers, etc). I know nothing about Objective C or iOS, but I'm going to wager that you'll just be getting the output of the script back.
In other words,
Probably!
Why don't you try out the code and see what you get? If it works, then congratulations: you've created your first web service.