I got an error while adding Twilio SMS APi to my website. My site is in wordpress and using Woo commerce.
Error : Fatal error: Class 'Client' not found in /var/www/html/++++/wp-content/themes/dokan-theme-v2.2.2-child/functions.php on line 4583
My code is below:
function wl8OrderPlacedTriggerSomething($order_id){
//do something...
//echo get_stylesheet_directory_uri(). '/twilio-php-master/Twilio/Rest/Client.php';
require_once( get_stylesheet_directory_uri(). '/twilio-php-master/Twilio/autoload.php');
require( get_stylesheet_directory_uri(). '/twilio-php-master/Twilio/Rest/Client.php');
// Use the REST API Client to make requests to the Twilio REST API
// use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'xxxxxxxxxxxxxxxxxxxxxx';
$token = 'xxxxxxxxxxxxxxxx';
$client = new Client($sid, $token);
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to (xxxxxxx)
'xxxxxxxxx',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => '+xxxxxxx',
// the body of the text message you'd like to send
'body' => "Hey Jenny! Good luck on the bar exam!"
)
);
}
Please help me for the same.
Thank you,
Twilio developer evangelist here.
I think you may need to use the fully qualified namespace for the Client in this case. Try:
$client = new Twilio\Rest\Client($sid, $token);
Let me know if that helps.
Edit
OK, that didn't work. After reading around, I've found that it's not recommended to use require or require_once within a function. I'd recommend you require the autoload file outside of your function, use the namespace and then call the Client inside the function. Like this:
require_once( get_stylesheet_directory_uri(). '/twilio-php-master/Twilio/autoload.php');
use Twilio\Rest\Client;
function wl8OrderPlacedTriggerSomething($order_id){
$sid = 'xxxxxxxxxxxxxxxxxxxxxx';
$token = 'xxxxxxxxxxxxxxxx';
$client = new Client($sid, $token);
// and so on...
}
make sure that autoload.php and Client.php files are getting loaded properly.
its unable to load client call
Related
I have a react-native app running on expo with a login screen on my react-native application. When the user logs in, a JWT token is retrieved from Firebase and passed on to a which opens up a Wordpress Site. What I am trying to do is to automatically pass this JWT token to the and log them into the Wordpress site.
I tried several options, like installing the Firebase Authentication plugin by miniOrange, but I didn't know how to make this happen using the free version. I also tried using a custom script using the Code Snippets plugin and entering the below code. I had already installed php-jwt on my root folder in wordpress using compose.
require __DIR__ . '/vendor/autoload.php';
use \Firebase\JWT\JWT;
add_action('init', 'handle_jwt_token');
function handle_jwt_token() {
if (!empty($_GET['jwt_token'])) {
$jwt_token = sanitize_text_field($_GET['jwt_token']);
// Decode and verify the JWT token
$jwt_decoded = JWT::decode($jwt_token, YOUR_FIREBASE_PROJECT_ID, array('HS256'));
// Get the user data from the JWT token
$user_id = $jwt_decoded->sub;
$user_email = $jwt_decoded->email;
$user_name = $jwt_decoded->name;
// Log the user into Wordpress
$user = get_user_by('email', $user_email);
if (!$user) {
// Create a new Wordpress user if they don't already exist
$user_id = wp_create_user($user_email, wp_generate_password(), $user_email);
wp_update_user(array(
'ID' => $user_id,
'display_name' => $user_name,
));
}
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID);
wp_redirect("https://homepage_url.com");
exit;
}
}
I just ended up getting an error
Uncaught Error: Failed opening required '/var/www/vhosts/test.com/html/wp-content/plugins/code-snippets/php/admin-menus/vendor/autoload.php' (include_path='.:') in /var/www/vhosts/test.com/html/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(253)
My question is, are there any options available for me to achieve this? Thank you for your time in reading this!
You will able to achieve it with this extension.
https://firebase-wordpress-docs.readthedocs.io/en/latest/extensions/jwt.html#remote-authentication-via-url
It will require you generate a token on your React Native and pass it to the WebView, after that WordPress can handle the auto login.
You can try it here: https://dn-wp-autologin.web.app/
I created a module and implemented my Telegram bot code in /src/Controller/BotController.php. Now I want to set a webhook and use https://api.telegram.org/bot<token>/setwebhook?url=https://<my-site>/<my-module-path-in-"name.routing.yml">.
The webhook is set, but my code doesn't work and gives me a 500 error.
How can I fix this?
EDIT:
My code:
<?php
namespace Drupal\telegram\Controller;
use Drupal\Core\Controller\ControllerBase;
class TelegramController extends ControllerBase {
public function telegram() {
$update = file_get_contents("php://input");
$update = json_decode($update, TRUE);
$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
if (isset($chatId) and isset($message)) {
sendMessage($chatId, $message);
$url = "https://api.telegram.org/bot504387877:AAFHnQe-AdscpSZN42yY-JYem5jwdJc131Q/sendMessage?chat_id=" . $chatId . "&text=" . $message;
}
file_get_contents($url);
$build['#theme'] = 'new';
return $build;
}
}
I think it may cause by unescaped URL, but you need to provide error message to determine.
If you just want to make it done, you can use this Android application. For instance, use getWebhookInfo to obtain error reason.
I am getting an error in a PHP script I am building:
Fatal error: Class 'ical' not found in /home/abc/public_html/app/mods/googleCalendar_3.0/cache_events.php on line 74
Here is a snippet from my script file:
define('CLIENT_ID', 'ASDLJJLDSJLASDJLajdl;jdsljkASD;LKJASDLKJASD.apps.googleusercontent.com');
require_once('autoload.php'); // 2014-11-24 part of /usr/local/lib/php/google-api-php-client
require_once('/usr/local/lib/php/google-api-php-client/src/Google/Client.php'); // 2014-11-25
require_once('/usr/local/lib/php/google-api-php-client/src/Google/Service/Calendar.php'); // 2014-11-25
$ical = new ical('https://www.google.com/calendar/ical/CLIENT-ID/public/basic.ics');
$eventListArray = array_filter($ical -> events(), "locationfilter");
$eventCount = count($eventListArray);
print_r($eventListArray); echo "<br>";
echo "Event Count:" . $eventCount;echo "<br>";
exit;
I am simply trying to retrieve all events in my public calendar
Notes:
Calendar is publicly viewable
Just to make sure, I added my Auth & API's > Credentials > Service Account > Email Address to it just to be safe
If you want to use a service account your code is off quite a bit. I cant test this code my local webserver is acting up but it should be close you may have to tweek the $service->Events->list(); part it was kind of a guess. Make sure that you have the Service account email address added as a user on the calendar in question and it should work.
session_start();
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
/************************************************
The following 3 values an befound in the setting
for the application you created on Google
Developers console. Developers console.
The Key file should be placed in a location
that is not accessable from the web. outside of
web root. web root.
In order to access your GA account you must
Add the Email address as a user at the
ACCOUNT Level in the GA admin.
************************************************/
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com';
$Email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp#developer.gserviceaccount.com';
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);
// seproate additional scopes with a comma
$scopes ="https://www.googleapis.com/auth/calendar";
$cred = new Google_Auth_AssertionCredentials(
$Email_address,
array($scopes),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Calendar($client);
// you should only need to do this once print this and you will
// find the calendarId for the one you are looking for.
$calendars = $service->calendarList->list();
$events = $service->events->list($yourCalendarID);
Note: all you need is the Google Dir you can remove everything above that you dont really need it. Code was edited from the only tutorial i have that shows this in PHP.
I'm in need of help for a custom form in which emails are not being sent.
Context: Within Drupal, I have installed the following modules: PHPMailer, SMTP Authentication Support, Mail System and Mime Mail.
Configuring the above modules you have the option to test your configurations and when preforming such tests emails are being sent properly. However, when writing a module for a form, emails are not being sent.
I don't get any type of erros nor message. I just don't get the email.
Here is the snipped of code that I'm using:
function application_form_submit($form, &$form_state) {
$subject = "testing web form";
$body = array();
$body[] = "Mail body";
$send = FALSE;
$mail_message = drupal_mail('application', 'apply-jobs', 'email#gmail.com', language_default(), $params = array(), $from = 'user#test.com', $send);
$mail_message['subject'] = $subject;
$mail_message['body'] = $body;
$mail_system = drupal_mail_system('application', 'apply-jobs');
$mail_message = $mail_system->format($mail_message);
$mail_message['result'] = $mail_system->mail($mail_message);
}
Suggestions?
You've got an odd way of defining optional parameters. This bit:
$from = 'user#test.com'
will evaluate to... nothing
Try changing your drupal_mail() call like this:
$mail_message = drupal_mail('application', 'apply-jobs', 'email#gmail.com', language_default(), array(), 'user#test.com', $send);
I found the solution to my question. The solution is:
The Mail System module allows one to Configure Mail System settings per module, which means that I had to create new mail system for my customized module an indicate the mail system that I want to use. After I did this, all my email are being sent without any problems.
Hope this helps someone, as there is very little information about this.
Thank you all.
I'm getting an error (INVALID_SESSION_ID) when trying to send an authenticated GET request to Salesforce.com.
Here is the plug-in in its entirety, which basically just outputs the body of the REST response to whatever page has the [MembershipTables] shortcode:
if (!class_exists('WP_Http')) {
include_once(ABSPATH . WPINC . '/class-http.php');
}
// This is obviously the real username
$username = 'xxxx#xxxx.xxx';
// And this is obviously the real password concatonated with the security token
$password = 'xxxxxxxxxxxxxx';
function getMembershipTables() {
$api_url = 'https://na15.salesforce.com/services/apexrest/directory';
$headers = array('Authorization' => 'Basic ' . base64_encode("$username:$password"));
$args = array('headers' => $headers);
$request = new WP_Http;
$result = $request->request($api_url, $args);
$body = $result['body'];
echo "$body";
}
add_shortcode( 'MembershipTables', 'getMembershipTables' );
I should note that I can successfully hit this endpoint with Curl, though I use a session token I get from Salesforce using the old SOAP API to keep it equivalent (i.e., no client id/secret).
Am I doing something wrong with WP_Http? Or cannot I not authenticate a salesforce.com request using basic auth?
Thanks.
The salesforce API does not support Basic authentication, you need to call it with a sessionId. You can obtain a sessionId by various methods include interactive & programatic OAuth2 flows, and via a Soap login call.
Basis Interactive had a similar problem to solve. When I worked on the project I opted to to call the SalesForce CRM via the preset form plugin and a custom JS Cookie PHP Wordpress Plugin. We had this problem easily resolved by developing custom calls to SalesForce CRM via a getRequest in PHP passing data to the SalesForce CRM.
Test Site in Use:
http://newtest.medullan.com/wp/?page_id=3089
Here is the code and recycle the logical queries
Download Link:
http://basisinteractive.net/webdesign.html#wordpress