If I have a php file on my site, how could I pass an NSString
to a POST
variable (with objective-c) so I can echo it onto the page? I am new to php, so sorry if this is obvious.
如果我的站点上有一个php文件,如何将NSString传递给POST变量(使用objective-c),以便将其回显到页面上?我是php新手,如果这是显而易见的,我很抱歉。
Using ASIFormDataRequest
:
使用ASIFormDataRequest:
NSURL *myURl = [[NSURL alloc] initWithString:@"http://www.mysite/something.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURl];
[request setDelegate:self];
[request setUsername:@"username"];
[request setPassword:@"password"];
[request setPostValue:@"This is a string" forKey:@"phpVar"];
[request startAsynchronous];
Delegate method calls:
委派方法调用:
-(void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Failed %@ with code %d and with userInfo %@",[error domain],[error code],[error userInfo]);
}
-(void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"Finished : %@",[request responseString]);
}
There are a couple of problems here:
这里有几个问题:
- Even if I enter the incorrect credentials,
requestFinished
only gets called. - 即使我输入了不正确的凭证,requestFinished也只会被调用。
Edit -- Just fixed (1), created a password protected directory and stuck the php file in there.
编辑——只需修改(1),创建一个密码保护目录并将php文件插入其中。
- "This is a string" isn't getting passed to phpVar in the php file and won't display on the page
- “这是一个字符串”不会在php文件中传递给phpVar,也不会显示在页面上
Here is what I wrote in php:
这是我在php中写的:
<?php
$blah = $_POST['phpVar'];
echo $blah;
?>
Thanks for your help.
谢谢你的帮助。
1 个解决方案
#1
0
There's potentially a few different places that this process could break down.
这个过程可能会有一些不同的地方。
- Since you're attempting to set a literal in your instance of ASIFormDataRequest, we can probably safely assume it actually contains that value.
- 由于您试图在您的ASIFormDataRequest实例中设置一个文字,因此我们可以放心地假设它实际上包含该值。
- The next item to validate is that the POST is actually hitting the PHP page (but not necessarily passing the correct values). There are a few ways to accomplish this, but the easiest may be to view the access log of the web server. How to accomplish this will vary depending on the server (i.e. Apache, IIS, etc.).
- 要验证的下一项是POST实际上正在访问PHP页面(但不一定传递正确的值)。有几种方法可以实现这一点,但是最简单的方法可能是查看web服务器的访问日志。如何实现这一点取决于服务器(比如Apache、IIS等)。
- If the script is indeed hitting the page, the last step should be validating what (if any) request parameters are bing passed. Instead of dumping out the singular value form $_POST, I would recommend you take a broader look at all the request values by echoing them out with a function call like so:
- 如果脚本确实击中了页面,最后一步应该是验证bing传递了什么(如果有的话)请求参数。我建议您更广泛地查看所有请求值,而不是将单值形式$_POST抛出,方法是使用类似这样的函数调用来回显它们:
.
。
<?php
var_dump($_REQUEST);
?>
This should verbosely print out everything in $_GET, $_POST, and $_COOKIE, which can be handy just in case the value isn't getting sent through a POST, as you'd expect. If the value is present, it should be trivial to access it or modify the way in which the request was sent so that it is delivered as expected.
这应该会在$_GET、$_POST和$_COOKIE中打印出所有的内容,这样就很方便了,因为如果值不像您所期望的那样通过POST发送的话。如果该值是存在的,那么访问它或修改发送请求的方式以使其按预期交付应该是很简单的。
#1
0
There's potentially a few different places that this process could break down.
这个过程可能会有一些不同的地方。
- Since you're attempting to set a literal in your instance of ASIFormDataRequest, we can probably safely assume it actually contains that value.
- 由于您试图在您的ASIFormDataRequest实例中设置一个文字,因此我们可以放心地假设它实际上包含该值。
- The next item to validate is that the POST is actually hitting the PHP page (but not necessarily passing the correct values). There are a few ways to accomplish this, but the easiest may be to view the access log of the web server. How to accomplish this will vary depending on the server (i.e. Apache, IIS, etc.).
- 要验证的下一项是POST实际上正在访问PHP页面(但不一定传递正确的值)。有几种方法可以实现这一点,但是最简单的方法可能是查看web服务器的访问日志。如何实现这一点取决于服务器(比如Apache、IIS等)。
- If the script is indeed hitting the page, the last step should be validating what (if any) request parameters are bing passed. Instead of dumping out the singular value form $_POST, I would recommend you take a broader look at all the request values by echoing them out with a function call like so:
- 如果脚本确实击中了页面,最后一步应该是验证bing传递了什么(如果有的话)请求参数。我建议您更广泛地查看所有请求值,而不是将单值形式$_POST抛出,方法是使用类似这样的函数调用来回显它们:
.
。
<?php
var_dump($_REQUEST);
?>
This should verbosely print out everything in $_GET, $_POST, and $_COOKIE, which can be handy just in case the value isn't getting sent through a POST, as you'd expect. If the value is present, it should be trivial to access it or modify the way in which the request was sent so that it is delivered as expected.
这应该会在$_GET、$_POST和$_COOKIE中打印出所有的内容,这样就很方便了,因为如果值不像您所期望的那样通过POST发送的话。如果该值是存在的,那么访问它或修改发送请求的方式以使其按预期交付应该是很简单的。