i've problem with Google App Engine and PHP.
I use Slim framweork for a sample project to test App Engine (currently I use AWS).
I created a simple Slim app and deployed it to App engine and to AWS. On AWS it runs fine, but on App Engine i get "class not found error":
2018/07/29 21:38:15 [error] 9#9: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Class 'projectName\Middleware\Logging' not found in /app/public/index.php:21
thrown in /app/public/index.php on line 51" while reading response
header from upstream, client: 172.17.0.4, server: , request: "GET //
HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host:
"projectName-183713.appspot.com"
/public/index.php
<?php
namespace projectName;
require __DIR__ . '/../vendor/autoload.php';
session_start();
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
require __DIR__ . '/../src/dependencies.php';
require __DIR__ . '/../src/middleware.php';
require __DIR__ . '/../src/routes.php';
require __DIR__ . '/../src/projectName/Routes/route_login.php';
require __DIR__ . '/../src/projectName/Routes/route_user.php';
require __DIR__ . '/../app/routes.php';
require __DIR__ . '/../app/database.php';
use \projectName\Middleware\Logging;
use \projectName\Middleware\Authentication;
$logging = new Logging();
$auth = new Authentication($app->getContainer()->get('router'));
$app->add($logging);
$app->add($auth);
$app->get('/hello/{name}', function ($request, $response, $args) {
echo 'Hello';
return $response;
});
$app->run();
/src/projectName/Middleware/Logging.php
<?php
namespace projectName\Middleware;
class Logging{
public function __invoke($request, $response, $next)
{
$response = $next($request, $response);
return $response;
}
}
app.yaml
runtime: php
env: flex
runtime_config:
document_root: public
handlers:
- url: .*
script: index.php
- url: /(.+\.php)$
script: \1
__DIR__ . '/../vendor/autoload.php'
"autoload": {
"psr-4": {
"\\projectName\\": "src/projectName",
"\\projectName\\Models\\": "src/projectName/Models",
"\\projectName\\Middleware\\": "src/projectName/Middleware"
}
}
The problem was with psr-4 in composer.json.
App Enginen doesn't work with two backslash in the route. So, the solution is:
"\\projectName\\": "src/projectName",
=>
"projectName\\": "src/projectName",
AWS and local dev (standard php docker) works fine with double backslash, but App engine not.
Related
I tried with the below code to get the Account details, it's working fine.
require_once __DIR__ . "/vendor/autoload.php";
$apikey = "<API KEY FROM HELLOSIGN>";
$client = new HelloSign\Client($apikey);
$account = $client->getAccount();
But when I try with the below code to get an Account using SDK, reference link: https://developers.hellosign.com/api/reference/operation/accountGet/:
require_once __DIR__ . "/vendor/autoload.php";
$config = HelloSignSDK\Configuration::getDefaultConfiguration();
// Configure HTTP basic authorization: api_key
$config->setUsername("YOUR_API_KEY");
// or, configure Bearer (JWT) authorization: oauth2
// $config->setAccessToken("YOUR_ACCESS_TOKEN");
$api = new HelloSignSDK\Api\AccountApi($config);
try {
$result = $api->accountGet(null, 'jack#example.com');
print_r($result);
} catch (HelloSignSDK\ApiException $e) {
$error = $e->getResponseObject();
echo "Exception when calling HelloSign API: "
. print_r($error->getError());
}
It throws an error: Fatal error: Uncaught Error: Class "HelloSignSDK\Configuration" not found
Added autoload, why is this issue if using SDK? Kindly help!
I'm just getting started with PHP and I ran into a small problem.
I've downloaded a package using composer require <package_name> and now I'm not sure how to access it from my .php file. I tried bunch of things but I couldn't make it work.
I'm trying to use this package: https://packagist.org/packages/giggsey/libphonenumber-for-php
EDIT:
This is the code I use to test:
<?php
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
// Instantiate app
$app = AppFactory::create();
// Add Error Handling Middleware
$app->addErrorMiddleware(true, false, false);
// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
$swissNumberStr = "044 668 18 00";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {
$swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH");
var_dump($swissNumberProto);
} catch (\libphonenumber\NumberParseException $e) {
var_dump($e);
}
// Run application
$app->run();
And it says: Undefined type 'libphonenumber\PhoneNumberUtil'.
I am trying to set up a website that has google sheets API functionality, due to the constraints of the hosting service, I installed the API on my local computer with Composer before uploading everything onto the site. However, when I try creating the Google_Service_Sheets object, it says that the class cannot be found.
I've tried recreating the autoload.php file, and also adding a seperate require function:
require_once "./googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets.php";
Main code:
require_once $configs["googleapifilev2"];
require_once "./googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets.php";
$client = new \Google_Client();
$client->setApplicationName('test');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig($configs["googlecredentials"]);
$service = new \Google_Service_Sheets($client);
autoload.php code:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit68a7a3a3b2a74c49476ad55dd7b1c990::getLoader();
An error occurs only when I call the Google_Service_Sheets object, but not the Google_Client.
Error message:
Fatal error: Uncaught Error: Class 'Google_Service_Sheets_Resource_Spreadsheets' not found in /storage/ssd3/963/10211963/public_html/googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets.php:69
Stack trace:
#0 /storage/ssd3/963/10211963/public_html/twowvotingaction.php(56): Google_Service_Sheets->__construct(Object(Google_Client))
#1 {main} thrown in /storage/ssd3/963/10211963/public_html/googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets.php on line 69
(Related lines in the API sheets.php file):
public function __construct(Google_Client $client, $rootUrl = null)
{
parent::__construct($client);
$this->rootUrl = $rootUrl ?: 'https://sheets.googleapis.com/';
$this->servicePath = '';
$this->batchPath = 'batch';
$this->version = 'v4';
$this->serviceName = 'sheets';
$this->spreadsheets = new Google_Service_Sheets_Resource_Spreadsheets( //Line 69
$this,
$this->serviceName,
'spreadsheets',
For some reason, when uploading the files, I accidentally put the ./googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets folder, which defines the class, in the wrong directory, I moved it, and it now works.
I have created a slim project in godaddy. after creation, I verify everything works by going to pctechtips.org/apps/slimpro/public and I get the slim webpage. the default index.php inside public directory contaings some code to display that page. when I remove the code and replace it with mine. It doesn't display anything. what is wrong?
default slim install index.php
<?php
if (PHP_SAPI == 'cli-server') {
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
my index.php page
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
?>
I Think the problem here is about the path of composer auto load file
Its not correct to be in the public directory
In default index.php
require __DIR__ . '/../vendor/autoload.php';
In your code
require 'vendor/autoload.php';
SettingsController.php:
namespace App\Controllers;
use Google\Cloud\Storage\StorageClient;
class SettingsController extends Controller {
public function createBucket($request, $response) {
$projectId = 'myProjectId'; //here I specified my projectId
$storage = new StorageClient([
'projectId' => $projectId
]);
$bucketName = 'socnetfilestestkekdsfa213kfh34';
$bucket = $storage->createBucket($bucketName);
}
}
So, I wrote this code in accordance with documentation, but I'm always getting 500 Network Error.
This is my index.php:
<?php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../config/db.php';
$app = new \Slim\App(['settings' => $config]);
require __DIR__ . '/../app/dependencies.php';
require __DIR__ . '/../app/routes.php';
$app->run();
And this is my project structure:
Finally, I've solved the problem.
As I mentioned require __DIR__ . '/../vendor/autoload.php'; was correct. In my case, the reason of 500 Error was an environment variable. For the deployed app you should put your Service Google Cloud Account key config.json into your project. I put mine in a config folder (check my project structure above).
It means that in Heroku 'Settings' i need to specify
GOOGLE_APPLICATION_CREDENTIALS: ../config/yourAppName.json
Thanks this article:
https://medium.com/#naz_islam/how-to-authenticate-google-cloud-services-on-heroku-for-node-js-app-dda9f4eda798