Context: I am following the instructions provided in the Twilio iOS Quickstart to test out the Chat function. A simple PHP server is required to generate tokens.
Inside token.php, these are the lines to generate the token:
$token = new AccessToken(
$TWILIO_ACCOUNT_SID,
$TWILIO_API_KEY,
$TWILIO_API_SECRET,
3600,
$identity
);
The instructions only asked me to modify config.php, which I have:
<?php
// Required for all uses
$TWILIO_ACCOUNT_SID = 'actual_SID';
$TWILIO_API_KEY = 'actual_API_key';
$TWILIO_API_SECRET = 'actual_API_secret';
// Required for Video
$TWILIO_CONFIGURATION_SID = '';
// Required for Chat
$TWILIO_CHAT_SERVICE_SID = 'actual_Chat_Service_SID';
// Required for Notify
$TWILIO_NOTIFICATION_SERVICE_SID = '';
// Required for Sync
$TWILIO_SYNC_SERVICE_SID = '';
As they have instructed, I run the server with php -S localhost:8000 -t webroot and get the following error:
/token.php?device=browser - Class 'Twilio\Jwt\AccessToken' not found in /Users/me/Downloads/sdk-starter-php-master/webroot/token.php on line 19
Twilio developer evangelist here.
Before running the application with php -S localhost:8000 -t webroot make sure you have installed the dependencies using composer:
composer install
Then you should be ready to run.
Related
I successfully implemented Google's sign-in feature which is running quite nicely on my local machine (Mac, PHP, MySQL) following these instructions. I posted how I implemented this here.
I have recently uploaded the work-in-progress site to a container in a Google Cloud Run environment but this sign-in feature is no longer working. The error log shows: PHP Fatal error: Uncaught Error: Failed opening required '/var/www/html/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /var/www/html/includes/oauth.php:5.
I've take this to mean that the /vendor/... dependencies are either in a different location, or not there at all (I exclude these files from the deployment, though I do include the composer.json file which requires the google/apiclient) and I can't figure out if there is a simple configuration I need to perform to tell the Cloud Run service about the dependency.
I'm getting very lost in the Google documentation. One track suggests that I should abandon this method and move to Google Identity Services Library which would be quite frustrating as I spent quite some effort getting the current method working correctly.
The javascript components work well and produce a pop-up to allow the user to select their Google ID and log in. The callback function works too as the oauth.php file is executed on the server and it outputs to the console the sessionId. It's just the require_once line that's causing the failure.
// oauth.php
<?php
session_start();
error_log("oauth.php SessionId: " . session_id(), 0);
include 'connect.php';
require_once __DIR__ . '/vendor/autoload.php';
$jwt = new \Firebase\JWT\JWT; //Allow for discrepancies between server and auth times
$jwt::$leeway = 100;
$CLIENT_ID = "XXX";
$client = new Google_Client(['client_id' => $CLIENT_ID]); // Specify the CLIENT_ID of the app that accesses the backend
$client->setRedirectUri('postmessage');
$client->addScope("email");
$client->addScope("profile");
if (isset($_POST['idtoken'])){
$id_token = $_POST['idtoken'];
// Code continues to validate and process the user data
}
Has anyone experienced this challenge and found a resource to help with migrating a google sign-in service from a standalone apache-based deployment to Google Cloud Run?
My Dockerfile (which doesn't contain any information about composer. I'm now reading that I need to perform a composer install either within this Dockerfile or within docker-compose):
# Use the official PHP image.
# https://hub.docker.com/_/php
FROM php:8.0-apache
# Configure PHP for Cloud Run.
# Precompile PHP code with opcache.
RUN docker-php-ext-install -j "$(nproc)" opcache
RUN docker-php-ext-install -j "$(nproc)" mysqli
RUN set -ex; \
{ \
echo "; Cloud Run enforces memory & timeouts"; \
echo "memory_limit = -1"; \
echo "max_execution_time = 0"; \
echo "; File upload at Cloud Run network limit"; \
echo "upload_max_filesize = 32M"; \
echo "post_max_size = 32M"; \
echo "; Configure Opcache for Containers"; \
echo "opcache.enable = On"; \
echo "opcache.validate_timestamps = Off"; \
echo "; Configure Opcache Memory (Application-specific)"; \
echo "opcache.memory_consumption = 32"; \
} > "$PHP_INI_DIR/conf.d/cloud-run.ini"
# Copy in custom code from the host machine.
WORKDIR /var/www/html
COPY . ./
# Use the PORT environment variable in Apache configuration files.
# https://cloud.google.com/run/docs/reference/container-contract#port
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
# Configure PHP for development.
# Switch to the production php.ini for production operations.
# RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# https://github.com/docker-library/docs/blob/master/php/README.md#configuration
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
composer.json:
{
"require": {
"google/apiclient": "^2.11"
}
}
I added the following to the end of my Dockerfile. I added the USER line as I was getting a "An error was encountered loading IAM roles associated with the service acount[sic]." error. This user has the following roles: Cloud Run Admin, Secret Manager, Service Accounts, Cloud Build. I'm still getting build failures, probably because I'm not using the user correctly. I read that composer wants to install as root which causes problems during a cloud build:
FROM composer as builder
WORKDIR /app/
USER XXX#cloudbuild.gserviceaccount.com
COPY composer.* ./
RUN composer install
COPY --from=builder /app/vendor /var/www/html/vendor
I've installed mjml cli using the following command (as described in the mjml documentation):
npm install mjml --save
now if i did node_modules/.bin/mjml in the command line it will run successfully.
the problem is when i use the symfony process component in php i got the following error (even if it's the right path):
The command "/Users/qoraiche/Documents/my-app/node_modules/.bin/mjml" failed. Exit Code: 127(Command not found) Working directory: /Users/qoraicheOS/Documents/my-app/public Output: ================ Error Output: ================ env: node: No such file or directory
Symfony process code:
$process = new Process(base_path('node_modules/.bin/mjml'));
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
By the way i have installed mjml globally as well and try it with no luck.
Try providing the full path to node.js when calling the MJML binary from a Symfony Process().
$process = new Process('/your/path/to/node ' . base_path('node_modules/.bin/mjml'));
I use this method in my own apps. Of course, you will need to provide the input and output files to actually transpile anything.
I am trying to access a website with selenium and i am getting below error
And i used the following code i have tried header('Access-Control-Allow-Origin: *'); but did't work for me
require_once "phpwebdriver/WebDriver.php";
$webdriver = new WebDriver("localhost",4444);
//$ffprofile = $webdriver->prepareBrowserProfile("");
$webdriver->connect("chrome");
$webdriver->get("https://healofy.com/"); sleep(3);
$element=$webdriver->findElementBy(LocatorStrategy::id,"Baby_1_2_years");
if($element) {
print_r($element);
$element->click();
}
It could be you're using old php webdriver client (2013) ? which is not compatible with current selenium and browser.
use updated PHP selenium facebook/webdriver and here the setup step:
# if you have composer
composer require facebook/webdriver
# if not download composer.phar
curl -sS https://getcomposer.org/installer | php
php composer.phar require facebook/webdriver
read the github page above if it missing something.
and PHP code
<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('vendor/autoload.php');
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
$driver->get("https://healofy.com/");
$driver->findElement(WebDriverBy::xpath('//label[#for="Baby_1_2_years"]'))->click();
//$driver->quit();
?>
If you use Selenium webdriver and want to get image from page, but blocked by cors.
You can just say Selenium to go to image url and after that take screenshot.
And your image will be saved!
And cors will not be a problem.
Here is example code in PHP:
$img_url = '.../6028384213.jpg';
$driver->get($img_url);
$driver->takeScreenshot('img.png');
Your image will be opened by browser and after that saved to your local computer!
I'm using PHP to try using the Google Cloud Spanner. I already did the gCloud settings and everything, and that's right. Now I need to make the connection via PHP to do a CRUD with the database that is in Spanner, but the code below always returns the error:
PHP Fatal error: Undefined constant 'Grpc\STATUS_UNKNOWN' in
/xxx/xxxx/www/vendor/google/cloud-spanner/Connection/Grpc.php on line
129
The code I have is:
<?php
require 'vendor/autoload.php';
use Google\Cloud\Spanner\SpannerClient;
/* Error start here */
$spanner = new SpannerClient([
'projectId' => 'my-project-id'
]);
$db = $spanner->connect('instance', 'database');
$userQuery = $db->execute('SELECT * FROM usuario WHERE login = #login', [
'parameters' => [
'login' => 'devteam'
]
]);
$user = $userQuery->rows()->current();
echo 'Hello ' . $user['login'];
The requirements I use in the composer are:
"require": {
"google/cloud": "^0.32.1",
"google/cloud-spanner": "^0.2.2"
}
I noticed that if I enter through the browser, the error presented above continues to appear. If I run the command php teste.php on the terminal, it runs the script correctly, ie, the terminal works and the browser does not.
Google Cloud PHP's spanner client is gRPC only. This means to use it you will need to install the gRPC PHP extension:
pecl install grpc
Once you have done that, add google/proto-client-php and google/gax to your composer.json and run composer update. After this is done, the error will be resolved.
For those wanting more detailed instructions, see this page for installing and enabling gRPC for PHP!
Since you mentioned that it works on CLI but not on browser, I can say that you need to enable the grpc extension on your php web server config.
E.g. Add
extension=grpc.so to your /etc/php/5.6/apache2/php.ini
I have a webapp running on Azure App Service. I am trying to test a Post deployment script in Kudu Debug CMD console.
php -d extension=php_redis.dll -f postdeploy.php
Here is my postdeploy.php file
<?php
ini_set('error_reporting', -1);
ini_set("display_errors", 1);
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');
$redis = new Redis();
...
?>
I get the following error
Fatal error: Class 'Redis' not found in D:\home\site\deployments\tools\PostDeploymentActions\postdeploy.php on line 13
PHP Warning: PHP Startup: Unable to load dynamic library 'D:\Program Files (x86)\PHP\v5.6\ext\php_redis.dll' - The specified module could not be found.
in Unknown on line 0
I uploaded the proper version of php_redis.dll to the PostDeploymentActions folder. This is the same binary that the webapp is using via the app setting in portal PHP_EXTENSIONS = bin\php_redis.dll. However, I am not sure how to load this for this script.
Is there a way that I can load the php_redis.dll in Kudu post deployment script?
I tried php -d extension=./php_redis.dll -f postdeploy.php, php -d extension=%~dp0php_redis.dll -f postdeploy.php and other weird combinations with no luck.
Please try to add the PHP extension via ini settings, refer to https://azure.microsoft.com/en-us/documentation/articles/web-sites-php-configure/#how-to-enable-extensions-in-the-default-php-runtime at the Configure via ini settings section.
Generally:
Add an App Setting to your Web App with the key PHP_INI_SCAN_DIR and value d:\home\site\ini
Create an configuration file in d:\home\site\ini called extensions.ini
Add configuration settings to the extensions.ini file using the same
syntax you would use in a php.ini file. For example: extension =
php_redis.dll.
Restart Web Apps service.
Via this approach, you can configure the PHP extension into Kudu console site's PHP runtime. And configure via the App Settings will only configure the extension into IIS.
Any further concern, please feel free to let me know.