I'm trying to integrate Twilio into my application. When I run their sample code, it runs perfectly fine:
require_once "twilio-php-master/Twilio/autoload.php";
use Twilio\Rest\Client;
$AccountSid = "XXXXXX";
$AuthToken = "XXXXXX";
$client = new Client($AccountSid, $AuthToken);
$sms = $client->account->messages->create(
"+15555555555",
array(
'from' => "+15556665566",
'body' => "Hey, Monkey Party at 6PM. Bring Bananas!"
)
);
However, when I try to integrate it into a class, I keep getting a PHP Fatal Error Class 'Twilio\Rest\Client' not found every time it executes. This is the code in my class:
require_once "twilio-php-master/Twilio/autoload.php";
use Twilio\Rest\Client;
class Customer{
//constructors etc
public function sendText(){
$AccountSid = "XXXXXX";
$AuthToken = "XXXXXX";
$client = new Client($AccountSid, $AuthToken);
$sms = $client->account->messages->create(
'+15555555555',
array(
'from' => "+15556665566",
'body' => "Hey, Monkey Party at 6PM. Bring Bananas!"
)
);
}
}
I've checked the opening tags in the Twilio Library, I've ensured that the file location is correct (it throws the error when the
$client = new Client($AccountSid, $AuthToken);
line is executed, not at the require_once) but I can't seem to figure out what's going wrong.
Related
I am trying to send a simple SMS via Twilio with Php, but i get this Fatal error,
Uncaught exception 'Twilio\Exceptions\TwilioException' with message
'Unknown context accounts' in
C:\xampp\htdocs\Twilio\vendor\twilio\sdk\Twilio\Rest\Client.php:687
Stack trace:
0 C:\xampp\htdocs\Twilio\twilio.php(24): Twilio\Rest\Client->__call('accounts', Array)
1 C:\xampp\htdocs\Twilio\twilio.php(24): Twilio\Rest\Client->accounts('AC8687f4eaba8c6...')
2 {main} thrown in
C:\xampp\htdocs\Twilio\vendor\twilio\sdk\Twilio\Rest\Client.php on
line 687
This is my local server code:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$account_sid = 'AC8687f4eaba8c68XXXXXXXXXXXXX';
$auth_token = '6baf210351f27a38850XXXXXXXXXXXXXXXX';
$client = new Client($account_sid, $auth_token);
$messages = $client->accounts('AC8687f4eaXXXXXXXXXXX')
->messages->create('+52722XXXXXXX', array(
'From' => '+151240XXXXX',
));
?>
Twilio developer evangelist here.
It looks as though you are trying to send a message from the account you authorised the PHP library with in the first place. In this case, you do not need to call to the accounts resource first. It may have been an intentional omission, but I also notice your message doesn't have a body.
The following code should work for you:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$account_sid = 'AC8687f4eaba8c68XXXXXXXXXXXXX';
$auth_token = '6baf210351f27a38850XXXXXXXXXXXXXXXX';
$client = new Client($account_sid, $auth_token);
$messages = $client->messages->create('+52722XXXXXXX', array(
'From' => '+151240XXXXX',
'Body' => 'Hello from my PHP code!'
));
?>
I am creating my own message logging when sending and receiving messages from/to twilio via the PHP REST api sdk 5. I want to get the message sid when sending a new message and digging around in the code I found I can get it from
Twilio\Http\CurlClient.php
line 36 starts:
36 list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')
37 ? array($parts[1], $parts[2])
38 : array($parts[0], $parts[1]);
I added line 39
39 $GLOBALS["curlResponseBody"] = $body;
so I can retrieve the json response which has an entry for 'sid' of the message just created.
There has go to be a method for getting that information but I just haven't seen it mentioned anywhere in respect to SDK5.
Here is the code I am using to create the message:
require_once 'Twilio/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$client = new Client($account_sid, $auth_token);
$client->messages->create(
$toPhone,
array(
'from' => $fromTwilioPhone,
'body' => $responseMessage,
)
);
//This edit to the Twilio PHP Library is found in Twilio\Http\CurlClient.php
$curlResponseBody = json_decode($GLOBALS["curlResponseBody"]);
$newMessageSid = $curlResponseBody->sid;
Is there some way to use $client to get to the 'sid' of the message just created?
Twilio evangelist here.
The create() function returns an object that should let you get the message sid:
$client = new Client($account_sid, $auth_token);
$msg = $client->messages->create(
$toPhone,
array(
'from' => $fromTwilioPhone,
'body' => $responseMessage,
)
);
echo $msg.Sid
Hope that helps.
I'm trying to connect to the twilio for sending SMS. It was working but it stopped. After i returned from holidays.
Well SMS sending is working, but i cant echo the answer from Twilio.
I found out that my php file is working sweet on PHP 5.3 but on 5.6 it is throwing an error. So it has something to do with echoing $client but i dont know whats wrong.
Here is my code:
<?php
// this line loads the library
require dirname(__FILE__) . "../../../includes/Services/Twilio.php";
$account_sid = 'XXX';
$auth_token = 'XXX';
$client = new Services_Twilio($account_sid, $auth_token);
//Get the submitted data
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$mamiMobile = $request -> mamiPhone;
$text = $request -> smsoffer;
$client->account->messages->create(array(
'To' => $mamiMobile,
'From' => "+41798071977", //From Number from Twilio
'Body' => $text
));
//This works on php 5.3 but on 5.6 it is not working!
echo ($client);
;?>
The Error i get in the PHP.log:
[03-Mar-2016 18:18:16 Europe/Zurich] PHP Catchable fatal error: Method Services_Twilio::__toString() must return a string value in /Applications/MAMP/htdocs/angular-bootstrap-admin-web-app-with-angularjs/angular/includes/php/sms_connector_twillio_offer.php on line 34
Twilio developer evangelist here.
In order to echo the response from Twilio you shouldn't be echoing the $client itself, but the response to $client->account->messages->create. Why not try something like:
$response = $client->account->messages->create(array(
'To' => $mamiMobile,
'From' => "+41798071977", //From Number from Twilio
'Body' => $text
));
echo ($response);
In answer to what changed between PHP 5.3 and 5.6, my guess is that something happened between those versions to how $this responds to foreach such that this code no longer works as expected.
Could you raise a bug in the GitHub issues for the twilio-php project so that someone can take a look at it? Thanks!
Hi i have an issue when i call Google Drive API using the PHP Client Library.
my coding is
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($title);
$result = $service->files->insert($file, array(
'data' => file_get_contents("dd.doc"),
'mimeType' => 'text/plain',
'uploadType' => 'multipart'
));
I got the following errors
Undefined variable: service Trying to get property of non-object
Call to a member function insert()
Did you include the client lib in your script? or did you just forget to add that part?
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
Also check that $service was created properly. you are missing that code.
$client_id = '<YOUR_CLIENT_ID>';
$client_secret = '<YOUR_CLIENT_SECRET>';
$redirect_uri = '<YOUR_REDIRECT_URI>';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
A very good example can be found here
I'm using this php script to INSERT (upload) a file to my Google Drive, and its perfect:
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$drive = new Google_Client();
$drive->setClientId('XXX');
$drive->setClientSecret('YYY');
$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));
$gdrive = new Google_DriveService($drive);
$drive->setAccessToken(file_get_contents('token.json'));
$doc = new Google_DriveFile();
$doc->setTitle('Test');
$doc->setDescription('Test Document');
$doc->setMimeType('text/plain');
$content = file_get_contents('test.txt');
$output = $gdrive->files->insert($doc, array(
'data' => $content,
'mimeType' => 'text/plain',
));
print_r($output);
Now I want to UPDATE (not upload) my existing Google Drive file, and I'm using this script:
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$drive = new Google_Client();
$drive->setClientId('XXX');
$drive->setClientSecret('YYY');
$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));
$gdrive = new Google_DriveService($drive);
$drive->setAccessToken(file_get_contents('token.json'));
$fileId = "ZZZ";
$doc = $gdrive->files->get($fileId);
$doc->setTitle('Test'); // HERE I GET THE ERROR "CALL TO A MEMBER FUNCTION SETTITLE()..."
$doc->setDescription('Test Document');
$doc->setMimeType('text/plain');
$content = file_get_contents('test.txt');
$output = $gdrive->files->update($fileId, $doc, array(
'newRevision' => $newRevision,
'data' => $content,
'mimeType' => 'text/plain',
));
print_r($output);
Unluckly I get this error:
PHP Fatal error: Call to a member function setTitle() on a non-object in line $doc->setTitle...
I have followed THIS reference. Please can u help me to resolve the issue, or can you suggest the precise and right code to UPDATE a file to Google Drive through php? Thanks!
You are expecting $doc to be an object, which it is not because the Google client libraries are configured to return data arrays instead of objects by default.
To change this behavior without modifying the original source you can add a local_config.php file next to the existing config.php that has these contents:
<?php
$apiConfig = array(
'use_objects' => true,
);
The client libraries will detect and use this configuration automatically.