How to access the callback status twilio when the call is finished? - php

I'm trying to access the status AFTER the call is done, and display it in the first page, not in the callback page.
i have calling page:
require 'twilio/Services/Twilio.php';
$sid = "xxx"; // Your Account SID from www.twilio.com/user/account
$token = "xxx"; // Your Auth Token from www.twilio.com/user/account
$client = new Services_Twilio($sid, $token);
try {
$call = $client->account->calls->create(
'000', // From a valid Twilio number
$number, // Call this number
// Read TwiML at this URL when a call connects (hold music)'
"http://myurl.com/voice.xml", array('Method' => 'GET','StatusCallback'=>'http://myurl.com/callback.php'));
if($call->status =='failed')result(array('status'=>'failed status '));
else result(array('status'=>$call->status));
} catch (Exception $e) {
result(array('status'=>'not a valid number'));
}
my issue is that i'm not understanding how i can get the $_POST info that twilio will send to my callback.php on my calling page.php....

Please read Twilio API explaintion Click here for reference from Twilio Site to know the post parameters from Twilio on your callback back page.
http://myurl.com/callback.php
$callDur = $_POST['CallDuration'];
$recdDur = $_POST['RecordingDuration'];
To get these values on other page store them in session and use them where you want.

Related

Error: Voice call using twilo in php code

I using twilio for call system, but my error:
Error: Account not authorized to call +84983xxxxxx. Perhaps you need
to enable some international permissions:
https://www.twilio.com/user/account/settings/international
I checked in Vietnam (+84) via https://www.twilio.com/user/account/settings/international but still encounter the same error
My php code:
<?php
require 'twilio-php/Services/Twilio.php';
function call_notify($number) {
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'AC34962eb3f445dfa40893e64bb8xxxxxx';
$token = '9b0667c68299cf484228344d67xxxxxx';
// A phone number you have previously validated with Twilio
$phonenumber = '+155920xxxxx';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
$number, // The number of the phone receiving call
'http://103.255.238.29/notify.xml' // The URL Twilio will request when the call is answered
);
echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
call_notify("+84983xxxxxx");
Please help me, thanks
Twilio developer evangelist here.
I clicked on the URL to the international settings but was redirected to the Messaging international settings.
Can you go to the Voice international settings here https://www.twilio.com/console/voice/settings/geo-permissions and ensure you have Vietnam checked on that page too.
I'll feedback that the error is pointing to the wrong page.
Let me know if that helps!

Error while making outbound call on twilio

I am new to twilio.I am using twilio trial account and I am using php.I have created my account and the verification code was sent to my number .But when i try to make a call i get the error
Error: The source phone number provided, +1937xxxxxx6, is not yet verified for your account. You may only make calls from phone numbers that you've verified or purchased from Twilio.
Here is my code
<?php
// Include the Twilio PHP library
require 'twilio-php-master/Services/Twilio.php';
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'Axxxxxxxxxxxxxxxxxxffa58';
$token = 'f878xxxxxxxxxxxxxxxeb05';
// A phone number you have previously validated with Twilio
$phonenumber = '+19xxxxxxxx6';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
'+91xxxxxxxx7', // The number of the phone receiving call
'http://demo.twilio.com/welcome/voice' // The URL Twilio will request when the call is answered
);
echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
It happened to me when using the test account/token.
I double checked that the my phone number is verified but it was giving me this error.
Then I replaced the test account with my real account sid/token and the call succeeds.
I consider this as a bug in their system.
tested with python 3.6, twilio 6.16.2

Twilio SMS Using PHP (Not Sending or Loading PHP page)

So this is what's on the top of the PHP file:
<?php
// Start the session
session_start();
?>
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXX";
$token = "XXXXXXXXXXXXXXXXXX";
$client = new Services_Twilio($sid, $token);
$client->account->messages->sendMessage("+1234567890", "+0987654321", "Please?! I love you <3, Twilio Test");
?>
<?php
// Start the session
session_destroy();
?>
Then I have the <html> beneath.
The Issue I'm having is that the page just loads a white blank page and when I check the Twilio Log to see if any SMS are logged.
But the page loads just fine without the middle PHP (the part that's supposed to send the SMS through Twilio)
The Log is completely empty and shows no record of any outgoing SMS.
Any help would be appreciated!
Where are you getting sendMessage from? According to Docs this is how it is done
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "AC8ed2351ef2156d21a66faf913443188c";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$sms = $client->account->sms_messages->create("+14158141829", "+14159352345", "Jenny please?! I love you <3", array());
echo $sms->sid;

Writing a twitter API using php that is username specific

I've written a twitter api application using the following tutorial:
http://www.youtube.com/watch?v=GQaPt-gQVRI
How can I modify the script to generate a timeline stream that is specific to a user so that the application when run will show user's timeline stream and not mine (since i wrote the app and therefore it has my twitter credentials)
Thanks
the php application validates my twitter credentials using the following:
<?php
require 'tmhOAuth.php'; // Get it from: https://github.com/themattharris/tmhOAuth
// Use the data from http://dev.twitter.com/apps to fill out this info
// notice the slight name difference in the last two items)
$connection = new tmhOAuth(array(
'consumer_key' => 'my key',
'consumer_secret' => 'my secret',
'user_token' => 'my token', //access token
'user_secret' => 'my user secret' //access token secret
));
// set up parameters to pass
$parameters = array();
if ($_GET['count']) {
$parameters['count'] = strip_tags($_GET['count']);
}
if ($_GET['screen_name']) {
$parameters['screen_name'] = strip_tags($_GET['screen_name']);
}
if ($_GET['twitter_path']) { $twitter_path = $_GET['twitter_path']; } else {
$twitter_path = '1.1/statuses/user_timeline.json';
}
$http_code = $connection->request('GET', $connection->url($twitter_path), $parameters );
if ($http_code === 200) { // if everything's good
$response = strip_tags($connection->response['response']);
if ($_GET['callback']) { // if we ask for a jsonp callback function
echo $_GET['callback'],'(', $response,');';
} else {
echo $response;
}
} else {
echo "Error ID: ",$http_code, "<br>\n";
echo "Error: ",$connection->response['error'], "<br>\n";
So without having to pass a new username in the api call, how can i add a snippet to require the user to log in? and if i add that snippet for the user to log in, will the api automatically populate the authentication strings with the user's?
You can send a get request to the following url to get a users timeline.
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
You can replace the parameters screen_name with the username you want to access, and you can replace count with the number of tweets you would like to get, count is optional and doesn't have to be included.
You can read more about statuses/user_timeline on the office twitter API site: https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
If you wish to get a user to sign in then your best bet would be to use the twitteroauth library by abraham
Download and include in your project, then include the library and start a session.
require("twitteroauth/twitteroauth.php");
session_start();
Then create a new instance and authenticate with your app details. You can set a url to redirect to when the user authenticates. You also need to cache your tokens.
$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');
$request_token = $twitteroauth->getRequestToken('http://example.com/loggedin.php');
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
Redirect the user to twitter to authenticate
header('Location: '.$twitteroauth->getAuthorizeURL($request_token['oauth_token']));
In the file that you set twitter to redirect to you need to re-authenticate using the tokens created. Twitter will also add a parameter to your url which you use to create a access token for that user. Now when you send GET requests to twitter, it does it on behalf of the user logged in.
require("twitteroauth/twitteroauth.php");
session_start();
$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$user_info = $twitteroauth->get('account/verify_credentials');
print_r($user_info);
You can get additional details from $user_info which you can cache or store in a database, which will allow you to remember users that have already authenticated. You will need to use oauth_token and oauth_secret, something like this.
$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET', 'OAUTH_TOKEN', 'OAUTH_SECRET');

PHP/Twitter oAuth - Automated Tweets

Im using the following code to read to consumer_key and consumer_secret from config.php, pass it to twitter and retrieve some bits of information back from them.
What the script below attempts to do is 'cache' the request_token and request_secret. So in theory I should be able to reuse those details (all 4 of them to automatically tweet when required).
<?php
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
$consumer_key = CONSUMER_KEY;
$consumer_secret = CONSUMER_SECRET;
if (isset($_GET["register"]))
{
// If the "register" parameter is set we create a new TwitterOAuth object
// and request a token
/* Build TwitterOAuth object with client credentials. */
$oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request = $oauth->getRequestToken();
$request_token = $request["oauth_token"];
$request_token_secret = $request["oauth_token_secret"];
// At this I store the two request tokens somewhere.
file_put_contents("request_token", $request_token);
file_put_contents("request_token_secret", $request_token_secret);
// Generate a request link and output it
$request_link = $oauth->getAuthorizeURL($request);
echo "Request here: " . $request_link . "";
die();
}
elseif (isset($_GET["validate"]))
{
// This is the validation part. I read the stored request
// tokens.
$request_token = file_get_contents("request_token");
$request_token_secret = file_get_contents("request_token_secret");
// Initiate a new TwitterOAuth object. This time we provide them with more details:
// The request token and the request token secret
$oauth = new TwitterOAuth($consumer_key, $consumer_secret,
$request_token, $request_token_secret);
// Ask Twitter for an access token (and an access token secret)
$request = $oauth->getAccessToken();
// There we go
$access_token = $request['oauth_token'];
$access_token_secret = $request['oauth_token_secret'];
// Now store the two tokens into another file (or database or whatever):
file_put_contents("access_token", $access_token);
file_put_contents("access_token_secret", $access_token_secret);
// Great! Now we've got the access tokens stored.
// Let's verify credentials and output the username.
// Note that this time we're passing TwitterOAuth the access tokens.
$oauth = new TwitterOAuth($consumer_key, $consumer_secret,
$access_token, $access_token_secret);
// Send an API request to verify credentials
$credentials = $oauth->oAuthRequest('https://twitter.com/account/verify_credentials.xml', 'GET', array());
// Parse the result (assuming you've got simplexml installed)
$credentials = simplexml_load_string($credentials);
var_dump($credentials);
// And finaly output some text
echo "Access token saved! Authorized as #" . $credentials->screen_name;
die();
}
?>
When i run /?verify&oauth_token=0000000000000000 - It works however trying to resuse the generated tokens etc... I get a 401
Here is the last bit of code where I attempt to reuse the details from Twitter combined with my consumer_key and consumer_secret and get the 401:
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
// Read the access tokens
$access_token = file_get_contents("access_token");
$access_token_secret = file_get_contents("access_token_secret");
// Initiate a TwitterOAuth using those access tokens
$oauth = new TwitterOAuth($consumer_key, $consumer_key_secret,
$access_token, $access_token_secret);
// Post an update to Twitter via your application:
$oauth->OAuthRequest('https://twitter.com/statuses/update.xml',
array('status' => "Hey! I'm posting via #OAuth!"), 'POST');
Not sure whats going wrong, are you able to cache the details or do i need to try something else?
You can't store the OAuth tokens in cache and to more than 1 request with it, as OAuth is there to help make the system secure, your "oauth_token" will contain some unique data, this token will only be able to make one call back to twitter, as soon as the call was made, that "oauth_token" is no longer valid, and the OAuth class should request a new "oauth_token", thus making sure that every call that was made is secure.
That is why you are getting an "401 unauthorized" error on the second time as the token is no longer valid.
twitter is still using OAuth v1 (v2 is still in the draft process even though facebook and google already implemented it in some parts)
The image below describes the flow of the OAuth authentication.
Hope it helps.
A while ago I used this to connect to twitter and send tweets, just note that it did make use of some Zend classes as the project was running on a zend server.
require_once 'Zend/Service/Twitter.php';
class Twitter {
protected $_username = '<your_twitter_username>';
protected $_token = '<your_twitter_access_token>';
protected $_secret = '<your_twitter_access_token_secret>';
protected $_twitter = NULL;
//class constructor
public function __construct() {
$this->getTwitter();
}
//singleton twitter object
protected function getTwitter() {
if (null === $this->_twitter) {
$accessToken = new Zend_Oauth_Token_Access;
$accessToken->setToken($this->_token)
->setTokenSecret($this->_secret);
$this->_twitter = new Zend_Service_Twitter(array(
'username' => $this->_username,
'accessToken' => $accessToken,
));
$response = $this->_twitter->account->verifyCredentials();
if ($response->isError()) {
throw new Zend_Exception('Provided credentials for Twitter log writer are wrong');
}
}
return $this->_twitter;
}
//send a status message to twitter
public function update( $tweet ) {
$this->getTwitter()->status->update($tweet);
}
}
In your second script, it looks like you aren't setting the Consumer Key and Consumer Secret when you create your TwitterOAuth instance.
// Initiate a TwitterOAuth using those access tokens
$oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);

Categories