I am using following PHP script for APNS
function send_push_ios_test($msg)
{
// Put your device token here (without spaces):
$deviceToken = '6778601ee2fd7809a90f2eaf709d5bbbbd3bec38c5b35e1428d7857f4b6ec0a7';
//$deviceToken = '6778601e e2fd7809 a90f2eaf 709d5bbb bd3bec38 c5b35e14 28d7857f 4b6ec0a7';
// Put your private key's passphrase here:
$passphrase = 'Pass#123';
// Put your alert message here:
$message = $msg;
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '../uploads/ios_pns/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => array(
'body' => $message,
'action-loc-key' => 'Bango App',
),
'badge' => 2,
'sound' => 'oven.caf',
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
}
The script shows connected to APNS & then message delivered successfully, but I am not getting push message on my device. Can anybody help me out with this.
Push notification is enabled on my device for sample app as well as from XCODE.
Related
I'm trying to send SMS using CodeIgniter 4 but something went wrong any help or another way to send?
This is my code:
public function message()
{
/*Check submit button */
if ($this->request->getPost()) {
$email = $this->input->post('email');
$data=$this->users_model->getUserByEmail($email);
$phone=$data['phone'];
$authKey = "3456655757gEr5a019b18";
/*Multiple mobiles numbers separated by comma*/
$mobileNumber = $phone;
/*Sender ID,While using route4 sender id should be 6 characters long.*/
$senderId = "ABCDEF";
/*Your message to send, Add URL encoding here.*/
$message = "From Codeigniter 4";
/*Define route */
$route = "route=4";
/*Prepare you post parameters*/
$postData = array(
'authkey' => $authKey,
'mobiles' => $mobileNumber,
'message' => $message,
'sender' => $senderId,
'route' => $route
);
/*API URL*/
$url="https://control.msg91.com/api/sendhttp.php";
/* init the resource */
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
/*,CURLOPT_FOLLOWLOCATION => true*/
));
/*Ignore SSL certificate verification*/
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
/*get response*/
$output = curl_exec($ch);
/*Print error if any*/
if (curl_errno($ch)) {
echo 'error:' . curl_error($ch);
}
curl_close($ch);
echo "Message Sent Successfully !";
}
}
After run the code above my web page return "Message Sent Successfully!", but nothing received in my phone. What is the problem?
What does the cURL call responds in the $output variable?
You already put the output in it and i think it will guide you to the reason why the SMS is not sending out to your phone.
after uploading an image in web system a i want to push the image to another web system b.i am using guzzle http client to push and save the image in system b.i have been able to save the image in system a but when it reaches the part to push and save to system b an error that i have set to show when there is an error on uploading the image.here is my function to save the image on system a
public function productSavePicture(Request $request)
{
try {
$validation = Validator::make($request->all(), [
'product_id' => 'required',
]);
if ($validation->fails()) {
throw new \Exception("validation_error", 19);
}
$product_details = product::where('systemid', $request->product_id)->first();
if (!$product_details) {
throw new \Exception('product_not_found', 25);
}
if ($request->hasfile('file')) {
$file = $request->file('file');
$extension = $file->getClientOriginalExtension(); // getting image extension
$company_id = Auth::user()->staff->company_id;
if (!in_array($extension, array(
'jpg', 'JPG', 'png', 'PNG', 'jpeg', 'JPEG', 'gif', 'GIF', 'bmp', 'BMP', 'tiff', 'TIFF'))) {
return abort(403);
}
$filename = ('p' . sprintf("%010d", $product_details->id)) . '-m' . sprintf("%010d", $company_id) . rand(1000, 9999) . '.' . $extension;
$product_id = $product_details->id;
$this->check_location("/images/product/$product_id/");
$file->move(public_path() . ("/images/product/$product_id/"), $filename);
$this->check_location("/images/product/$product_id/thumb/");
$thumb = new thumb();
$dest = public_path() . "/images/product/$product_id/thumb/thumb_" . $filename;
$thumb->createThumbnail(
public_path() . "/images/product/$product_id/" . $filename,
$dest,
200);
$systemid = $request->product_id;
$product_details->photo_1 = $filename;
$product_details->thumbnail_1 = 'thumb_' . $filename;
$product_details->save();
// push image to system on saving
$client = new \GuzzleHttp\Client();
$url = "http://systemb/api/push_image";
$response = $client->request('POST',$url,[
'headers' => [ ],
'multipart' => [
[
'name' => $filename,
'contents' => file_get_contents($product_details->getPath()),
],
],
]);
} else {
return abort(403);
}
} catch (\Exception $e) {
if ($e->getMessage() == 'validation_error') {
return '';
}
if ($e->getMessage() == 'product_not_found') {
$msg = "Error occured while uploading, Invalid product selected";
}
{
$msg = "Error occured while uploading picture";
}
$data = view('layouts.dialog', compact('msg'));
}
return $data;
}
i am getting the error "Error occured while uploading picture" but the error is saved in systema but its unabe to be pushed in systemb..i havent understood where i have gone wrong with my code base but i guess that part on guzzle isnt being executed because the data is being saved in systema but its unable to be pushed to systemb.what might be the issue here
Your class Product doesnt have the method getPath() declared
file_get_contents($product_details->getPath())
Change it so it uses the path you used above that line
file_get_contents(public_path() . "/images/product/$product_id/".$filename)
I can send sms when a user click on a button but i can't send sms when a user set a time or date and sms will send autometic on that times.
in the below code is when a user click on a button for sent sms. Now what thinks should i change to solved my problem.Please help me i need help.
<?php
require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;
/*echo '+'.$_REQUEST['mobile'];*/
if (isset($_REQUEST['mobile'])) {
$sid = '';
$token = '';
/*$client = new Twilio\Rest\Client($sid,$token);*/
$client = new Client($sid, $token);
$message = $client->messages->create(
'+'.$_REQUEST['mobile'],
array(
'from' => '',
'body' => 'testing'
)
);
}
?>
You Have to add Country code with Plus sign Like +1 for USA and +91 for India.
require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;
if (isset($_REQUEST['mobile'])) {
$sid = '';
$token = '';
$client = new Client($sid, $token);
$message = $client->messages->create(
'+{Country_Code}'.$_REQUEST['mobile'],
array(
'from' => '',
'body' => 'testing'
)
);
}
I have a godaddy dedicated server on which I have installed SSL certificate from godaddy.
I am using following script for sending push notification
$passphrase = 'pass';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '../folder/file/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
//echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => 1,
'category' => 'NOTIFICATION'
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
fclose($fp);
When I run this code from the browser I receive notification but when i run this same code from cron job it gives me unable to connect error.
I ran the script from terminal it gives me the following error
stream_socket_client(): Failed to enable crypto
PHP Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error)
Check if the correct port is open -
http://www.canyouseeme.org. (You are looking for port 2195)
If it is not open then you need to call GoDaddy and ask them to open port 2195 on your dedicated server. (They will not open the port for shared hosting)
Here are a few tips that should can help you out:
Go to entrust.net/downloads/root_request.cfm and download entrust_2048_ca.cer
Add following code:
stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');
Make sure if path is right: '../folder/file/ck.pem' ?
Switch and try both the sandbox and live ssl links.
I've been working on upgrading from v 1 to 1.1 for a few days now. I have 100s of users that can't see their followers in my dashboard.
I followed all the steps by changing /1/ to /1.1/ still not working.
Getting error 404 (Not Found)
Controller:
function twitter_auth()
{
if ( !$this->tweet->logged_in() )
{
die('some how you are not logged in');
}
$tokens = $this->tweet->get_tokens();
$this->tweet->enable_debug(TRUE);
$user = $this->tweet->call('get', 'account/verify_credentials');
var_dump($user);
}
$options = array(
'count' => 10,
'page' => 2,
'include_entities' => 1
);
$timeline = $this->tweet->call('get', 'statuses/home_timeline');
var_dump($timeline);
}
Jobs:
function get_twitter_subscribers($args)
{
$rate = #file_get_contents('http://api.twitter.com/1.1/application/rate_limit_status.json');
$rate = #json_decode($rate);
if($rate->remaining_hits == 0):
$status = 'retask';
else:
$new_data = #file_get_contents('http://api.twitter.com/1.1/users/show.json? screen_name='.$args['account_id'].'&include_entities=true');
$new_data = #json_decode($new_data);
if(isset($new_data->followers_count)){
$insert_args = array(
'account_id' => $args['account_id'],
'metric' => 'subscribers',
'value' => $new_data->followers_count,
'platform' => 'twitter'
);
$insert = $this->CI->metrics_model->insert_metric($insert_args);
if (isset($insert)) { $status = 'complete'; }
}else{
$this->send_error_email("Function: get_twitter_subscribers - new_data->followers_count not set for account id: " . $args['account_id'] . "\r\n\r\n" . "args: " . http_build_query($args) . "\r\n\r\n" . 'http://api.twitter.com/1.1/users/show.json?screen_name='.$args['account_id'].'&include_entities=true');
}
endif;
if(!isset($status)){
$this->send_error_email("Function: get_twitter_subscribers - status not set." . "\r\n\r\n" . "args: " . http_build_query($args) . "\r\n\r\n" . 'http://api.twitter.com/1.1/users/show.json? screen_name='.$args['account_id'].'&include_entities=true');
$status = 'error';
}
return $status;
controllers:
function auth($platform)
{
if($platform == 'facebook'):
echo 'auth facebook';
elseif($platform == 'twitter'):
$this->load->library('tweet');
// current key in php page
$tokens = array(
'oauth_token' => 'xxxxxxxxxxxxx',
'oauth_token_secret' => 'xxxxxxxxxxxx'
);
$this->tweet->set_tokens($tokens);
if ( !$this->tweet->logged_in() ) :
$this->tweet->set_callback(site_url('manage/auth/twitter'));
$this->tweet->login();
else:
echo 'Twitter logged in. Return to manager';
endif;
endif;
}
What am I doing wrong?
This issue has been resolved. I created my own cURL library.
function get_twitter_subscribers($args){
$request = curl_init();
$bearer = "AAAAAAAAAAAAAAAAAAAAAJ%2xxxxxxxxxxxxxxx0000x0x0x0x0";
curl_setopt($request, CURLOPT_SSLVERSION, 1);
curl_setopt($request, CURLOPT_URL, 'https://api.twitter.com/1.1/users/show.json?screen_name='.$args['account_id'].'&include_entities=true');
curl_setopt($request, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$bearer));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$new_data = json_decode($file_get_contents = curl_exec($request));
curl_close($request);
if(isset($new_data->followers_count)){
$insert_args = array(
'account_id' => $args['account_id'],
'metric' => 'subscribers',
'value' => $new_data->followers_count,
'platform' => 'twitter'
);
$insert = $this->CI->metrics_model->insert_metric($insert_args);
if (isset($insert)) { $status = 'complete'; }
}else{
$this->send_error_email("Function: get_twitter_subscribers - new_data->followers_count not set for account id: " . $args['account_id'] . "\r\n\r\n" . "args: " . http_build_query($args) . "\r\n\r\n" . 'https://api.twitter.com/1.1/users/show.json?screen_name='.$args['account_id'].'&include_entities=true');
}
if(!isset($status)){
$this->send_error_email("Function: get_twitter_subscribers - status not set." . "\r\n\r\n" . "args: " . http_build_query($args) . "\r\n\r\n" . 'http://api.twitter.com/1.1/users/show.json?screen_name='.$args['account_id'].'&include_entities=true');
$status = 'error';
}
return $status;
}