通过HTTPHeaderField

时间:2016-01-28 22:01:07

标签: php swift http-headers

我试图将一些带有空格和特殊字符的值从Swift程序传递到php脚本,方法是将它们归为HTTPHeaderFields,其代码类型为:

let theRequest=NSMutableURLRequest(URL:NSURL(string:urlString)!, cachePolicy:.ReloadIgnoringLocalAndRemoteCacheData,
                    timeoutInterval:100.0)
theRequest.addValue(self.name, forHTTPHeaderField: "name")
theRequest.addValue(self.abbot, forHTTPHeaderField: "abbot")
theRequest.addValue(self.address, forHTTPHeaderField: "address")
theRequest.addValue(self.email!, forHTTPHeaderField: "email")
theRequest.addValue(self.phone!, forHTTPHeaderField: "phone")
theRequest.addValue(self.website!, forHTTPHeaderField: "website")

由php脚本收到:

$name=$_SERVER['HTTP_NAME'];
$abbot=$_SERVER['HTTP_ABBOT'];
$address=$_SERVER['HTTP_ADDRESS'];
$email=$_SERVER['HTTP_EMAIL'];
$phone=$_SERVER['HTTP_PHONE'];
$website=$_SERVER['HTTP_WEBSITE'];

此机制适用于所有变量,但适用于地址1。当我执行调用时,我收到错误:

  

PHP注意:未定义索引:第9行/var/www/html/iPhone/iPuja/insertMonastery.php中的HTTP_ADDRESS

在php脚本中;在HTTP_ADDRESS是保留字的情况下,我尝试将地址名称更改为其他字符串,但无济于事。可能是什么问题呢?

谢谢,

1 个答案:

答案 0 :(得分:0)

问题是由于我没有URLEncode字符串。一旦使用:

theRequest.addValue(self.address.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!, forHTTPHeaderField: "address")

价值经历了;出于某种原因,在发送带有特殊字符的非urlencoded字符串时,不会发出错误,而是发送任何内容。 在PHP脚本上,我当然必须对事物进行urldecode并设置:

$mysqli->query("SET NAMES utf8");

为了应对重音字符串和其他特殊字符。