installing Google Client Library for Google Analtics PHP - php

first of all I am new to this topic, so I hope my question is not too stupid.
I want my website to have PHP access to Google Analytics metrics. I followed every step of this description from google. Unfortunately when I upload everything on my server and try to run the test-site, I always get the following error-message:
Fatal error: Uncaught exception 'Exception' with message 'This library
must be installed via composer or by downloading the full package. See
the instructions at
https://github.com/google/google-api-php-client#installation.' in
/home/users/myftp/dev.mywebsite.com/dashboard/google-api-php-client-master/src/Google/autoload.php:14
Stack trace: #0
/home/users/myftp/dev.mywebsite.com/dashboard/HelloAnalytics.php(8):
require_once() #1
/home/users/myftp/dev.mywebsite.com/dashboard/HelloAnalytics.php(104):
getService() #2 {main} thrown in
/home/users/myftp/dev.mywebsite.com/dashboard/google-api-php-client-master/src/Google/autoload.php
on line 14
So apparently there is something wrong with the embedding of the Google client library. In the error message it says I have to use Composer, but in the GitHub documentation they say, manual download would be fine as well. I think in the end this shouldn't make any difference? I am not familiar with composer or GitHub, this is why i downloaded it manually.
I uploaded it on the server and put it into the same directory like the HelloAnalytics.php. I address it in HelloAnalytics.php via
require_once 'google-api-php-client-master/src/Google/autoload.php'

From the looks of it you are using the wrong autoloader.
src/Google/autoload.php looks for the composer autoloader and if it doesn't exist it throws the exception you are getting. Since you didn't install using Composer it is not found and that exception is thrown.
If you clone the repository using git you will have the correct SPL autoloader. If you download the package it uses the composer autoloader.
Try downloading using:
git clone -b v1-master https://github.com/google/google-api-php-client.git
Or switch to the v1-master branch and use this autoloader instead. You will see that file differs between the master branch and the v1-master branch.

to avoid AUTOLOADer error, install MASTER-V1 version:
https://github.com/google/google-api-php-client/tree/v1-master
p.s. if you will get other error, then ensure that you have correctly included the "SERVICE EMAIL" (that is like: xxxxxx#analytics-xxxxx.iam.gserviceaccount.com ).

Related

is this the right php header for pdftk

I'm writing a very basic catenation script and lifted this straight off the pdftk 0.10.0 packagist site:
use mikehaertl\pdftk\Pdf;
// Extract pages 1-2 into a new file
$pdf = new Pdf('sourcefile.pdf');
$result = $pdf->cat(1, 2)
->saveAs('newfile.pdf');
if ($result === false) {
$error = $pdf->getError();
}
But I'm getting this error:
Fatal error: Uncaught Error: Class "mikehaertl\pdftk\Pdf" not found in C:\PHP8\index.php:7 Stack trace: #0 {main} thrown in C:\PHP8\index.php on line 7
All the sample scripts I've seen have that same header. I'm sure it's something really basic that I'm not seeing here.
Since the error is showing the Class not found, it would make me initially think that the package wasn’t installed in your project. If you run composer require mikehaertl/php-pdftk, does that get you rolling?
If you’re not familiar with Composer, here’s a quick guide to getting started: https://packagist.org/
Apparently, when installed via composer, the pdftk.exe file is not downloaded for some reason. I downloaded it manually from the github page and referred to it in the header.
Ok, finally found the answer. You need to install pdftk beforehand as packagist does not automatically pull it for you.

Symfony Process error when running composer Laravel 8

suddenly in my project with laravel 8 I ran composer to uninstall a dependency that I want to reinstall for a sense nothing more than order and start from scratch and I started to throw this error that has to do with Symfony Process:
PHP Fatal error: Uncaught TypeError: fclose (): Argument # 1 ($ stream) must be of type resource, bool given in phar: // C: /ProgramData/ComposerSetup/bin/composer.phar/vendor/symfony/process/ Pipes / WindowsPipes.php: 71
What can be the mistake? I read something like that has to do with the update of the symfony Process but I don't know why. The only thing I did was install Laravel / Passport for the use of token in the user login.
Whilst running PHP 8 on Windows I too had this error. I tried to clear out the temp directory manually per Composer loading from cache - when that didn't work. I also found that composer's symfony usage had locked a temp file in an odd way.
I needed to clear the temp file, and I used filelocker to unlock it, easily enough. Once I had unlocked and deleted the file, I was able to run composer as expected again.
Here is a related stack overflow question about the temp file location: Composer install: error on temporary file (%USERPROFILE%\AppData\Local\Temp works for me)
They have names like 'sf_proc_00.err'. I found them easily by sorting the temp files by date, and only trying to remove the ones modified today.
A reboot, or identifying the symfony process tying up the temp file would also work. According to file locked - it was an instance of mingw git for me.

PHP error calling a function from a package

I am new to php and have just installed my first package via composer. I'm now trying to call a function from the package I installed as follows:
<?php
require_once 'vendor/autoload.php';
$value = 1;
$aws = AmazonGiftCode::make()->buyGiftCard($value);
echo $aws;
?>
But I get the following error:
PHP Fatal error: Uncaught Error: Class 'AmazonGiftCode' not found in
/public_html/php/test.php:4 Stack trace:
#0 {main} thrown in /public_html/php/test.php on line 4
Based on my (albeit limited) experience with other languages, I'm guessing I have to load the package that contains the class first. The package folder is in the same directory as the test.php file, in the subfolder vendor/kamerk22/AmazonGiftCode/. But I think this is where I don't know enough to troubleshoot it based on the information I could find.
Your directory structure should look like this.
test.php
composer.json
vendor
└───autoload.php
└───kamerk22
└───AmazonGiftCode
Make sure you installed the package using composer, and not by downloading it.
I'm guessing I have to load the package that contains the class first.
Unlike what you would normally expect when importing stuff, when using composer you only have to import the autoload.php file, and composer will take care of loading other packages as needed. By as needed what I mean is that as soon as composer sees you use the AmazonGiftCode class it will import the AmazonGiftCode package, but if one of your REST endpoints doesn't use anything from the AmazonGiftCode package it won't ever load it. This allows you to not have to worry about slowing down the entire application when you want to use a composer package for only a few endpoints. At least that's the way I understand how composer works.
Just run composer dump-autoload once and the class should known then.
You could also get verbose and use kamerk22\AmazonGiftCode\AmazonGiftCode;
But that AmazonGiftCode looks quite Laravel specific... that's why it may still fail, even if it may be found by auto-load. One needs to setup Laravel framework in the first place; just see this query (just in case if you may wonder where all these missing classes may come from).

Guzzle can’t find its own files

I want to run Guzzle on Xampp server, but when I try to run it, I get message:
Fatal error: Class 'Guzzle\Http\Exception\BadResponseException' not found in D:\2 Workspace\5 Aptana\OpenTok\Guzzle\Http\Exception\ClientErrorResponseException.php on line 8
I need it as a part of PHP API for OpenTok project. I can't use composer, so I downloaded it manually, but it doesn't work. I think it can be something with namespace and use.
What should I do to get this working?

How can I use the newly revamped Dropbox-PHP API?

I would like to use the Dropbox-PHP API that has recently come under development again. It is located here: http://code.google.com/p/dropbox-php/
I did cloned it with hg clone https://dropbox-php.googlecode.com/hg/ dropbox-php and I get this file structure:
Dropbox/API.php
Dropbox/autoload.php
Dropbox/Exception/Forbidden.php
Dropbox/Exception/NotFound.php
Dropbox/Exception/OverQuota.php
Dropbox/Exception/RequestToken.php
Dropbox/Exception.php
Dropbox/OAuth/PEAR.php
Dropbox/OAuth/PHP.php
Dropbox/OAuth/Zend.php
Dropbox/OAuth.php
examples/accountinfo.php
examples/createaccount.php
examples/download_image.php
examples/getmetadata.php
examples/oauth_workflow.php
examples/uploading.php
But I get this error when trying to run accountinfo.php (or example):
Warning: include(Dropbox/autoload.php) [function.include]: failed to open stream
No such file or directory in dropbox-api/examples/accountinfo.php on line 7
Right, so then I move the Dropbox folder inside of where all the example files are and still get an error message:
Fatal error: Uncaught exception 'Dropbox_Exception' with message 'The OAuth class
could not be found! Did you install and enable the oauth extension?' in
examples/Dropbox/OAuth/PHP.php:36 Stack trace: #0 examples/accountinfo.php(9):
Dropbox_OAuth_PHP->__construct('', '') #1 {main} thrown in
examples/Dropbox/OAuth/PHP.php on line 36
So I'm obviously not doing something right but I have no idea what.
Also saw on the site where it has instructions on installing:
pear channel-discover pear.dropbox-php.com
pear install dropbox-php/Dropbox-alpha
I ran those two commands and it still won't work. I don't usually have any problems coding in PHP but the lack of documentation is a little frustrating.
Update
As noted in the accepted answer below my main problem was not having oAuth installed on the system. I'm running OS X 10.6 - if someone can provide some clear and easy instructions on how to build / install this to work with XAMPP / PHP 5.3 I will accept your answer. I've tried the articles online about using homebrew and such but these are flaky and do not seem to work for me. Guessing I will have to build / install it from scratch.
The Dropbox folder needs to be inside one of the folders in your include_path.
Edit:
Also oauth needs to be "installed" on the system and included in php.ini (when you do phpinfo() oAuth should show up as a module). then things should work.

Categories