ini_set('max_execution_time', 3000);
require 'Services/Twilio.php';
$version = "2010-04-01";
$sid = 'xxxxxx';
$token = 'xxxxxx';
$phonenumber = 'xxxxxxxxxx';
$client = new Services_Twilio($sid, $token, $version);
try {
$call = $client->account->calls->create($phonenumber, "3104200693", "http://demo.twilio.com/docs/voice.xml", array(
"Method" => "GET",
"StatusCallback" => "http://localhost/twilio/call-response.php",
"StatusCallbackMethod" => "GET",
"StatusCallbackEvent" => array("initiated", "ringing", "answered", "completed"),
"IfMachine" => "Continue",
));
//header("Location: http://localhost/twilio/call-response.php");
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
This is my twilio code to call a number, i am successfully calling with this, but i want to store response in database like call completed or not and if answered then who answered call human or machine.
Please help.
The Twilio servers will send an HTTP request to the URL given as StatusCallback. Quite obviously, hopefully, Twilio cannot contact your localhost. 1) your computer is probably not physically reachable from outside your network, and 2) localhost is something different for everyone.
You need to provide a URL which is publicly accessible from the internet. That means you either need to run your code in a server, or you use a service like http://ngrok.com to establish a tunnel to your local machine during development.
I dont think "StatusCallback" => "http://localhost/twilio/call-response.php" will make you get call response. You should give a server url.
For example
"StatusCallback" => "http://example.com/twilio/call-response.php"
In call-response.php,
<?php
file_put_contents(__DIR__."/status_log.txt", json_encode($_REQUEST));
// twilio call status and other sata will be written to this file. Please check this file after call.
// I am giving because i dont know correct twilio returning params
?>
More info here
Related
The QuickBooks-V3-PHP-SDK is giving me a hard time.
I'm trying to use it to sync invoice data. I can successfully get authorization using OAuth 2.0 and connect my client PHP web application to QuickBooks Online. However, while I can successfully make a getCompanyInfo call and receive the results, I can't make a getUserInfo call (or most of the other API calls for that matter), as it will complain about the "access token object" not being set yet.
While connecting to QuickBooks and getting authorization, everything seems to work accurately - and the access token object is successfully placed in the PHP Session. But for some reason, it complains when I try to get the UserInfo data, or use any of the other API calls.
Here's the error that I receive:
Fatal error: Uncaught QuickBooksOnline\API\Exception\SdkException: [0]: Can't get OAuth 2 Access Token Object. It is not set yet.
thrown in /compiled/src/Core/OAuth/OAuth2/OAuth2LoginHelper.php on line 129
Here's how I try to get the UserInfo:
$dataService->updateOAuth2Token($accessToken);
$OAuth2LoginHelper = $dataService->getOAuth2LoginHelper();
$result = $OAuth2LoginHelper->getUserInfo();
Again, getting the CompanyInfo works without any problems.
Does anyone have any idea what's going on?
Below I'm including the code from apiCall.php (from the HelloWorld example package, further edited by myself). This file is used to make requests through Ajax after I already connected to and received authorization from QuickBooks Online.
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use QuickBooksOnline\API\Core\ServiceContext;
use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
use QuickBooksOnline\API\Facades\Invoice;
session_start();
function makeAPICall()
{
// Create SDK instance
$config = include('config.php');
$dataService = DataService::Configure(array(
'auth_mode' => 'oauth2',
'ClientID' => $config['client_id'],
'ClientSecret' => $config['client_secret'],
'RedirectURI' => $config['oauth_redirect_uri'],
'scope' => $config['oauth_scope'],
'baseUrl' => "development"
));
/*
* Retrieve the accessToken value from session variable
*/
$accessToken = $_SESSION['sessionAccessToken'];
$dataService->throwExceptionOnError(true);
/*
* Update the OAuth2Token of the dataService object
*/
$dataService->updateOAuth2Token($accessToken);
//Get the requested data:
$action = filter_input(INPUT_GET, "action", FILTER_SANITIZE_SPECIAL_CHARS);
$method = "get$action";
switch($action){
case "CompanyInfo":
$result = $dataService->$method();
break;
case "UserInfo":
$OAuth2LoginHelper = $dataService->getOAuth2LoginHelper();
$result = $OAuth2LoginHelper->getUserInfo();
break;
case "InvoiceExample":
$allInvoices = $dataService->Query("SELECT * FROM Invoice");
$result = $allInvoices;
break;
}
var_dump($result);
return $result;
}
$result = makeAPICall();
?>
Anyone any idea what I'm doing wrong?
The Intuit (QuickBooks) support team provided the answer to me. In my ApiCall code above, I had to use the following code to make the UserInfo request:
$result = $OAuth2LoginHelper->getUserInfo($accessToken->getAccessToken(), 'development');
Obviously, if this would involve production keys, I would have to replace 'development' with 'production'.
Iam using twilio Programmable voice while trying to Tracking the call status of an outbound call and save it log whether the user answer or cut the call ,but the problem i didn't get any log and while checking in twilio debugger it showing error:15003
Here my Code:
<?php
require_once "vendor/autoload.php";
use Twilio\Rest\Client;
$AccountSid= 'AC04421826f5ffaa58eaefa1ba6984dac2';
$AuthToken= 'token';
$client = new Client($AccountSid, $AuthToken);
try {
$call = $client->account->calls->create(
// to call.
"+9120000000",
"+1209000001",
array(
"url" => "http://demo.twilio.com/docs/voice.xml",
"method" => "GET",
"statusCallbackMethod" => "POST",
"statusCallback" => "localhost/twilio/log.txt",
"statusCallbackEvent" => array(
"initiated", "ringing", "answered", "completed"
)
)
);
echo "Started call: " . $call->sid;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Twilio evangelist here.
Looks like you have set the statuscallback property to point to localhost.
localhost is a domain that is only reachable from your own local machine so Twilio does not how to reach the localhost running on your local machine.
I'd suggest checking out ngrok which is a simple tool that lets you expose the web server running on your local machine via a publicly addressable domain.
Hope that helps
The issue here is a GET or POST request cannot be written directly to log.txt file and the file should be hosted on a http domain.
change the following code
"statusCallback" => "localhost/twilio/log.txt",
to
"statusCallback" => "http://yourdomain.com/twilio/log.php",
as you rather need an additional file log.php which can write to log.txt
<?php
$status = implode(',',$_REQUEST) ;
$handle = fopen('log.txt','a');
fwrite($handle, $status);
?>
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!
struggling to understand the oauth2 token and refresh token processes
ive got this code
$url = 'https://www.googleapis.com/oauth2/v3/token';
$data = array('client_id' => 'clientid', 'client_secret' => 'secret','refresh_token' => 'token','grant_type' => 'refresh_token');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'approval_prompt'=>'force',
'access_type'=>'offline',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
that code above gives me an access token , and i followed this link suggested by one fellow stackoverflower, pinoyyid, BUT , im confunsed on how to correctly use the resulting access token to access drive and copy a file...
all the process ive seen usually involves $client = new Google_Client() and im not sure on how to use the whole POST http://..... thing, so basically i need to figure out if i use the access token i got with the code above in a new instance of google client, or i simply do a post to a url with necesary info ( which im not clear on also ) any help/clarification is appreciated guys really
EDIT #1
what i want to achieve is to allow the end user to access my drive via my webpage, to let them copy a spreadsheet in my drive , and access it via my website, to store data on the spreadsheet,the spreadsheet will always be on my drive, never on the end user
EDIT #2
code as per your posts is as follows, using the service account,,,,the files are inside that gmail account which i created on the api console a service account
<?php
require 'Google/autoload.php';
$client = new Google_Client();
// Replace this with your application name.
$client->setApplicationName("TEST");
// Replace this with the service you are using.
$service = new Google_Service_Drive($client);
// This file location should point to the private key file.
$key = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/number-privatekey.p12');
$user_to_impersonate = 'admin#testpr.com';
$cred = new Google_Auth_AssertionCredentials(
'number#developer.gserviceaccount.com',
array('https://www.googleapis.com/auth/drive'), ****//this here has to be drive not drive.file
$key,
'notasecret',
'http://oauth.net/grant_type/jwt/1.0/bearer',
$user_to_impersonate
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$originFileId = "longnumber";
$copyTitle = 'copied';
$newfile = copyFile($service, $originFileId, $copyTitle);
print_r($newfile);
function copyFile($service, $originFileId, $copyTitle)
{
$copiedFile = new Google_Service_Drive_DriveFile();
$copiedFile->setTitle($copyTitle);
try {
return $service->files->copy($originFileId, $copiedFile);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
?>
so got it working just now, ty all for your time guys really and edited my post to reflect the dang thing
If you want to give the end user access to your drive, you have to give your application authority to make API calls on behalf of a user (in this case you) in your domain. For this you have to set up a service account and generate a p12 key in the Google Developers Console. You have to enter the https://www.googleapis.com/auth/drive API scope in your Admin Console as well.
Full explanation and examples can be found here: https://developers.google.com/api-client-library/php/auth/service-accounts.
To achieve this you also need the Google API's client library: https://github.com/google/google-api-php-client (also mentioned in the Google manual).
Code example to let users make API calls on behalf of one of your accounts: https://developers.google.com/api-client-library/php/guide/aaa_oauth2_service
I want to post some text automatically to my application twitter account. I create an account for that and take all the needed information and write a php code for it. it is simple code:
I made it just to test the post ability :
<?php
require_once 'oauth/twitteroauth.php';
$message = "hiiiiiii"; #actual message to twitter
define("CONSUMER_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("CONSUMER_SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("OAUTH_TOKEN", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("OAUTH_SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
print_r($connection);
$content = $connection->get('account/verify_credentials');
//print_r($content);
$connection->post('statuses/update', array('status' => $message));
?>
I get the information from the account so i think the connection is working but why i can not post any thing? i tried to post this message but after executing this code nothing happened on my twitter no new tweet is shown????
$connection->post is a method :
/**
* POST wrapper for oAuthRequest.
*/
function post($url, $parameters = array()) {
$response = $this->oAuthRequest($url, 'POST', $parameters);
if ($this->format === 'json' && $this->decode_json) {
return json_decode($response);
}
return $response;
}
My first port of call if you are able to receive information from the account using those credentials, would be that your app has read AND write (posting) permissions:
Go to dev.twitter.com/apps. Select your app, then under 'settings' -->
'application type' you will find what you need.
Although that didn't work, no matter what you changed in your code posting to your account through this app wouldn't work as that functionality was outside the permissions you set yourself. So now we know that you're able to post:
Change
$connection->post('statuses/update', array('status' => $message));
To
$result = $connection->post('statuses/update', array('status' => $message));
Then see what the value of $result is. This might provide some clues
Ok so the response is telling you, you still don't have permissions, you need to regenerate your token and access credentials then update your code with them:
define("CONSUMER_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("CONSUMER_SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("OAUTH_TOKEN", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("OAUTH_SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
Those credentials still have the previous permissions attached to them, when you request new token, it will reference the new permissions and allow you to post.