How send sms in php? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 15 days ago.
Improve this question
How do I fix this issue? I cannot send sms on my work.
$ch = curl_init(); //curl()
$parameters = array(
'apikey' => '', //Your API KEY
'number' => '',//number
'message' => 'I just sent my first message with Semaphore', //message
'sendername' => 'SEMAPHORE');
curl_setopt( $ch, CURLOPT_URL,'https://semaphore.co/api/v4/messages');
curl_setopt( $ch, CURLOPT_POST, 1 );
//Send the parameters set above with the request
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $parameters ) );
How do I fix this issue? I have my apikey in semaphore.

Hey i tried this code and print output
<?php
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
$ch = curl_init();
$parameters = array(
'apikey' => '', //Your API KEY
'number' => '0987654321',//for testing no
'message' => 'I just sent my first message with Semaphore',
'sendername' => 'SEMAPHORE'
);
curl_setopt( $ch, CURLOPT_URL,'https://semaphore.co/api/v4/messages' );
curl_setopt( $ch, CURLOPT_POST, 1 );
//Send the parameters set above with the request
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $parameters ) );
// Receive response from server
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close ($ch);
//Show the server response
echo $output;
i got return output
"message_id":73840803,
"status":"Pending",
Using live key and test and try to hit same code check status is pending to received

This worked for me
$ch = curl_init();
$parameters = array(
'apikey' => '',
'number' => '9000000000',
'message' => 'I just sent my first message with Semaphore',
'sendername' => 'SEMAPHORE'
);
curl_setopt( $ch, CURLOPT_URL,'https://semaphore.co/api/v4/messages' );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//Send the parameters set above with the request
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $parameters ) );
$result = curl_exec($ch);
print_r($result);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
print_r($error_msg);
}
curl_close($ch);
exit;

Related

PHP curl_exec stops execution of the rest of the script without error logs

I have a php script running with curl and reporting setup as such, but it stops executing on the line with the curl_exec() method, and there are no errors thrown:
$fields = array(
'auth' => array(
'cId' => 'DEADBEEF-8675309-8675309-123123-4321',
'sig' => 'Not really a signature',
'data' => array(
'field' => 'pat',
'value' => '12',
'id1' => 'lasagna',
'id2' => 'peperoni'
)
),
'item1' => 'QPFMgH1TnCTLrylGeNs8yzYVVXxUgR0RHwj9jNwgXJJEfxODdoOKDOJLv66CSU5XKRfu4KYtDJB5rAmngxNrRDFpWU69oHMTlZoHAewuy3ft',
'item2' => 'gMiGdw==',
'tokenList' => array(
"token", "list"
)
);
$postfields = json_encode($fields);
error_reporting(E_ALL);
ini_set('display_errors', true);
$curl = curl_init('http://localhost:8080/endpoint');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//$result = curl_exec($curl);
echo "before curl_exec" . "\n\n";
$response = json_decode(curl_exec($curl));
echo "after curl_exec" . "\n\n";
print_r($response);
EDIT: forgot to include the line with json_encode for the $fields variable to pass to curl_setopt();
There are a couple of issues with your code:
you put the "post" data into $fields but then...
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
...you're passing $postFields to curl.
Assuming this is a typo, fixing with
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
won't work.
If the parameter used for CURLOPT_POSTFIELDS is an array it must be an associative array where keys and values are strings (or values that can be casted/converted to strings).
In your case $fields is an array where some values are arrays. That won't work (and raises a warning "array to string conversion").
You set an header that specifies that the data sent with the request is in JSON format
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
If you want to send the data into $fields as JSON with a POST request you can do it this way:
$fields = array( /* ... */ );
$json_fields = json_encode( $fields );
$curl = curl_init( 'http://localhost:8080/endpoint' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $json_fields );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json',
'Content-Length: ' . strlen($json_fields) ] );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
$response = curl_exec( $curl );
$response_data = json_decode( $response );
If still something needs to be fixed proceed with debugging by steps to isolate the problem:
// 1 - Check curl error
$errnum = curl_errno( $curl );
$errstr = curl_strerror( $errnum );
echo "Error: $errnum - $errstr\n";
// 2 - Check http status code of the response
$status = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
echo "Status: $status\n";
// 3 - Inspect the response before coverting to JSON
echo "Response:\n";
var_export( $response );
echo "\n";

Simple PHP discord notification now respond with error code 50006

I have a simple daily PHP notification to a discord webhook. It was working for almost a year, but it's now responding me an error:
{"message": "Cannot send an empty message", "code": 50006}
$content is created before and filled, and it's not empty.
I replaced the real username and avatar link here.
$hookObject = json_encode([
"type" => "rich",
"content" => "**Rotation today**\n\n".$content,
"username" => "avatar",
"avatar_url" => "link to avatar",
"tts" => false,
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
$ch = curl_init();
curl_setopt_array( $ch, [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $hookObject,
CURLOPT_HTTPHEADER => [
"Length" => strlen( $hookObject ),
"Content-Type" => "application/json"
]
]);
$response = curl_exec( $ch );
curl_close( $ch );
Does anyone know what might be the problem now?
I also got this error last day.
I found this code and it works:
//===========================================
// Create new webhook in your Discord channel settings and copy&paste URL
//===========================================
$webhookurl = "YOUR_WEBHOOK_URL";
//===========================================
// Compose message. You can use Markdown
// Message Formatting -- https://discordapp.com/developers/docs/reference#message- formatting
//===========================================
$msg = "Test **message** ";
$json_data = array ('content'=>"$msg");
$make_json = json_encode($json_data);
$ch = curl_init( $webhookurl );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $make_json);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );

Google Firebase iOS push works in console but not in API

I'm using https://github.com/arnesson/cordova-plugin-firebase/ to receive Google Firebase messages on a ionic based app.
After set certificates, install plugin and setup Firebase account I was able to receive notifications (on both android and ios devices) sended through the Firebase Console.
But when I send through the Firebase API (https://firebase.google.com/docs/cloud-messaging/http-server-ref) only android devices receive the notification. I'm using the following code:
$data = Array
(
[to] => <token>
[notification] => Array
(
[title] => My Title
[text] => Notification test
[sound] => default
[vibrate] => 1
[badge] => 0
)
)
$jsonData = json_encode($data);
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
$header = array(
'Content-Type: application/json',
"Authorization: key=".$gcmApiKey
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
No errors are returned:
{"multicast_id":904572753471539870406,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1477063422322568734%3d5c78243d5c7824"}]}
What can be wrong?
For iOS, try adding in parameter priority set to high and content_available set to true in your payload.
See the parameter details here.
try this code
function sendGCM($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "YOUR_KEY_HERE",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
?>
also try it in curl terminal
curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"body\":\"Yellow\"},\"priority":10}"
a little late but with working example,
I'm using the code below for ios and android push, you are missing the priority and content_available fields
example :
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => $token,
'notification' => array('body' => $message , "sound" => "default"),
'data' => $message,
"sound"=> "default",
'priority' => "high" ,
'content_available' => false
);
$headers = array(
'Authorization:key = your-key',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);

Send GCM Notification from PHP

I am trying to send GCM notification using php. The script runs fine if I hard code the values and I do receive the notification as well. But when I try to pass the value as input, it fails with error:
{
"multicast_id":8013504586085987118,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[
{
"error":"InvalidRegistration"
}
]
}
Working Code:
<?php
// Replace with the real server API key from Google APIs
$apiKey = "Google Api Key";
// Replace with the real client registration IDs
$registrationIDs = array("Registerationid");
// Message to be sent
//$message = "Your message e.g. the title of post";
$msg = array
(
'message' => 'TestMessage',
'title' => 'TestTitle',
'subtitle' => 'TestSubtitle',
'tickerText' => 'TestTicker',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $msg ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the URL, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>
code throwing error
<?php
$registrationIDs = array( $_GET['id'] );
// Replace with the real server API key from Google APIs
$apiKey = "API KEY";
// Replace with the real client registration IDs
//$registrationIDs = array( $_GET['id'] );
// Message to be sent
//$message = "Your message e.g. the title of post";
$msg = array
(
'message' => 'TestMessage',
'title' => 'TestTitle',
'subtitle' => 'TestSubtitle',
'tickerText' => 'TestTicker',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $msg ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the URL, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
// print the result if you really need to print else neglate thi
echo $result;
//print_r($result);
//var_dump($result);
?>

error in Android: GCM using PHP

I used following code in PHP to send GCM message to Android:
<?php
$apiKey = "xxxxx"; //my api key
$registrationIDs = array("APA91bGGN7o7AwVNnv35lwP5Jw8OTJQL331XcxPfEIu4xt-ZKLe6R0aSSbAve99uKSDXhzE9L2PVLihpqFt0DEawhymUs9h5ICbTMweMAEJypg6ZLFqmf6SOGlyULQzudw9MM1DjbPaaKbo--wxWoHGkjyec2H_63e7mesYjaRf4_rgxBe655M0");
// Message to be sent
$message = "Message Text";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
It's working fine. Here I added my device tokens manually(hardcoded).
But when i tried getting device tokens from the database, I am getting below mentioned error.
Code:
<?php
include 'config.php';
if($_REQUEST['msg']!='')
{
echo $message = $_REQUEST['msg'];
// Replace with real BROWSER API key from Google APIs
$apiKey ="xxxxxx"; //my api key
// Replace with real client registration IDs
$sql = mysql_query("SELECT `device_token` FROM `users`");
$result = mysql_fetch_array($sql);
$registrationIDs = $result;
// Message to be sent
$message = $_REQUEST['msg'];
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
}
I also tried using json_encode($registration_ids) with no use.
"registration_ids" field is not a JSON array
I finally got a solution. I used a for loop to get all the results into an array and converted array into JSON array using json_encode();
I was getting the same error because my array indexes are not properly sorted I fix it by using PHP's sort() function.
Before Sort
$registration_ids = Array
(
[1] => "xxx"
[6] => "xxx"
[8] => "xxx"
)
Sort
sort($registration_ids);
After Sort
$registration_ids = Array
(
[0] => "xxx"
[1] => "xxx"
[2] => "xxx"
)
Pass to PHP's cURL
$fields = array(
'registration_ids' => $ids,
'data' => array('message' => $data)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

Categories