Canceling Notification with OneSignal REST API - php

So I'm trying to cancel notifications using the REST API of oneSignal. I have successfully sent them and scheduled them using the oneSignal REST API but canceling is proving to be difficult. Mostly it is because in the documentation the cURL is:
curl --include \
--request DELETE \
--header "Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj" \
https://onesignal.com/api/v1/notifications/{notificationId}?app_id={appId}
This is my PHP code so far:
$ch = curl_init();
$httpHeader = array(
'Authorization: Basic MY_REST_API_KEY'
);
$url = "https://onesignal.com/api/v1/notifications/" . NOTIFICATION_ID . "?app_id=" . APP_ID;
$options = array (
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $httpHeader,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => "DELETE",
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
I'm not too sure how to convert the shell to php and right now it is not deleting the notifications. the $response isn't returning anything meaning it is not working. Any help would be awesome.
Thanks

I had to add one more option to my options array so basically this:
$options = array (
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $httpHeader,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_SSL_VERIFYPEER => FALSE
);
That allowed the request to go through!

Related

PHP curl PUT does not continue respectively send payload/data

I need to PUT some json data to an API endpoint, which works as expected via command line curl, but not via php curl and I don't have any idea, why it doesn't.
my command is
curl -v --insecure --request PUT --url <https://blabla/blablabla> --user 'username:password' --header 'Content-Type: application/json' --data '<valid json data>'
but it doesn't work this way within php:
// get cURL resource
$curl = curl_init();
// set cURL options
$curloptions = array(
CURLOPT_PUT => true, // set method to PUT
CURLOPT_RETURNTRANSFER => true, // return the transfer as a string
CURLOPT_VERBOSE => true, // output verbose information
CURLOPT_SSL_VERIFYHOST => false, // ignore self signed certificates
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERNAME => $config['uag']['user'], // set username
CURLOPT_PASSWORD => $config['uag']['pass'], // set password
CURLOPT_HTTPHEADER => array( // set headers
"Content-Type: application/json",
),
CURLOPT_POSTFIELDS => $jsondata // set data to post / put
);
curl_setopt_array($curl, $curloptions);
foreach($serverurilist as $uri) {
// set url
curl_setopt($curl, CURLOPT_URL, $uri);
// send the request and save response to $response
$response = curl_exec($curl);
// stop if fails
if(!$response) {
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
var_dump($response);
}
// close curl resource to free up system resources
curl_close($curl);
What doesn't work? The payload / data doesn't get submitted. If I tcpdump the command line und php version without encryption, I can see, that the command line submits the data right after the Expect: 100-continue request and the HTTP/1.1 100 Continue response from the server. The php version doesn't do anything after the HTTP/1.1 100 Continue response and quits after reaching the timeout.
From documentation:
CURLOPT_PUT - true to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.
and you are not using any file to provide content.
You should use CURLOPT_CUSTOMREQUEST => 'PUT'.
This is your same cUrl request exported from Postman:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://blabla/blablabla',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS =>'<valid json data>',
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ='
],
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
You should use this CURLOPT_CUSTOMREQUEST=>'PUT'

PHP Get Access an Token from Gotowebinar API

I try using PHP to get an access token in GoToWebinar API, from this documentation https://developer.goto.com/guides/HowTos/03_HOW_accessToken/
here my php script
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.getgo.com/oauth/v2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'Authorization: Basic MWM4ODE2MmItMGZkZS00YTM4LWI0NGMtMWMzOGFhMDY5YmI4OktQSDg2d1hnVUM5a0FqbithMVBsOGc9PQ==',
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
),
CURLOPT_POSTFIELDS =>"redirect_uri=http://indotourismevents.com/webinar&grant_type=authorization_code&code=eyJraWQiOiJvYXV0aHYyLmxtaS5jb20uMDIxOSIsImFsZyI6IlJTNTEyIn0.eyJscyI6IjczYjkyNzhmLTBmNjMtNDE3NC1iYTljLWE3ZTIyYzIwMDhmNyIsInVyaSI6Imh0dHA6Ly9pbmRvdG91cmlzbWV2ZW50cy5jb20vd2ViaW5hci8iLCJzYyI6ImNvbGxhYjogaWRlbnRpdHk6c2NpbS5tZSBpZGVudGl0eToiLCJhdWQiOiIxYzg4MTYyYi0wZmRlLTRhMzgtYjQ0Yy0xYzM4YWEwNjliYjgiLCJzdWIiOiIzODc4MTIxOTUzODYzNTgxMTkwIiwianRpIjoiOTgyYTYyMWQtYTFhYi00NTE3LWFlYTgtNDFlYmY3OWE0NzFlIiwiZXhwIjoxNTk0NzQxOTUxLCJpYXQiOjE1OTQ3NDEzNTEsInR5cCI6ImMifQ.Cy6TR18iKmCtotZnnPvMUS9QMh4EFBP6FMcqBOWN9LtricreNg1qnA45UJB0cQALB1d5h9zgc-nDkU4GtTNr8ZnDSI26On3YqFJBICptmfrHhI7ZZIwH-p8YilKirHua1shEPVpradyAj_epjfmejd35QbCXD8aTz3uq-ocRGZFvz0WZfAA7wWryMBnkvTm5BM4fTx99Q4AGfT27QFomaR4hGsLy6uUM4N3rcqP7VNM21XeuIZ-5U1zd2Ew0-gnWoNo8lOF1hJeihZ_xyMlY9AgN7HAwW55JmUkFSsNVHdLznYebzXuDJZXGYnXxQwIyVym3tmx88JRUcI0bMzJ5-w",
));
$response = curl_exec($curl);
$err = curl_error($curl);
from the documentation, curl must give some header and form body
curl -X POST "https://api.getgo.com/oauth/v2/token" \
-H "Authorization: Basic YTIwfAKeNGYtODY4YS00MzM5LTkzNGYtNGRhMmQ3ODhkMGFhOjNuYU8xMElBMmFnY3ZHKzlJOVRHRVE9PQ==" \
-H "Accept:application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "redirect_uri=https://example.com&grant_type=authorization_code&code=iS0vynEEvRFA9i6kZ8gvNDnnOGE..."
and i give all required header&form body, but still return an error like this
{"error":"invalid_request","error_description":"Required parameter(s) missing or wrong."}
how to PHP get access token from GoToWebinar API?
I experienced this problem when I logged in with the same user account to G2Webinar as my developer user (the oauth client's user was the same as the user webinar user)
Try to register another account.

PHP CURL does not return the full token

I am using PHP curl to do basic Auth that return a jwt token.
When I Request the token in shell using curl i get a token of length 381 .However when I do the request in my php code i only get a token of length 326 which is not complete which lead to other problems in my other php requests .
how can i make my php code give me the full length of my token that is 381 in my response and not just 326?
Update my cURL shell code is :
curl --location --request POST 'https://xxxx/xxxx/auth/login' \
--header 'Authorization: Basic XXXXX'
my curl php code :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxxxx/xxxxx/auth/login",
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( "Authorization: Basic xxxxxxx" ),
) );
$response = curl_exec($curl);
curl_close($curl); echo $response;
Thanks

How to write curl -X PUT -T script in php

I am working on a Hootsuite script that needs to upload a file using cURL PUT request. On Hootsuite site, I found below example code but I am being able to convert below code in PHP script :
curl -X PUT -H 'Content-Type: video/mp4' \
-H "Content-Length: 8036821" \
-T "/Users/kenh/Downloads/amazing_race.mp4" \
"https://hootsuite-video.s3.amazonaws.com/production/3563111_6923ef29-d2bd-4b2a-a6d2-11295411c988.mp4?AWSAccessKeyId=AKIAIHSDDN2I7V2FDJGA&Expires=1465846288&Signature=3xLFijSn02YIx6AOOjmri5Djkko%3D"
I tried below code but it is still not working :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://hootsuite-video.s3.amazonaws.com/production/3563111_6923ef29-d2bd-4b2a-a6d2-11295411c988.mp4?AWSAccessKeyId=AKIAIHSDDN2I7V2FDJGA&Expires=1465846288&Signature=3xLFijSn02YIx6AOOjmri5Djkko%3D",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => array('file' => '#' . realpath('/Users/kenh/Downloads/amazing_race.mp4')),
CURLOPT_HTTPHEADER => array(
'Content-Type: video/mp4',
'Content-Length: 8036821'
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
So can anybody help me out by telling how to convert above code in PHP ?
I'll be thankful.
Try appending http_build_query on the CURLOPT_POSTFIELDS option, like below :
CURLOPT_POSTFIELDS => http_build_query( array( 'file' => '#' . realpath( '/Users/kenh/Downloads/amazing_race.mp4' ) ) ),
From what I know, the http_build_query is required on the post fields when using PUT method.

How to write Curl api example into PHP curl

I am using https://apptweak.io api,
they didn't mention PHP example in their code example...
please tell me, how to understand and write Curl API example into PHP curl
Here is simple "appteak.com" curl example
curl -G https://api.apptweak.com/ios/applications/284993459/informations.json \
-d country=us \
-d language=en \
-d device=iphone \
-H "X-Apptweak-Key: my_api_key"
Here is other example
$ curl -H "X-Apptweak-Key: my_api_key" \
https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl
How to write these curl in PHP?
i did this in php, after google, but its not work for me.
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl',
// CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'X-Apptweak-Key' => "my_api_key",
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
var_dump($resp ); //return false
I find correct answer
$token = "my_api_key";
$url = "http://URL.com";
$options = array('http' => array(
'header' => array("X-Apptweak-Key: $token"),
));
$context = stream_context_create($options);
$result = file_get_contents($url, 0, $context);

Categories