I want to start by saying I have searched extensively, including google, connect.squareup.com, and Stack Overflow and cannot find an answer to my question. I have posed this question to Square support, and (even though I mentioned I could not access command line and have already done the tutorial mentioned below, I was directed to said tutorial and told to use the command line that I can't use. Here's my problem...
I am trying to set up the square connect API on a webserver for a client. On the webserver, I do not have access to command line (or SSH without an additional cost to the client). The only ways I have found to get the location_id requires command line (as far as I can tell). Is there a way to not have to use command line? Or is there another way to put in the required command without command line access?
Here is what I have done so far:
I read somewhere (can't remember where now) that you can install composer locally, use command line on your machine to install the dependencies needed for square-connect, then move all the files to your server. I have done that to get the files on the server.
My file structure looks like this:
payment-portal/
vendor/
composer/
square/
autoload.php
composer.json
composer.json
composer.lock
composer.phar
locations-test.php
process-card.php
The locations-test.php contains:
<?php
require 'vendor/autoload.php';
$access_token = "ACCESS_TOKEN";
$location_api = new \SquareConnect\Api\LocationApi();
echo $location_api->listLocations($access_token);
Note: ACCESS_TOKEN has been replaced with my personal access token.
Of coarse, if I access this file at mywebpage.com/payment-portal/locations-test.php, I get a blank page.
Also, I am not getting any errors in the error log.
The way to get locations is lined out here.
Under "Retrieving your location IDs." I am OK up until this point, but I cannot use command line in my server to use the code:
php locations-test.php
What are my other options to get the location_id? What am I missing?
Thanks in advance for any help or direction!
According the php-sdk you're using for connecting to the square API, the required format for $access_token should be "Bearer YOUR_ACCESS_TOKEN" no just your access token.
/**
* #param string $authorization The value to provide in the Authorization header of
* your request. This value should follow the format `Bearer YOUR_ACCESS_TOKEN_HERE`.
Try using this instead and see if it works.
require 'vendor/autoload.php';
$access_token = "Bearer ACCESS_TOKEN";
$location_api = new \SquareConnect\Api\LocationApi();
echo $location_api->listLocations($access_token);
Of course, don't forget to replace the ACCESS_TOKEN part with your own access token.
Related
I tried to implement the simplesamlphp library into my web application. But when I call the requireAuth() function I get a PHP fatal error message. Uncaught Exception: Loader: Illegal character in filename.....
It seems like he can't resolve the Class SimpleSAML\Module\saml\Auth\Source\SP
But I don't know why.
Does anyone have a idea how to fix this?
I already deleted the whole simplesamlphp installation and reinstalled it.
I use the following code:
require 'var/www/simplesamlphp/lib/_autoload.php';
$lAuthSrc = new \SimpleSAML\Auth\Simple('default-sp');
if (!$lAuthSrc->isAuthenticated()) {
$lAuthSrc->requireAuth();
}
$lAttributes = $lAuthSrc -> getAttributes();
foreach($lAttributes as $lAttribute) {
print_r($lAttribute);
}
Some additional informations:
The configured authentication source test works fine. If I login via the configured authentication source, everything works fine and I don't get any error messages (the requireAuth() function don't get called in this case).
I use the latest version of simplesamlphp v.1.18.3
If you need any more information, please let me know.
Honestly it looks like your path is messed up on the require... are you sure you should be using:
require 'var/www/simplesamlphp/lib/_autoload.php';
and not
require '/var/www/simplesamlphp/lib/_autoload.php';
Do you really have a 'var/www' subdirectory relative to the location of the script? That looks wrong to me. If you include that first / before var it makes that path absolute to the typical install location for SSP.
Thank you all for your help. I discovered this morning the issue. The issue was the autoloader which I use for my own application. I registered the application autoloader in another file which gets executed before the code you see above. And simplesamlphp uses some conditions like:
if (!class_exists($className))
And beacuse I registered my application autoloader before the function class_exists checked if the class exists in my application. In my application I don't use namespaces and this was the issue.
To fix this issue, I unregistered my application autoloader before using the simplesamlphp code and registered the autoloader again after the simplesamlphp code.
I hope this will save some of you headaches.
I'm trying to work with the Google Cloud Vision API with PHP. I created my first test page and when run from a website, the PHP works correctly with no issues. I have a new twist on my project and I want to run the same PHP code from the terminal.
The issue I'm having now though is that I get an error that my script doesn't have the credentials loaded.
Here is the code I'm using to load my credentials file that works in my website version...
putenv('GOOGLE_APPLICATION_CREDENTIALS=Credential-file.json');
This doesn't work on my Terminal run version of the code. So I've tried a couple other options like...
putenv('GOOGLE_APPLICATION_CREDENTIALS=/Full/Path/To/Credential-file.json');
and
$_ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "/Full/Path/To/Credential-file.json";
The exact error is...
PHP Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /Library/WebServer/Documents/google_vision/vendor/google/auth/src/ApplicationDefaultCredentials.php:156
My question is... Is there a better way to load the credentials file when I'm trying to run my PHP from the terminal? Or what mistake am I making in my version?
I should mention that I'm running this on MacOS with the Google Cloud client libraries.
Any help/suggestions would be greatly appreciated.
Thanks in advance!
The error message indicates that the variable is not set properly (as opposed to it being set to an invalid path).
putenv only lasts the duration of the current script. Setting values on $_ENV does not make them available to getenv, the method used by the Google auth library. Are you setting the credentials environment variable within the same script you're executing?
You can provide the value in the invocation as well:
GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json php my-script.php
Or set the variable in your terminal session:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json
php my-script.php
If you're using Google\Cloud\Vision\V1\ImageAnnotatorClient, you can authenticate with a keyfile directly:
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
$vision = new ImageAnnotatorClient([
'credentials' => '/path/to/keyfile.json
]);
Or with Google\Cloud\Vision\VisionClient:
use Google\Cloud\Vision\VisionClient;
$vision = new VisionClient([
'keyFilePath' => '/path/to/keyfile.json'
]);
I'm following the official docs to setup google cloud firestore to integrate with my php project
[1] https://cloud.google.com/firestore/docs/quickstart-servers
when I initialize the FirestoreClient I get the following error
Error rendering 'projects/{project=*}/databases/{database=*}': expected binding 'project' to match segment '{project=*}', instead got null\n
Provided bindings: Array\n
(\n
[project] => \n
[database] => (default)\n
)\n
First In the documentation it tells to use ENV VARIABLE though using the following command
export GOOGLE_APPLICATION_CREDENTIALS=/Users/user/Desktop/programming/workarea/.firebase.config.json
I investigated a lot and I think the problem happens when the library tries to establish a connection with the firestore server.
I couldn't find the root of the problem. but I think for some reason the php server not reading the env vars so it's not able to establish a proper connection
I struggled some time with this too, resolved it after realizing this: as said here, you first have to authenticate. For that, install the gcloud-sdk available here, then follow the steps provided.
Second, you have to set your environment variable with the path to your project's credentials file, as you were doing, in the bash (i use ~/.bashrc), exit the terminal and reenter it (necessary, as it may not apply the file edit for that terminal session). You could also type source .bashrc to apply changes without closing the terminal.
Then you should be fine, assuming you have the other required environment dependencies installed found here.
Resolving the issue. My Case:
Before reading the question, my issue was solved due to my development environment. Using CodeKit (an application on MacOS), upon building my code from the source folder, items such as the composer.json and other files did not transfer causing the issues described below. If this does happen to you scout the two folder to look for discrepancies the paste the missing docs from the src to the build folder.
:: QUESTION ::
I am starting to use GCP today and after following the instructions defined here:
composer require google/cloud-storage
then:
putenv("GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json");
require __DIR__.'/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$myid = "my-project-id";
$storage = new StorageClient([
'projectId'=>$myid
]);
var_dump($storage->buckets());
When running this i get the following error:
Fatal error: Class 'Google\Auth\Cache\MemoryCacheItemPool' not found in /place/to/vendor/google/cloud-core/RequestWrapperTrait.php on line 94
I have no idea how to solve the issue, as i am just getting started with GCP. No idea whether this is a problem with the platform or my code.
File structure appears as follows for the Google Auth:
vendor
google
auth
src
tests
cloud-core
cloud-storage
the /Cache/MemoryCacheItemPool exists inside both the tests and src folder, but the above is referencing it minus the src or tests folder.
I have also ran:
composer update
and uninstalled and reinstalled the package to no effect
Google Cloud Project Link
Where did you find the code that you used and pasted? Because the one present in the official documentation is different.
This portion of code is the one in the tutorial you linked, try to use the client library and post the error logs if get any!
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$projectId = 'YOUR_PROJECT_ID';
$storage = new StorageClient(['projectId' => $projectId]);
$bucketName = 'my-new-bucket';
$bucket = $storage->createBucket($bucketName);
echo 'Bucket ' . $bucket->name() . ' created.';
Remember that bucket name should be unique and therefore I would advice you to test it with a long complex name to avoid to hit already used names, and always test the result of the operation.
UPDATE
I tested also your code and it is working as well, therefore I believe that is an error in the setup of the environment.
Did you get any error while running the composer require google/cloud-storage? Because the class that is missing Google\Auth\Cache\MemoryCacheItemPool is part of Psr that is installed by the composer
[...]
Installing psr/cache (1.0.1)
Loading from cache
[...]
UPDATE2
Matthew M found the error in its configuration and posted:
Finally resolved the issue. I'm using CodeKit in my working
environment and it looks like it is changing something when it
compiles. Ran an uncompiled version and it's working fine.
In the Heroku PHP documentation they talk about "accessing /db" but I am not sure how to do this. Here is the page I am referring to:
https://devcenter.heroku.com/articles/getting-started-with-php#provision-a-database
It's the part that starts "Now when you access your app’s /db route..."
As a total newbie to PHP and heroku, I had this same question. After some playing around I found that "when you access your app’s /db route" means to do the same thing as you did for the cowsay dependency earlier in the tutorial but replace 'cowsay' with 'db', so
instead of:
$ heroku open cowsay
write
$ heroku open db
/db will be exposed as a web page and will return the contents defined by the query and the template. This way you can easily provide read access (GET http method) for a table's content.