Send android notification via firebase from heroku php server - php

I'm attempting to push a notification to a specific Android device using a heroku PHP server. I'm not having any luck in doing so, however.
I can push a notification through the firebase console just fine (i.e. the problem is not with my Android app).
Here is my code (which I got from How do I send a POST request with PHP?):
$url = 'https://fcm.googleapis.com/fcm/send';
$data = array('score' => '5x1', 'time' => '15:10');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n" .
"Authorization: key=MY_SERVER_KEY\r\n",
'method' => 'POST',
'data' => http_build_query($data),
'to' => 'MY_FCM'
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
I feel like I'm doing something terribly basic wrong (like my JSON is not formatted correctly or something).
The firebase apis can be found here: https://firebase.google.com/docs/cloud-messaging/send-message
I've been working on this for a couple days now, and any assistance will be much appreciated. Thanks guys!
Update
A quick note that Heroku doesn't support the HttpRequest() class from what I've experienced, however, cURL works great. Also, I didn't mention it, but I was actually wanting a notification message to be sent, not just a data message. So, my final code looked like the following:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n
\"notification\" : {\n
\"body\" : \"Goku\",\n
\"title\" : \"Over 9000\",\n
},\n
\"to\" : \"MY_FCM_TOKEN\"\n
\"priority\" :
\"high\"\n
}",
CURLOPT_HTTPHEADER => array(
"authorization: key=MY_SERVER_KEY",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

In your code
'http' => array(
'header' => "Content-type: application/json\r\n" .
"Authorization: key=MY_SERVER_KEY\r\n",
'method' => 'POST',
'data' => http_build_query($data),
'to' => 'MY_FCM'
)
you have to send data and to inside the key 'content'.
/* $mydata contains 'data' and 'to' */
'http' => array(
'header' => "Content-type: application/json\r\n" .
"Authorization: key=MY_SERVER_KEY\r\n",
'method' => 'POST',
'content' => http_build_query($mydata)
)
These are few recommended ways to send fcm notification using php
HttpRequest
$request = new HttpRequest();
$request->setUrl('https://fcm.googleapis.com/fcm/send');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'cache-control' => 'no-cache',
'content-type' => 'application/json',
'authorization' => 'key=YOUR_FCM_API_KEY'
));
$request->setBody('{
"data" : {
"name" : "Goku",
"power_level" : "Over 9000",
"fighting_skill" : "excellent"
},
"to" : "FCM_ID_OF_RECIEVER"
}');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
pecl_http
<?php
$client = new http\Client;
$request = new http\Client\Request;
$body = new http\Message\Body;
$body->append('{
"data" : {
"name" : "Goku",
"power_level" : "Over 9000",
"fighting_skill" : "excellent"
},
"to" : "FCM_ID_OF_RECIEVER"
}');
$request->setRequestUrl('https://fcm.googleapis.com/fcm/send');
$request->setRequestMethod('POST');
$request->setBody($body);
$request->setHeaders(array(
'cache-control' => 'no-cache',
'content-type' => 'application/json',
'authorization' => 'key=YOUR_FCM_API_KEY'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
cURL
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n \"data\" : {\n \"name\" : \"Goku\",\n \"power_level\" : \"Over 9000\",\n \"fighting_skill\" : \"excellent\"\n },\n \"to\" : \"FCM_ID_OF_RECIEVER\"\n}",
CURLOPT_HTTPHEADER => array(
"authorization: key=YOUR_FCM_API_KEY",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Related

My PlayFab reset email API POST request seems not to have a payload

I am trying to submit a php response to the following PlayFab API: https://learn.microsoft.com/en-us/rest/api/playfab/admin/account-management/reset-password?view=playfab-rest
This is my first ever php script and I have been working with this for quite some time and being able to run my script without errors but it seems like I have no payload.
The echo in the code:
echo ' The resp: ' .$resp;
...response from the above echo is:
The resp: string(0) ""
Unfortunately I really do not know what is wrong and how to fix it as there is not error messages. I have tried to echo $options array to control if there is a payload but not succeeded. I realise I need help and would really appreciate that.
$url = "https://titleId.playfabapi.com/Admin/ResetPassword/json/";
$data = array('Password' => $pw1, 'Token' => $params['token']);
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header' => 'X-SecretKey: xxxxxxxxxxxx\r\n',
'ignore_errors' => true
)
);
$context = stream_context_create($options);
$resp = file_get_contents($url, false, $context);
echo ' The resp: ' .$resp;
var_dump($resp);
Ok here is the solution. I was able to get this to work in Postman and when I test the code it works.
Here is the snippet that work:
echo '>>>>>>>>>> TEST <<<<<<<<<<';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://99999.playfabapi.com/Admin/ResetPassword?Password=pekapeka&Token=1B5A9C01EFD5DB69',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'X-SecretKey: xxxxxxx',
'Content-Type: application/json\\'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
print_r($response);
echo '<br>========== END ==========';

Error creating natural language classifier with api using PHP, returns data too small error but works in Postman

The cURL request works in Postman with the following:
curl -i -u "apikey:12345" \
-F training_data=#rtcu.csv \
-F training_metadata="{\"language\":\"en\",\"name\":\"RTCU\"}" \
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/v1/classifiers"
Postman's generated cURL code for PHP returns { "code" : 400, "error" : "Data too small", "description" : "The number of training entries received = 0, which is smaller than the required minimum of 5" }
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"training_data\"; filename=\"rtcu.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"training_metadata\"\r\n\r\n{\"language\":\"en\",\"name\":\"RTCU\"}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic 12345",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I've tried adding the "#" in front of the filename like suggested in other posts with no success. I haven't had this problem with other IBM Watson services and their cURL calls. What could be the issue?
Using CURLFile worked for me
$uploadFilePath = 'rtcu.csv';
$name = "RTCU";
$lang = "en";
$uploadFileMimeType = "text/csv";
$uploadFilePostKey = 'training_data';
$metaPostKey = "training_metadata";
$uploadFile = new CURLFile(
$uploadFilePath,
$uploadFileMimeType,
$uploadFilePostKey
);
$curlHandler = curl_init();
curl_setopt_array( $curlHandler, [
CURLOPT_URL => 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic 12345",
"cache-control: no-cache"
),
CURLOPT_POSTFIELDS => [
$uploadFilePostKey => $uploadFile,
$metaPostKey => "{\"language\":\"{$lang}\",\"name\":\"{$name}\"}"
],
] );
$response = curl_exec( $curlHandler );
$err = curl_error( curlHandler );
curl_close( curlHandler );
if ( $err ) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}

How to consume my webservice with PHP

I have a question ..
My app gives me the following information:
HTTP + JSON
The following are sample HTTP requests and responses. The placeholders shown need to be replaced with actual values.
POST /json/reply/Zona
HTTP/1.1
Host: equatepro.azurewebsites.net
Content-Type: application/json
Content-Length: length
{"zonaId":0,"nombre":"String","creadoPor":"String","creadoFecha":"/Date(-62135596800000-0000)/","modificadoPor":"String","modificadoFecha":"/Date(-62135596800000-0000)/","estado":"String","nota":"String","borrar":false}
AND then
The following routes are available for this service:
POST /api/zonas
PUT /api/zonas/{zonaId}
enter image description here
enter image description here
I'm trying to communicate with my webservice using PUT method
My code
<?php
$pantalla="zonas";
%id =8;
$url= "http: //miapp.com /api/zonas/8".$pantalla ;
$url = $url ."/" . $id;
// complete url http://miapp.com/api/zonas/8
//build json
$ConstructorJson = array(
'ZonaId' => $Datos['txt_codigo'],
'Nombre' => $Datos['txt_Nombre'],
'CreadoPor' => $Datos['txt_CreadoPor'],
'CreadoFecha' => $Datos['txt_CreadoFecha'],
'ModificadoPor' => $Datos['txt_ModificadoPor'],
'ModificadoFecha' => $Datos['txt_ModificadoFecha'],
'Estado' => $Datos['cbo_Estado'],
'Nota' => $Datos['txt_Notas']
);
$json = json_encode($ConstructorJson);
$opts = array(
"http" => array(
"method" => "PUT",
"header" => "Accept: application/xml\r\n",
"content" => $json
)
);
$context = stream_context_create($opts);
$response = file_put_contents($url,'8',false,$context);
?>
Give me the following error
Warning: file_put_contents(http: //miapp .com/api/zonas/8): failed to open >stream: HTTP wrapper does not support writeable connections in C:\xampp\htdocs\Codigo2.0\models\zonas.model.php on line 34
and nothing happens.
I would rather connect using PHP curl.
$ConstructorJson = array(
'ZonaId' => $Datos['txt_codigo'],
'Nombre' => $Datos['txt_Nombre'],
'CreadoPor' => $Datos['txt_CreadoPor'],
'CreadoFecha' => $Datos['txt_CreadoFecha'],
'ModificadoPor' => $Datos['txt_ModificadoPor'],
'ModificadoFecha' => $Datos['txt_ModificadoFecha'],
'Estado' => $Datos['cbo_Estado'],
'Nota' => $Datos['txt_Notas']
);
$json = json_encode($ConstructorJson);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http: //miapp.com/api/zonas/8/zonas",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $json
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"Accept: application/xml\r\n",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
HTTP wrapper does not support writeable connections - basically, PHP is telling you "Hey, you can't use this function to write to a file that lives on the internet. How do you expect me to write a file # http: //miapp .com/api/zonas/8? Not gonna happen".
I assume what you're trying to do is to send a PUT request # that location to update a zonas resource with ID 8.
Solution
Consider using a proper HTTP client that can send actual HTTP request methods and conform to the HTTP spec.
My personal favourite inside PHP is Guzzle -
http://docs.guzzlephp.org/en/stable/. Guzzle is a standalone package and can be downloaded from their site. You can use it in any PHP project - without or without a framework.
With Guzzle, you'd do something like the following:
$client = new GuzzleHttp\Client();
$json = json_encode($ConstructorJson);
$headers = [
"Accept" => "application/xml\r\n"
]
$request = new Request('PUT', $url, ['body' => $json, 'headers' => $headers]);
$client->send($request);
I finally solved the problem (thank you Kyle O'Brien)
Code
<?php
// web service url + tabla + id
$url = "mywebservice.com/zonas/8";
$Datos = $_POST;
//create a array with dates
$ConstructorJson = array(
'Nombre' => $Datos['txt_Nombre'],
'CreadoPor' => $Datos['txt_CreadoPor'],
'CreadoFecha' => $Datos['txt_CreadoFecha'],
'ModificadoPor' => $Datos['txt_ModificadoPor'],
'ModificadoFecha' => $Datos['txt_ModificadoFecha'],
'Estado' => $Datos['cbo_Estado'],
'Nota' => $Datos['txt_Notas']
);
//convert array to json
$json = json_encode($ConstructorJson);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>

Passing variable in curlopt_url

I want to pass a variable in CURLOPT_URL, here is my code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.postmates.com/v1/customers/cus_KtQih0aARUZXdk/deliveries/$delivery_id/cancel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
'pickup_address' => $_POST["pickup_address"],
'pickup_phone_number' => $_POST["pickup_phone_number"],
'pickup_name' => $_POST["pickup_name"],
'dropoff_address' => $_POST["dropoff_address"],
'dropoff_phone_number' => $_POST["dropoff_phone_number"],
'dropoff_name' => $_POST["dropoff_name"],
'manifest' => $_POST["manifest"]
),
CURLOPT_HTTPHEADER => array(
"authorization: Basic MjhiMDU0ODktNjdkYS00M2VhLTg0NmMtYWQ1MWQ2MGNmMDA1Og==",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=---011000010111000001101001",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$delivery_id = trim($_POST['show_name']);
curl_close($curl);
$response = json_decode($response);
$timestamp = json_decode($dateJSON, true);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response->fee;
}
I want to add the variable $delivery_id in the url, but I am not too familiar with cURL and the above written code is not working. Please show me the way to include this variable in my url.
Once I had the same error. The solution is : use ' -apostrophe - and . -dot- before and after the variable. Like this:
/deliveries/'.$delivery_id.'/cancel

Retrieve a JSON from postman on PHP

I am trying to retrieve a json from an api (and store as a variable), but I am unable to retrieve.
I am trying in all ways that postman and the web give to me.
I Am using laravel/homestead (ubuntu)
I am using laravel framework.
This way give me a HTTP Status 415 - Unsupported Media Type
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "443",
CURLOPT_URL => "https://iescities.com:443/IESCities/api/data/query/268/sql?origin=original",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "select * from wifi",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 1471daf9-329d-e9b4-b144-0383850f4769"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I also try another way but I haven't http\Client class and I haven't idea of how install and/or link it.
$client = new http\Client;
$request = new http\Client\Request;
$body = new http\Message\Body;
$body->append('select * from wifi');
$request->setRequestUrl('https://iescities.com:443/IESCities/api/data/query/268/sql');
$request->setRequestMethod('POST');
$request->setBody($body);
$request->setQuery(new http\QueryString(array(
'origin' => 'original'
)));
$request->setHeaders(array(
'postman-token' => 'e854f317-fdcc-1888-0043-758a3d5e32ba',
'cache-control' => 'no-cache'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
Lot of thanks and sorry for my ignorance.
From the IESCities manual:
Query must be sent in text/plain format.
So in the line you set the headers:
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 1471daf9-329d-e9b4-b144-0383850f4769"
),
You should put another key:
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 1471daf9-329d-e9b4-b144-0383850f4769",
"Content-Type: text/plain"
),
After doing this you should be able to query the API.
Have you tried decoding the JSON?
$response = curl_exec($curl);
$responseJSON = json_decode($response, true);
print_r($responseJSON);
If you JSON looks like:
{
"name": Susy,
"age": 22
}
You can pull the age by using echo $responseJSON["age"];

Categories