How can send sms by url codeigntor - codeigniter

http://52.36.50.145:8080/MainServlet?orgName=XXX&userName=XXX&password=XXX&mobileNo=967777662112&text=
msg + "&coding=2"
I have that's url how can send send sms by for mulit user codeigntor

You must use cURL in CodeIgniter. this function works fine for Sending SMS.
function sms_code_send($number='',$message='')
{
$username = 'username';
$password = '*******';
$originator = 'sender name';
$message = 'Welcom to ......, your activation code is : '.$message;
//set POST variables
$url = 'http://exmaple.com/bulksms/go?';
$fields = array(
'username' => urlencode($username),
'password' => urlencode($password),
'originator' => urlencode($originator),
'phone' => urlencode($number),
'msgtext' => urlencode($message)
);
$fields_string = '';
//url-ify the data for the POST
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
//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,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}

Related

How can I send sms using codeigniter 4?

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.

can't autometic sms using twilio

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'
)
);
}

This is my codiegniter contact us form code , it's correct , but i want include mail functionality in this contact us form,

this code is working perfect for saving data into databse , but i just want to include mail functionality in this code.
Please take a look.
I have tried a lot, but in vain. Any help would be nice.
<?php
class contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
}
function index()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required');
$this->form_validation->set_rules('companyname', 'Companyname', 'trim|required');
//$this->form_validation->set_rules('industryname', 'Industryname', 'trim|required');
$this->form_validation->set_rules('country', 'Country', 'trim|required');
$this->form_validation->set_rules('message', 'Message', 'trim|required');
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'companyname' => $this->input->post('companyname'),
//'industryname' => $this->input->post('industryname'),
'country' => $this->input->post('country'),
'message' => $this->input->post('message')
);
if ($this->db->insert('contacts', $data))
{
// i just want a mailer
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
redirect('contactform/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error. Please try again later!!!</div>');
redirect('contactform/index');
}
}
}
//custom callback to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
this code is working perfect , to save data in to database. Thanks in advance.
My mailer attempt is this
<?php
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user#gmail.com'; // email id
$config['smtp_pass'] = 'password'; // email password
$config['mailtype'] = 'html';
$config['wordwrap'] = TRUE;
$config['charset'] = 'iso-8859-1';
$config['newline'] = "\r\n"; //use double quotes here
$this->email->initialize($config);
?>
<?php
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'user#gmail.com';
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">successfully!</div>');
redirect('contactform/index');
}
else
{
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> error </div>');
redirect('contactform/index');
}
?>
I am assuming that you want to send mail from localhost.To do this you can use PHPMailer library
$this->load->library('PHPMailer/PHPMailer');
//require("class.phpmailer.php");
$mail=new PHPMailer(); //creat PHPMailer object
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; //needs login information
$mail->SMTPSecure = "tls"; //specifies tls security
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; //gmail smtp port
/******************* Set Your Credentials********************/
$mail->Username ="user#gmail.com" ; //SMTP gmail address
$mail->Password = "password"; // SMTP account password
$mail->From = "fromxyz"; //sender's gmail address
$mail->isHTML(true);
$mail->FromName = "From name";
$mail->AddAddress($email);//receiver's e-mail address
$mail->Subject = "Email Subject";//e-mail subject
$mail->Body ="Email Message";//e-mail message
$mail->WordWrap = 50;
if(!$mail->send())
{
echo "error";
}
else
{
echo "successs";
}
If this also not work try making some changes in gmail account:
Login to gmail.
Go to My Account Section.
Then Go To Sign-in & Security > Connected apps & sites
In this section make Allow less secure apps: ON
in you Controller try this type
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'companyname' => $this->input->post('companyname'),
//'industryname' => $this->input->post('industryname'),
'country' => $this->input->post('country'),
'message' => $this->input->post('message')
);
//load email libary
$this->load->library('email');
//configure email settings
$config['protocol'] = 'smtp`';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user#gmail.com'; // email id
$config['smtp_pass'] = 'password'; // email password
$config['mailtype'] = 'html';
$config['wordwrap'] = TRUE;
$config['charset'] = 'iso-8859-1';
$config['newline'] = "\r\n"; //use double quotes here
$this->email->initialize($config);
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'user#gmail.com';
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">successfully!</div>');
}
else
{
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> error </div>');
}
}

Twitter OAUTH and Codeigniter

I'm trying to display tweets on a website built on codeigniter but can't seem to pull in the user tweets. To do this, I'm pulling the following script into my header as an include and then printing the tweet in the appropriate section with the code below. I've already set up the access tokens and consumer keys as well. Any ideas on why this is working?
Include file in header
<?php
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$oauth_access_token = "ACCESS TOKEN HERE";
$oauth_access_token_secret = "ACCESS TOKEN SECRET HERE";
$consumer_key = "CONSUMER KEY HERE";
$consumer_secret = "CONSUMER KEY SECRET HERE";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json, false);
$latest_tweet = $twitter_data[0];
?>
Print tweet
<span class="tweet">"<?php if(!empty($latest_tweet)){echo $latest_tweet->text;} else{echo "Welcome to Time Equities!";} ?>"</span>

HybridAuth send tweet with image

I'm using the HybridAuth library.
I'd like to be able to post message to my authenticated users twitter profile with images.
The setUserStatus method works well to automatically send a tweet.
I wrote the following method :
function setUserStatus( $status, $image )
{
//$parameters = array( 'status' => $status, 'media[]' => "#{$image}" );
$parameters = array( 'status' => $status, 'media[]' => file_get_contents($image) );
$response = $this->api->post( 'statuses/update_with_media.json', $parameters );
// check the last HTTP status code returned
if ( $this->api->http_code != 200 ){
throw new Exception( "Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
}
}
The message I get from twitter is :
Ooophs, we got an error: Update user status failed! Twitter returned an error. 403 Forbidden: The request is understood, but it has been refused.
How Can I get more precise info about error ?
Does anybody allready success in sending a picture attached to a tweet ?
Thanks !
Hugo
Thanks #Heena for making myself wake up on this question, I MADE IT ;)
function setUserStatus( $status )
{
if(is_array($status))
{
$message = $status["message"];
$image_path = $status["image_path"];
}
else
{
$message = $status;
$image_path = null;
}
$media_id = null;
# https://dev.twitter.com/rest/reference/get/help/configuration
$twitter_photo_size_limit = 3145728;
if($image_path!==null)
{
if(file_exists($image_path))
{
if(filesize($image_path) < $twitter_photo_size_limit)
{
# Backup base_url
$original_base_url = $this->api->api_base_url;
# Need to change base_url for uploading media
$this->api->api_base_url = "https://upload.twitter.com/1.1/";
# Call Twitter API media/upload.json
$parameters = array('media' => base64_encode(file_get_contents($image_path)) );
$response = $this->api->post( 'media/upload.json', $parameters );
error_log("Twitter upload response : ".print_r($response, true));
# Restore base_url
$this->api->api_base_url = $original_base_url;
# Retrieve media_id from response
if(isset($response->media_id))
{
$media_id = $response->media_id;
error_log("Twitter media_id : ".$media_id);
}
}
else
{
error_log("Twitter does not accept files larger than ".$twitter_photo_size_limit.". Check ".$image_path);
}
}
else
{
error_log("Can't send file ".$image_path." to Twitter cause does not exist ... ");
}
}
if($media_id!==null)
{
$parameters = array( 'status' => $message, 'media_ids' => $media_id );
}
else
{
$parameters = array( 'status' => $message);
}
$response = $this->api->post( 'statuses/update.json', $parameters );
// check the last HTTP status code returned
if ( $this->api->http_code != 200 ){
throw new Exception( "Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
}
}
To make it work you have to do like this :
$config = "/path_to_hybridauth_config.php";
$hybridauth = new Hybrid_Auth( $config );
$adapter = $hybridauth->authenticate( "Twitter" );
$twitter_status = array(
"message" => "Hi there! this is just a random update to test some stuff",
"image_path" => "/path_to_your_image.jpg"
);
$res = $adapter->setUserStatus( $twitter_status );
Enjoy !
I did not understand it for hybridauth then I used this library
https://github.com/J7mbo/twitter-api-php/archive/master.zip
Then I was successful using code below: (appears elsewhere in stack)
<?php
require_once('TwitterAPIExchange.php');
$settings= array(
'oauth_access_token' => '';
'oauth_access_secret' => '';
'consumer_key' => '';
'consumer_secret' => '';
// paste your keys above properly
)
$url_media = "https://api.twitter.com/1.1/statuses/update_with_media.json";
$requestMethod = "POST";
$tweetmsg = $_POST['post_description']; //POST data from upload form
$twimg = $_FILES['pictureFile']['tmp_name']; // POST data of file upload
$postfields = array(
'status' => $tweetmsg,
'media[]' => '#' . $twimg
);
try {
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url_media, $requestMethod)
->setPostfields($postfields)
->performRequest();
echo "You just tweeted with an image";
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>

Resources