【问题标题】:php file to get device token and store it for later userphp 文件以获取设备令牌并将其存储以供以后用户使用
【发布时间】:2014-07-18 14:38:37
【问题描述】:

我一直在研究各种设备令牌存储方法,我认为我的问题是我从 iOS 设备发送数据的位置。

例如

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myhost.com/filecreate.php?token=%@",
[[NSUserDefaults standardUserDefaults] objectForKey:@"apnsToken"]]]; //set here your URL

我应该编写什么样的 php 代码来处理这样的事情?

我目前正在使用 xampp,所以我的本地主机上有它。

我完全是 php 新手和体面的 iOS 开发人员,所以我真的不知道如何继续。

这就是我如何编辑 32 个整数的设备令牌

NSString * token = [NSString stringWithFormat:@"%@", deviceToken];
    //Format token as you need:
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
    token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];

用于推送到 php 脚本

【问题讨论】:

  • 你能举一个你正在推送的令牌的例子吗?
  • @DarylGill 你能再检查一下吗

标签: php ios iphone push-notification


【解决方案1】:

您正在使用查询字符串,因此您可以这样处理它:

$token = $_GET['token'];

我更喜欢发帖:

$token = $_POST['token'];

【讨论】:

    【解决方案2】:

    在您的 IOS 代码中,您正在使用 GET,只需将参数附加到 URL(普通 GET)。 如果您使用的是 NSURLConnection,我会更喜欢使用“GET”方法,通过将方法指定为 GET 而不是仅仅附加 url。

    你可以这样做,

       // Create the request.
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://myhost.com/filecreate.php"]];
    
    // Specify that it will be a POST request
    request.HTTPMethod = @"GET";
    
    // This is how we set header fields
    [request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    // Convert your data and set your request's HTTPBody property
    NSString *stringData = @"token=somedata&parameter_name=some_value";
    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = requestBodyData;
    
    // Create url connection and fire request
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    

    php中获取值的方法是

    $token=$_GET["token"];
    $second_param=$_GET["parameter_name"]; for GET method.
    

    $_GET 是一个 php Super Global,用于管理 GET 请求。同样,$_POST 用于管理 'POST' 请求。

    要存储数据,您可以使用 $_SESSION 进行短期存储。

    $_SESSION["token"]=$_POST["token"];
    

    对于长期存储使用 $FILE 数据库来创建或写入文件。

    参考this 了解更多关于超级全局变量的信息,例如 $_POST、$_SERVER 等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-04
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      相关资源
      最近更新 更多