【问题标题】:PHP forward slash in json post requestjson post请求中的PHP正斜杠
【发布时间】:2018-01-10 19:19:14
【问题描述】:

我有 fcm 服务器。在 Laravel lumen 中构建。

服务器地址:https://firenoti.000webhostapp.com/fcmserver/public/api/noti

发帖请求:title=Hi&token=f12S4al_8dg:APA91bFuNy4SrMOH8AiK0M_AGd8SUIraegqYEPgffdBY6AujN6b84ALtM22W9mAocuBq5bf83mHqOPPP1T-OSW0rAVMUVIOQkDxEd9l-MoQ3kaNrv4d4Q5pOsl5qKahwx_55FZ-Gf9gV&body=Hello

我使用 HttpRequester 进行发布请求。 这是我的服务器代码:

public function getData(Request $request)
 {

$firebase_url = 'https://fcm.googleapis.com/fcm/send';
$api_key  =  ”api_key”;

if ($request->has('token')  &&  $request->has('token')  &&  $request->has('token')) {

  $token = $request->get('token');
  $title = $request->get('title');
  $body = $request->get('body');

    if($token == "ALL"){
        $token = "/topics/all";
      }

  $fields = ['to' => $token,'data'=> array('title' => $title, 'body' => $body)];
  $headers = ['Content-type : application/json; charset=utf-8"',"Authorization:key = ".$api_key];

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $firebase_url);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  $result = curl_exec($ch);
  curl_close($ch);

  $result_array = json_decode($result,true);

  if ($result_array['success'] == 1) {

    $status = "OK";
    $res_message = "Notification send successfuly";

  }else{

    $status = "ERROR";
    $res_message = "Notification sending fail";

  }
  return response()->json(["status" => $status,"message"=>$res_message]);

}else{

  return response()->json(["status" => 'ERROR',"message"=>'Invalid request format']);

    }
}

`

当我使用用户令牌发送请求时,一切正常。

但是当我发送带有 token=ALL 的 post 请求时,我得到了错误

我认为问题就在这里

if($token == "ALL"){
    $token = "/topics/all";
}

如何解决?

【问题讨论】:

  • 你用过 JWT 吗?如果您使用 JWT,那么您必须使用其他变量而不是使用令牌。用于验证用户的令牌变量。
  • 没有。我不使用任何 jwt
  • 你添加了发布路线吗??
  • yes.post 与用户令牌正常工作。看图片
  • $fields = ['to' => $token,'data'=> array('title' => $title, 'body' => $body)];问题就在这里。你不能发送给所有你必须使用所有令牌为每个用户发送通知。

标签: php json firebase firebase-cloud-messaging lumen


【解决方案1】:

我认为问题就在这里

这不是问题。这就是解决方案。

代码显示“如果令牌是 ALL,则改用 /topics/all”。

当您手动发出请求时,您发送的是 ALL 而不是 /topics/all

【讨论】:

    猜你喜欢
    • 2012-10-29
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2019-03-04
    • 2012-07-25
    相关资源
    最近更新 更多