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
Related
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'.
At present, I have the following routes
Route::post('/report', [App\Http\Controllers\ServerController::class, 'report'])->name('report');
Route::get('/report', [App\Http\Controllers\ServerController::class, 'getData'])->name('get_data');
Route::get('/other',[App\Http\Controllers\ServerController::class, 'other'])->name('other');
Route::get('/other2',[App\Http\Controllers\ServerController::class, 'other2'])->name('other2');
Route::get('/other3',[App\Http\Controllers\ServerController::class, 'other3'])->name('other3');
...
I create report.php in the public directory
I want to bind the get and post requests of report.php to report and get_data
But I didn't find a similar explanation in the official document
What should I do?
Update:2021-05-21
I found a solution, but it didn't look elegant
Are there any other solutions?
<?php
// this is public/report.php
use Illuminate\Http\Request;
use Illuminate\Contracts\Http\Kernel;
define('LARAVEL_START', microtime(true));
if (file_exists(__DIR__ . '/../storage/framework/maintenance.php')) {
require __DIR__ . '/../storage/framework/maintenance.php';
}
require __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';
$_SERVER['REQUEST_URI'] = "/report.php/report";
$_SERVER['DOCUMENT_URI'] = "/report.php/report";
// var_dump($_SERVER);
$kernel = $app->make(Kernel::class);
$response = tap($kernel->handle(
$request = Request::capture()
))->send();
$kernel->terminate($request, $response);
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';
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.
i am new in firebase web if it possible to upload, download, and delete file using php. i have upload file using JS but i want to download using PHP.
Here is script of download file using JS but i want in PHP.
Thanks in advance...
My Code
[START storage_quickstart]
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Storage\StorageClient;
# Your Google Cloud Platform project ID
$projectId = 'My project ID';
# Instantiates a client
$storage = new StorageClient([
'projectId' => $projectId
]);
# The name for the new bucket
$bucketName = 'my bucket';
# Creates the new bucket
$bucket = $storage->createBucket($bucketName);
echo 'Bucket ' . $bucket->name() . ' created.';
# [END storage_quickstart]
return $bucket;
The short answer is that you should use gcloud-php. This requires that you set up a service account (or use Google Compute Engine/Container Engine/App Engine which provide default credentials).
It's likely that you'll create a service account, download a keyfile.json, and provide it as an argument to the StorageClient, like so:
# Instantiates a client
$storage = new StorageClient([
'keyFilePath' => '/path/to/key/file.json',
'projectId' => $projectId
]);
Alternatively, it looks like they've built another layer of abstraction, which takes the same arguments but allows you to use lots of other services:
use Google\Cloud\ServiceBuilder;
$gcloud = new ServiceBuilder([
'keyFilePath' => '/path/to/key/file.json',
'projectId' => 'myProject'
]);
$storage = $gcloud->storage();
$bucket = $storage->bucket('myBucket');
That's an old question, but I was struggling with same problem... hope my solution help someone.
In fact, I really don't know if there is an official way to do that, but I created the method below and it worked for me.
function storageFileUrl($name, $path = []) {
$base = 'https://firebasestorage.googleapis.com/v0/b/';
$projectId = 'your-project-id';
$url = $base.$projectId.'/o/';
if(sizeof($path) > 0) {
$url .= implode('%2F', $path).'%2F';
}
return $url.$name.'?alt=media';
}
To access files in the root of bucket:
$address = storageFileUrl('myFile');
Result: https://firebasestorage.googleapis.com/v0/b/your-project-id.appspot.com/o/myFile?alt=media
To access files inside some folder, do:
$address = storageFileUrl('myFile', ['folder', 'subfolder']);
Result: https://firebasestorage.googleapis.com/v0/b/your-project-id.appspot.com/o/folder%2Fsubfolder%2FmyFile?alt=media
Enjoy.