FCM HTTP v1 Batch request won't work in PHP - php

I'm using currently the Firebase messaging with PHP. I was able to make it work with a single notification with PHP and cURL. I've read the documentation about making batch request and I've constructed the request string as follow:
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.xxxxxnY
POST /v1/projects/xxxxxxxx/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"cxxxxx3",
"data":{
"typeNoti":"paiement",
"idcompte":"admin",
"typecompte":"paiement"
},
"notification":{
"title":"Test1",
"body":"Notification de test 1"
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer yaxxxxxxY
POST /v1/projects/xxxxxx/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"eoxxxxxU1",
"data":{
"typeNoti":"paiement",
"idcompte":"compte2",
"typecompte":"paiement"
},
"notification":{
"title":"Test1",
"body":"Notification de test 1"
}
}
}
--subrequest_boundary--
This string is generated in a file called batch_request.txt as the documentation said. When trying to send the request with cURL I receive this error:
{
"error": {
"code": 400,
"message": "Failed to parse batch request, error: 0 items. Received batch body: (0) bytes redacted",
"status": "INVALID_ARGUMENT"
}
}
I've experienced a strange behavior also, which is when I construct that string manually (in PHP I use HEREDOC), the batch request does occurs with 200ok response. I've tried to find some hidden character but couldn't.
I know there is a library called firebase-php. The problem with it is that I cannot find a straight foreward example like this:
//authenticate with json file
//create new token if it doesn't exist or skip
//create notification array
//create data array
//send multi notifications to multiple devices
I actually use a for loop and mono notification (one device token, one message at a time). The problem with this is that not all notifications are sent.
I appreciate your help thanks!

I gathered the code from firebase-php docs. After installing the library with composer I get a lot of errors and a lot of missing files in Firebase directory inside vendor like Factory.php etc
My solution was to NOT USE firebase-php library due to the numerous bugs I've faced (or maybe misuses) neither to construct the REST request text file for batch request. I used instead the NodeJS SDK which worked like a charm. I'm running a javascript file from my php code using exec.

Related

HTTP Responses, Webhooks and Json

I have been asked to write a program that takes a list of numbers and sends a post to ms.4url.eu via JSON/HTTP Post in format:
{
"username":"a",
"password":"b",
"msisdn":"071231231234",
"webhook":"http://example.com"
}
it receives a JSON Response,
{
"status":"ok",
"id":"1234-1234-12344423-123123"
}
I have been told I can use ngrok for the webhook and I have to send a HTTP Response 200 within 1s.
I should receive a Webhook Response:
{
"id":"1234-1234-12344423-123123",
"msisdn":"071231231234",
"status":"unavaliable",
"error":"1b",
"errorDesc":"Abscent Subscriber"
}
How would I go about grabbing the data from the JSON response and Responding with a HTTP 200 in order to receive the second response with the data?
I can get the first response in curl but I am unable to get the webhook working to a php file using ngrok and HTTP response sent to request the main information in the second response.
Edited :
I have executed the curl command,
curl -H 'content-type: application/json' \
-d '{"username":"a", "password":"b", "msisdn":"07123123124","webhook":"http://example.com/"}' \
HTTPS://ms.4url.eu/lookup
of which I get the first response "status ok". I would like to know how to get the response(Json format) in php using http post to the URL and the using a webhook to respond with 1second with a http 200 response to receive the further information from the API URL.
I ended up using ngrok and viewing the Raw POST response and getting the JSON and viewing the Raw data I still had more code to do in order t make this question valid as there are too many points to answer.

Error trying to create webhook

I tried to create a webhook for a task in Asana but the only response I'm getting is this:
{
"errors": [
{
"message": "Could not complete activation handshake with target URL. Please ensure that the receiving server is accepting connections and supports SSL",
"help": "For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"
}
]
}
(Status: 400 Bad Request)
I am sending a POST Request via Postman to https://app.asana.com/api/1.0/webhooks with following content:
{
"data":
{
"resource": 123456789012345,
"target": "https://example.com/asana.php"
}
}
The asana.php looks something like this:
$headers = getallheaders();
$secret_token = $headers['X-Hook-Secret'];
header('X-Hook-Secret: ' . $secret_token);
What am I doing wrong? Am I missing something?
According to the Asana API Reference (https://asana.com/developers/api-reference/webhooks),
The target must respond with a 200 OK and a matching X-Hook-Secret header to confirm that this webhook subscription is indeed expected.
When you send the header, do you know what response code is being sent? Perhaps you might want to have a look at the $http_response_code argument in http://php.net/manual/en/function.header.php

PHP API Content-Type: application/json. Response empty data

Works fine for:
Sending from Android platform to PHP (web service)
Request headers sent with this Content-Type: application/x-www-form-urlencoded
Not working for:
Request headers sent with Content-Type: application/json. no data is received
API is working on same platform but not in cross platform:
Web to web WORKING
Android to web NOT WORKING
In PHP added both header on top:
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
If you compare these two content types application/x-www-form-urlencoded and application/json in a "The first works with PHP, the second doesn't", then you probably expect the data to appear magically inside $_POST in both cases.
This will not happen. PHP only fills $_POST if the first content type is given (alternatively, application/multipart-form-data can be used for everything, especially file uploads).
If you want to use application/json, then you have to implement a parser on the PHP side yourself that reads the HTTP request body and parses it to your liking.
after log search i found my answer..
we need this function to get $_POST response from cross platform (android to web)
file_get_contents('php://input')
OR we can also use this function to get $_POST response
$HTTP_RAW_POST_DATA
here is complete function to get the response.
$data = urldecode(file_get_contents('php://input'));
echo $data;

"Unsupported media type" when PUTing to Apigility with Postman

I'm a building a RESTful API using Zend Framework 2 and Apigility by Zend Framework.
For testing, I use the chrome extension Postman REST-Client.
I can do GET requests and POST requests without problems by sending form-data without problems.
But when I try to do a PUT, PATCH or DELETE request, I get the following error:
{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Unsupported Media Type",
"status":415,
"detail":"Invalid content-type specified"
}
Accept whitelist in Rest-Service-Config of Apigility:
application/vnd.timber-ms.v1+json, application/hal+json, application/json
Content-Type whitelist:
application/vnd.timber-ms.v1+json, application/json
The content-type of the response is application/problem+json
What can I do to fix this and do successfull PUT/PATCH requests?
Is this a problem with Postman or Apigility?
You're getting the 415 error of Unsupported Media Type when Apigility cannot deserialize the data coming from the client. This recently was called out in the documentation.
I suspect your problem is due to the content-type being sent from postman. Pay special attention to the Content-Type Whitelist listed for the service and make sure it contains the content-type you are sending.
For example, if your service has only has application/json in the Content-Type Whitelist and you send the PUT/PATCH with postman as x-www-form-urlencoded, you will get a 415 error of Unsupported Media Type. If you change postman to send the PUT/PATCH with a content-type of application/json and the request body contains valid JSON, Apigility should accept the request.
You can check the content-type postman is sending by clicking on the "Preview" button just to the right of the "Send" button.
I was having a similar issue in Postman with the unsupported media type responses. However, I experienced this response on both PUT and POST requests using my company's API.
I verified that Postman was the problem here and not the request/API in use by running the same request with another similar Chrome extension called Advanced REST Client. I'm not familiar with Apigility, but pretty sure that Postman is the culprit here.
Hoping there is a fix for this issue as Postman and its collections feature is much easier to use than that of Advanced REST Client.
on postman go to normal tab (or other auths tab) and add header:
Content-Type application/json (or any type you need)
make sure that on the raw tab include the json data
{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Unsupported Media Type",
"status":415,
"detail":"Invalid content-type specified"
}
I had the same problem,
My solution was to write my data to [Body] in the [raw] in json format. Like this:
{"message": "UPDATED First Post!"}
And in [Headers] :
[KEY] Content-Type
[Value] application/json

POSTing with Content-Type: multipart/form-data results in empty request body

Recently I started getting validation errors when POSTing to my REST interface with Content-Type: multipart/form-data. Content-Type: application/x-www-form-urlencoded is working.
Here is my testing function:
/**
* #Rest\Route("/testtype")
*/
public function postTypeTestAction()
{
$request = $this->getRequest()->request->all();
$phpContents = file_get_contents("php://input");
return FOSView::create()->setStatusCode(200)->setData(array('request'=>$request, 'phpinput' => $phpContents));
}
When I POST using Content-Type: application/x-www-form-urlencoded:
{
"request":{
"test":"message"
},
"phpinput":"test=message"
}
When I POST using Content-Type: multipart/form-data:
{
"request":[
],
"phpinput":"------WebKitFormBoundaryFyQqAxqqfuhWzHUq\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\nmessage\r\n------WebKitFormBoundaryFyQqAxqqfuhWzHUq--\r\n"
}
Since there is no request data, I'm getting This value should not be blank validation errors. This breaks my application. I've been staring at this for so long, I'm certain I'm missing something simple.
I'm using Symfony 2.3.7 and FOSRestBundle 1.0.0.
The issue resolved itself literally overnight. No server reboot, no change to code, and I'm using the same tool to test (DHC by Restlet - a Chrome extension). However, since this was not working on both dev and staging environments, and is now working on both environments, this leaves me with the only answer being the testing tool, Chrome, a local caching issue or some combination.
Lesson learned: use multiple testing tools.
Now when I POST using Content-Type: application/x-www-form-urlencoded:
{
"request":{
"test":"message"
},
"phpinput":"test=message"
}
Now when I POST using Content-Type: multipart/form-data:
{
"request":{
"test":"message"
},
"phpinput":""
}

Categories