简体   繁体   中英

PHP forward slash in json post request

I have fcm server.Build in Laravel lumen.

Server url: https://firenoti.000webhostapp.com/fcmserver/public/api/noti

Post request: title=Hi&token=f12S4al_8dg:APA91bFuNy4SrMOH8AiK0M_AGd8SUIraegqYEPgffdBY6AujN6b84ALtM22W9mAocuBq5bf83mHqOPPP1T-OSW0rAVMUVIOQkDxEd9l-MoQ3kaNrv4d4Q5pOsl5qKahwx_55FZ-Gf9gV&body=Hello

I use HttpRequester for post request. This is my server code:

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']);

    }
}

`

When I send a request with user token everything work fine. 在此处输入图片说明

But when I send post request with token=ALL I get error 在此处输入图片说明

I think problem is here

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

How to fix that?

I think problem is here

That isn't the problem. That is the solution.

The code says "If the token is ALL then use /topics/all instead".

When you are making the request manually you are sending ALL instead of /topics/all .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM