Google Calendar API PHP Installation - php

I have been trying to get the Google Calendar API working with PHP, I even contacted my host (1and1) to figure things out, nothing is working
I've followed the examples on the PHP Quickstart, tried to run it just by going to the page and it gave me an error to run it in command line. So I logged into my SSH, navigated to the file and ran the command php5.4 CRON-temp.php. Now, I get the same error:
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'This application must be
run on the command line.' in /homepages/41/d159654153/htdocs/mysite
/admin/php/CRON-temp.php:16
Stack trace:
#0 {main}
thrown in <b>/homepages/41/d159654153/htdocs/mysite/
admin/php/CRON-temp.php</b> on line <b>16</b><br />
So, I've run it in the command line, but it still tells me to run it there. The code throwing the error comes from this:
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
I've been held up here... I've sent the code to a friend to try out and he gets a link to return where I'm supposed to go authorize this, but I'm not able to see any of that.
The tech person at 1and1 said
Now, if you will look at the error, the error changes from "parse error" to "Uncaught exception 'Exception". It appears that this application will run in server environment and not on a shared hosting. This is a limitation on the package that you have. You might need to go to a server package for this API to run.
Is this really the case? Do I really need to switch over to server hosting? Is there a way around this?
All I want to do is have a 2 way sync between Google Calendar and a mySQL server.

According to the Google code and instructions i.e. https://developers.google.com/google-apps/calendar/quickstart/php
first you try to run this using cmd.
Go to start type cmd and then black screen will appear type here as:
php yourfilename.php, then it will provide you a url and ask for verification code you just visit the said url and allow the Google and get key and enter the said key in cmd.
Your code is ready to use. Now comment the same code which is restricting for cli i.e:
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}

I found your post from a project that I'm currently working on. You can run quickstart.php from the command line which then gives you a link to paste into your browser to authorize access.
Once you have authorized access, you can run the script in the command line again and it displays upcoming events.
There is no browser output - that is what I am looking for.
I hope this helps.

Related

Symfony messenger can't consume messages

Few weeks ago I set up a Message system with Symfony Messenger and it worked great.
Today I wanted to create new object through message, so I went to my server and type the command to consume message
First I had this result
$ bin/console messenger:consume-messages amqp_notifications
/usr/bin/env: ‘php\r’: No such file or directory
It never happened before with my files, and I never changed the line ending or encoding of my file sin PHPstorm.
I tried to use $ php bin/console messenger:consume-messages amqp_notifications
but then I had this error.
Attempted to load class "AMQPConnection" from the global namespace.
Did you forget a "use" statement?
Pretty weird, because I have have the php-amqp ext installed as you can see on the screenshot of my phpinfo
I didn't change anything in my Message class or Handler.
Also, I tried to call new AMQPConnection() on a random action, just to try, and I didn't get the error.
I'm completely lost with this error this time, as everything is installed.
I use PHP 7.3.1 and symfony Messenger 4.2.2
It seems your second issue was already solved by ccKep on his comment.
The first one is that the specific shebang line #!/usr/bin/env php executes the first php found in the $PATH. So if you already have uninstalled it, which seems the case, or it has a symbolic link to another php version, you can get a wrong result.
Tries to check what is inside the $PATH and replace the PHP path for the correct one. You might get the place running which php.

`Invalid grant` error when cron runs website backup google drive api script

I am trying to take backup of my website`s files and database and upload it to google drive from my website via Google drive Api.
Followed instructions as per these links - backup files to google drive using php
https://www.matthewhipkin.co.uk/codelib/automatically-backing-up-your-web-server-files-to-googledrive-with-php/ and did the following -
downloaded the backupgoogledrive folder from https://github.com/hippy2094/backuptogoogledrive and uploaded that in my public_html/backup folder and renamed backuptogoogle.php as backup/index.php
I created the client ID, client secret and an authCode and put those in the setting.inc along with my MySQL username, password and database name. $homedir was put as /home/myuser/ and $sitedir was set to public_html/
Then, I ran in SSH - php /home/user/backup/index.php
This ran fine generating a json file and backup for files and database also was found in google drive.
Then, I set up cron job as follows -
5 0 * * 6 /usr/local/bin/php /home/myusername/public_html/backup/index.php
This should run the script at 00:05 on a saturday but it did NOT run or backup to google drive.
After that, I changed the cron to run every 15 minutes. I noticed that the files and database backups are going to home/myuser folder instead of google drive and the error_log in that folder is giving this error every 15 minutes -
Fatal error: Uncaught exception 'Google_AuthException' with message
'Error fetching OAuth2 access token, message: 'invalid_grant'' in
/home/myuser/public_html/backup/google-api-php-client/src/auth/Google_OAuth2.php:115
Stack trace:
#0
/home/myuser/public_html/backup/google-api-php-client/src/Google_Client.php(127):
Google_OAuth2->authenticate(Array, '4/SCxgUoxjnbe_0...')
#1
/home/myuser/public_html/backup/index.php(38):
Google_Client->authenticate('4/SCxgUoxjnbe_0...')
#2 {main} thrown in
/home/myuser/public_html/backup/google-api-php-client/src/auth/Google_OAuth2.php
on line 115`
Please note while registering at Google API at https://console.developers.google.com/apis I selected Other option in the Credentials Page and not Web Application because my aim is only to take backup for my site. Also, when I selected the Web Application option and generated the client ID and secret and tried to generate the authCode via url generated in SSH, it would give an error with the SSH generated url. So, the solutions for invalid grant which are mentioned in other posts do not work for me as I do not get option for setting call back url.
Please tell what I am doing wrong. Why I am able to run the script successfully manually and not via cron ? When I am running the script manually running in SSH. Only problem happens when cron runs.
Thanks in advance.
Found the solutions as follows -
Put in the file index.php this code -
chdir('/home/user/backup');
Then the cron backed - up the file to google drive properly. So, the problem was with the cron path.

Twig_Error_Loader exception caught when looking for a repository

I have to work on a project that my internship supervisor made on windows. He's making the backend and I'm making the frontend, but I need the backend to deal with the frontend of project.
I'm using XAMPP, and my Document Root is in /home/remy/www.
When I put in the URL localhost/Salto, it finds it rather easily. However, when I click on the web link that it shows to me, I get this error:
Fatal error: Uncaught exception 'Twig_Error_Loader' with message 'The
"/home/remy/www/Salto\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle/Resources/views"
directory does not exist
("/home/remy/www/Salto\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle/Resources/views").
Some other things that are in the filesystem.php in the core and where the error is :
/home/remy/www/Salto/vendor/twig/twig/lib/Twig/Loader/Filesystem.php on line 101.
I was asking myself if it could be with the \ in Windows (OS of my internship supervisor) and the fact that I'm working on Linux, which prevents me from go into the depths of the back-end.

Codeigniter Command line error - PHP Fatal error: Class 'CI_Controller' not found

After following the user guide instructions found here: http://ellislab.com/codeigniter/user-guide/general/cli.html I'm unable to run the test script via command line.
My controller located at /var/www/mysite/application/controllers/
class Tools extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
In my browser I can access
http://mysite/tools/message/ben
And the function correctly outputs "Hello ben"
From terminal I should be able to run:
$ php index.php tools message "Ben"
My terminal should print: "Hello Ben"
However I get the following error:
PHP Fatal error: Class 'CI_Controller' not found in /var/www/mysite/system/core/CodeIgniter.php on line 233
My server is pretty standard; ubuntu LAMP. Codeigniter is pretty standard too and I have no problem running non CI scripts via command line
My PHP binary is only located in /usr/bin/php <-- This post suggests an issue running CI directly from usr/bin/php, however I'm not operating a shared PHP service, and I don't see why this would make a difference to how PHP executes a CI script.
Any help or just an indication on how to debug this would be greatly appreciated.
Thanks in advance.
Solved! (partly) the issue was CodeIgniters error logging.
In application/config/config.php, I modified the following config property:
$config['log_threshold'] = 0;
This disables logging, and allows $ php index.php to execute.
If anyone can explain why CI only shows this error on CLI PHP - might help anyone else who has this issue and needs it resolved with error logging on.
To solve error "Class 'CI_Controller' not found" try going to Application -> Config -> database.php then check the database details like hostname, username, password and database.
To Mijahn:
I had this same problem, and after about two hours of tracing through code to figure out the problem, it seems that there is some sort of conflict with loading the CI_Controller when utilizing the native PHP load_class function.
I worked around this issue by making the following changes to the Common.php file (hack, I know).
//$_log =& load_class('Log');
require_once('system/libraries/Log.php');
$_log = new CI_Log();
My logs then where created exactly like I wanted. Hope this hack helps.
This site says to run codeigniter from the command line, one must set the $_SERVER['PATH_INFO'] variable.
$_SERVER['PATH_INFO'] is usually supplied by php when a web request is made. However, since we are calling this script from the command line, we need to emulate this small part of the environment as a web request.
The answer provided in this Stack Overflow post worked for me.
Within system/core/CodeIgniter.php, on around line 75, change:
set_error_handler('_exception_handler');
to...
set_exception_handler('_exception_handler');
Other users have reported that this gave them a better backtrace with which to debug the underlying issue, but for me, this actually removed the problem altogether.

How can I give PHP access to a COM object?

I am running PHP 5.3 on my local Windows 7 Laptop (have tried this all on our development server with no success, so I tried to see if I could get it to work on my laptop successfully first). When I call the following script:
$objAltovaXML = new COM('AltovaXML.Application');
I get the error:
Uncaught exception 'com_exception' with message 'Failed to create COM object `AltovaXML.Application': Access is denied.
I have checked using a PowerShell script I have found elsewhere on StackOverflow and have confirmed tht AltovaXML.Application is properly registered.
But how do I give my PHP script access to it?
While it should work I would just wrap a script around COM object and invoke that script via exec. Saves you a lot of trouble and is easy to debug separately and understand what exactly is going on.

Categories