This is my curl command to send notification through terminal. It works properly but how to send notification on button click(using XHR request)
curl --header "Authorization: key=AIzaSyCjrU5SqotSg2ybDLK0dMusvY" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"f5xzshqcfDE2qiKGJu858nFhqGCuk0uuUC6vm\"]}"
You can write a php script to serve the purpose and just make an XHR request with client ,like javascript or any other of your choice.
Here is my php script I did it some months ago and it is working.
<?php
$message = "\": 3,\"title\": \"High score alert!\",\"\": true,\"custom_field\": { \"score\": 51,\"headlines\": \"And now for something completely different...\" }}";
$apiKey = "Your Key";
$registrationIDs = array("Registration id");
$url = 'https://android.googleapis.com/gcm/send';
// Set POST variables
$notify = array(
"alert"=> "great match!",
"title"=> "Portugal vs. Denmark",
"icon" => "myicon",
"badge"=>3,
"vibrate"=>true,
"notification"=>"message"
);
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "payload" => $notify,
"notification"=>json_encode($notify)));
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$ch = curl_init(); // Open connection
curl_setopt($ch, CURLOPT_URL, $url );
// Set the url, number of POST vars, POST data
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_POSTFIELDS, json_encode( $fields ));
var_dump( $url );
var_dump($headers);
var_dump(json_encode( $fields ));
$result = curl_exec($ch); // Execute post
if($result === false)
die('Curl failed ' . curl_error());
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$response = json_decode($result);
print_r($response);
?>
Related
I am getting an error for the following PHP code:
$curl = curl_init("https://api.openai.com/v1/engines/davinci/completions");
$data = array(
'prompt' => 'how many sundays in 2023',
'max_tokens' => 256,
'temperature' => 0.7,
'model' => 'text-davinci-003'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer sk-MY-API-KEY']);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result);
print $result->choices[0]->text;
I correctly provided the API Key, but getting this error:
Error message: You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY)
All Engines endpoints are deprecated.
This is the correct Completions endpoint:
https://api.openai.com/v1/completions
Working example
If you run php test.php in CMD, the OpenAI API will return the following completion:
string(23) "
This is indeed a test"
test.php
<?php
$ch = curl_init();
$url = 'https://api.openai.com/v1/completions';
$api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';
$post_fields = '{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}';
$header = [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($result);
var_dump($response->choices[0]->text);
?>
I AM trying to get a JSON file from a another site and for this I am trying to make a post request but I am not able to get a JSON data. I am sharing my PHP code and CURL data please tell me where is problem and why it is not working
PHP
<?php
$a="liveclasses";
$b="playvod";
$c ="mod=liveclasses&ack=playvod&stream=https%3A%2F%2Fslhdovces.toprankers.com%2Fbharthiconcept%2F1128732%2F1128732-pr-BIOLOGYbyLavkushpandeysirClass21-bharthiconcept-1573820502927%2F1128732-pr-BIOLOGYbyLavkushpandeysirClass21-bharthiconcept-1573820502927.m3u8";
$url = 'https://live.bharticoncept.com/route?route=common%2Fajax';
$data = array('mod' => $a,'ack' =>$b, 'stream' => $c);
$options = array(
'http' => array(
'Host' => "live.bharticoncept.com",
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'cookie'=>"tr_live_bharticoncept_com=so1ofdpkcleeshaq5dsevteia0",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
}
echo("$result");
?>
And curl
curl -X POST -H "Host:live.bharticoncept.com" -H "content-type:application/x-www-form-urlencoded; charset=UTF-8" -H "cookie:tr_live_bharticoncept_com=so1ofdpkcleeshaq5dsevteia0" -H "content-length:255" -d 'mod=liveclasses&ack=playvod&stream=https%3A%2F%2Fslhdovces.toprankers.com%2Fbharthiconcept%2F1128732%2F1128732-pr-BIOLOGYbyLavkushpandeysirClass21-bharthiconcept-1573820502927%2F1128732-pr-BIOLOGYbyLavkushpandeysirClass21-bharthiconcept-1573820502927.m3u8' "https://live.bharticoncept.com/route?route=common%2Fajax"
Just replace your CURL code with this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://live.bharticoncept.com/route?route=common%2Fajax');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "mod=liveclasses&ack=playvod&stream=https%3A%2F%2Fslhdovces.toprankers.com%2Fbharthiconcept%2F1128732%2F1128732-pr-BIOLOGYbyLavkushpandeysirClass21-bharthiconcept-1573820502927%2F1128732-pr-BIOLOGYbyLavkushpandeysirClass21-bharthiconcept-1573820502927.m3u8");
$headers = array();
$headers[] = 'Host: live.bharticoncept.com';
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
$headers[] = 'Cookie: tr_live_bharticoncept_com=so1ofdpkcleeshaq5dsevteia0';
$headers[] = 'Content-Length: 255';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Here is my code, I keep on getting the same error response.
"{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}"
I can't figure it out. I'm using Eloqua APIs to get basic information such as total number of accounts, landing pages, users, images, etc. It's weird because I tried the API on POSTMAN application and it did work perfectly Screenshot of postman response to the API
PHP
$objetos = array("data/accounts", "data/contacts", "assets/emails", "assets/landingPages", "assets/images", "assets/forms", "system/users", "assets/microsites", "assets/customObjects");
for ($i = 0; $i < 9; $i++){
$url = 'http://secure.p03.eloqua.com/API/REST/1.0/' . $objetos[$i] . '?count=1';
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
$headers = array(
'Content-type: application/json',
'Authorization: BASIC "MY TOKEN"'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() ."/EloquaApi_lvl1.cer");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
$data[$i] = curl_exec($ch);
curl_close($ch);
}
echo json_encode($data);
?>
JS
function getObjetos(){
$.get("objetos.php", function (data) {
console.log(data);
}, "json").done(function (data) {
console.log(data);
// rest of my code
}
Console
console.log response (click for image)
Try changing the url http://secure.p03.eloqua.com/API/REST/1.0/ to https
Use https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=KEY
My full PHP code here
$data = array();
$data['siteUrl'] = 'https://example.com';
$data['urlList'][] = 'https://example.com/news/1';
$ch = curl_init('https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=KEY');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'charset=utf-8',
'HTTP/1.1'
),
CURLOPT_POSTFIELDS => json_encode($data)
));
$response = curl_exec($ch);
I created a Push messages program, it use firebase.google.com.
If a send push message via javascript the message is perfect, but if I send push message with Php curl the message do not same what I wrote in php file.
If I send via Php curl, the message will be what I wrote in service-worker.js.
Why is it?
Php code
function send_push_message($subscription){
if (empty($subscription)) return FALSE;
$ch = curl_init();
switch ($subscription["browser"]){
case "firefox":
curl_setopt($ch, CURLOPT_URL, "https://updates.push.services.mozilla.com/push/".$subscription["id"] );
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "TTL: 86400" ) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
break;
case "chrome":
/**
* https://console.firebase.google.com
* http://stackoverflow.com/questions/37427709/firebase-messaging-where-to-get-server-key
*/
// API access key from Google API's Console
define( 'API_ACCESS_KEY', '<API_SZERVER_KULCSOM>' );
$registrationIds = array($_GET["id"]);
// prep the bundle
$msg = array
(
'message' => 'My text',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gcm-http.googleapis.com/gcm/send" );
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_POSTFIELDS, json_encode($fields));
$response = curl_exec($ch);
echo $response;
if($response === false){
echo 'Hiba: '.curl_error($ch);
}else{
echo 'Siker';
}
curl_close($ch);
break;
}
}
$tomb["browser"] = "chrome";
$tomb["id"] = $_GET["id"];
if(isset($_GET["id"])){
send_push_message($tomb);
}
service-worker.js
self.addEventListener('push', function(event) {
var title = 'xxxxxx';
var body = 'Szerbusz YYY';
var icon = '/images/icon-192x192.png';
var tag = 'simple-push-demo-notification-tag';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
});
Thank You
Thank You,
I do not send message when visitor sign up, via javascript.
I send message for example 5 days later via Php Curl.
Send message will be that what I wrote javascript. When I send message, I use only Php curl 5 days later!
Why is $msg value in Php curl, if don't use that?
Thanks
Hello great stackoverflow, am trying to re-write the api below from javascript to php in other to be able to make json curl but displays error below
Warning: curl_setopt() expects exactly 3 parameters, 4 given in C:\xampp\htdocs\firstcare\curl.php on line 15
js curl
curl -X POST
"http://my_api.com/accesstoken?grant_type=client_credentials"
-H "Content-Type: application/x-www-form-urlencoded"
-d 'client_id=$myClient_id'
-d 'client_secret=$myClient_secret
php curl conversion
<?php
// 0 means unlimited timeout
ini_set('max_execution_time', 0);
$data = array(
'Content-Type' => 'application/x-www-form-urlencoded',
'client_id' => 'myclient_id',
'client_secret' => 'myclient_secret'
);
$url='http://my_api.com/accesstoken?grant_type=client_credentials';
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url,$data);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
echo '<pre>' . print_r($json, true) . '</pre>';
?>
Thanks
Error was thrown because curl_setopt requires 3 parameters - you've passed 4 here:
curl_setopt($ch, CURLOPT_URL,$url,$data);
May I suggeest guzzlehttp, wrapper for curll...also u seem to be passing headers in $data as the forth paramenter remove $data. If u want to sent cllient id as GET append the $url with http_build_query()
Try these codes
<?php
// 0 means unlimited timeout
ini_set('max_execution_time', 0);
$postdata = array(
'client_id' => 'myclient_id',
'client_secret' => 'myclient_secret'
);
$header = array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$url='http://my_api.com/accesstoken?grant_type=client_credentials';
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
echo '<pre>' . print_r($json, true) . '</pre>';
?>