Hello Developers I am using the Twilio API and Purchased a Number.
I have also created a secure SSL site uploaded the following code. Then it will show the following error as
Error : Authenticate
<?php
// Include the Twilio PHP library
require 'Services/Twilio.php';
// Twilio REST API version
$version = '2010-04-01';
// Set our AccountSid and AuthToken
$sid = 'myWorkingSID';
$token = 'myWorkingAuthToken';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
try {
// Get Recent Calls
foreach ($client->account->calls as $call) {
echo "Call from $call->from to $call->to at $call->start_time of length $call->duration";
}
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
Kindly help me in this issue
I had exactly the same issue. In my case, I had a subaccount. And each subaccount has its own account SID and Auth Token. As soon as I used the subaccount credentials it worked (and by "it worked" I mean I got a different error, which is always good news).
Related
I want to integrate Twilio SMS Api in Phalcon 3 using dependency injection.It would be great if anyone could guide me in this process.
Just like any other PHP library that you want to use, you first need to check what is the installation method.
As noted by Nikolay Mihaylov in the comments, check the documentation for Twillo here:
https://www.twilio.com/docs/libraries/php
As you will see they do offer an installation through composer. All you need to do in your project then is:
composer require twilio/sdk
Registering Twillo in your DI container is the same as any other service:
// Your Account SID and Auth Token from twilio.com/console
$config = [
'sid' => 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'token' => 'your_auth_token',
]
// $di is the DI container
// Using the $config variable in the current scope
$di->set(
'db',
function () use ($config) {
$sid = $config['sid'];
$token = $config['token'];
$client = new \Twilio\Rest\Client($sid, $token);
return $client;
}
);
References:
https://docs.phalconphp.com/en/latest/reference/di.html
https://www.twilio.com/docs/libraries/php
Twilio newbie using test account. I followed the instructions listed here for installing Twilio php:
https://www.twilio.com/docs/quickstart/php/sms
Because I was getting a certificate error, my host provider suggested I changed the CURLOPT_SSL_VERIFYPEER => false (from true). But now I'm getting this error. How to fix?:
Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The requested resource /2010-04-01/Accounts//Messages.json was not found' in
require "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACbxxxxxxx";
$AuthToken = "0cfxxxxxxx";
// Step 3: instantiate a new Twilio Rest Client
//$client = new Services_Twilio($AccountSid, $AuthToken);
$http = new Services_Twilio_TinyHttp(
'https://api.twilio.com',
array('curlopts' => array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
))
);
$client = new Services_Twilio($sid, $token, "2010-04-01", $http);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+13121111111" => "Curious George",
// "+14158675311" => "Virgil",
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->messages->sendMessage(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"929-xxx-xxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, Monkey Party at 6PM. Bring Bananas!"
);
// Display a confirmation message on the screen
echo "Sent message to $name";
}
Twilio developer evangelist here.
Firstly, you should never set CURLOPT_SSL_VERIFYPEER to false in production. From the curl manual:
WARNING: disabling verification of the certificate allows bad guys to man-in-the-middle the communication without you knowing it. Disabling verification makes the communication insecure. Just having encryption on a transfer is not enough as you cannot be sure that you are communicating with the correct end-point.
For the sake of getting you going with Twilio though your issue is in the variable names you are using.
At the top of the file you set:
$AccountSid = "ACbxxxxxxx";
$AuthToken = "0cfxxxxxxx";
But when you create a Twilio you use $sid and $token.
$client = new Services_Twilio($sid, $token, "2010-04-01", $http);
If you change those to $AccountSid and $AuthToken it should work as you expected.
I am still new to Twilio. I am writing a PHP script which will connect to Twilio and parse some information about calls. I am using the code from this page:
https://www.twilio.com/docs/api/rest/call
Here is my code:
require_once '../twilio-library/Services/Twilio.php';
// Twilio REST API version
$version = '2010-04-01';
// Set our AccountSid and AuthToken
$sid = 'abc132xxxxx';
$token = 'xxxxeeffv';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls as $call) {
echo "->".$call->Sid."<- <br/>";
}
The browser just outputs many blanks. No Sid values. What am I doing wrong?
Twilio Developer Evangelist here.
Try doing the following to get call information.
<?php
require_once '../twilio-library/Services/Twilio.php';
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'abc132xxxxx';
$token = 'xxxxeeffv';
$client = new Services_Twilio($sid, $token);
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls as $call) {
echo $call->sid;
}
Notice that if you're using the latest version of the library, you don't need to specify a version on your request. Double check that the path ../twilio-library/Services/Twilio.php is correct for you though.
If you're only testing, you could move the file Twilio.php to the same directory where your code is and just import Twilio.php on it.
If you then decide to filter the logs by date, here's how you would do it.
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls->getIterator(0, 50, array(
"Status" => "completed",
"StartTime>" => "2015-03-01",
"StartTime<" => "2015-05-10"
)) as $call
) {
echo $call->sid;
}
Let me know how it goes.
I am using the following code that I got from the twilio website. I need to get the QueueSid of my queue named "Sales". How do I go about doing this? If there is documentation for this subject please point me there as well. Thanks in advance!
<?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
$accountSid = "ACYYYYYYYYYY";
$authToken = "XXXXXXXXX";
$client = new Services_Twilio($accountSid,$authToken);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$member = $client->account->queues->get('QU5ef8732a3c49700934481addd5ce1659')->members->get("Front");
$member->update(array(
"Url" => "https://dl.dropboxusercontent.com/u/11489766/learn/voice.xml",
"Method" => "POST"
));
echo $member->wait_time;
Twilio developer evangelist here.
You can search for all of your queues using the Queues list resource. Then you will want to filter by the Friendly name to get your queue. Try something like this:
<?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
$accountSid = "ACYYYYYYYYYY";
$authToken = "XXXXXXXXX";
$client = new Services_Twilio($accountSid, $authToken);
foreach($client->account->queues as $queue){
if ($queue->friendly_name == "Sales"){
$foundQueue = $queue;
break;
}
}
echo $foundQueue;
I am attempting to retrieve the data from our content experiments from google analytics...
I am using the following code, my creds are good, and have been censored for this post...
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('Hello Analytics API Sample');
// Visit https://cloud.google.com/console to generate your
// client id, client secret, and to register your redirect uri.
$client->setDeveloperKey('xxxxx');
$service = new Google_Service_Analytics($client);
try {
$results = $service->management_experiments->listManagementExperiments('xxxx', 'xxxx', 'xxxx');
} catch (apiServiceException $e) {
print 'There was an Analytics API service error ' . $e->getCode() . ':' . $e->getMessage();
} catch (apiException $e) {
print 'There was a general API error ' . $e->getCode() . ':' . $e->getMessage();
}
echo '<pre>';
print_r($results);
echo '</pre>';
I am using the following example ....
https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtExperimentsGuide#list
Any ideas on why I am getting a 401 unauthorized? That a login is required?
The problem is that you haven't Authorized access to your data yet. Since you say its only your own data you want to access i sugest you look into a service account. By setting up a service account in Google apis console it will allow you to access your own data with out needing to login and autenticate the code all the time.
Check the following link. Read though Before you begin make sure you do all that.
https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtExperimentsGuide#service
Register your application in the Google Developers Console
Authorize access to Google Analytics data.
Create an Analytics service object
You have skiped the first two steps and gone directly to step 3 creating the service object. Once you have done step 1 you can use the following code for step 2.
Here is an example of how to use a service account in php ServiceAccount
That sample project is for the PredictionService not the google analytics service. You need to edit it slightly.
require_once '../../src/Google/Client.php';
require_once '../../src/Google/Service/Analytics.php';
// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = 'INSERT_YOUR_CLIENT_ID';
const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_ACCOUNT_NAME';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '/super/secret/path/to/key.p12';
$client = new Google_Client();
$client->setApplicationName("Google Analytics Sample");
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME(Email),
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents(KEY_FILE))
);
$client->setClientId(CLIENT_ID);
$service = new Google_Service_Analytics($client);
Now you have $service that you can use with the rest of your calls. Note: I didnt have time to test that code let me know if it doesnt work and i will give you a hand in fixing it.