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'.
Related
I downloaded Phalcon from official website
I copied php_phalcon.dll file to my xampp's php/ext directory
Edited the php.ini file located at D:\xampp\php\php.ini. and add there line extension=php_phalcon.dll at the end of the file.
I Restarted apache server and computer several times.
When I write phpinfo() to my code it seems phalcon was installed
Unfortunatelly Whan I try to run some code like
<?php
try {
// Autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs([
'../app/controllers/',
'../app/models/'
]);
$loader->register();
// Dependency Injection
$di = new \Phalcon\DI\FactoryDefault();
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views');
return $view;
});
// Deploy the App
$app = new \Phalcon\Mvc\Application($di);
echo $app->handle()->getContent();
} catch(\Phalcon\Exception $e) {
echo $e->getMessage();
}
?>
I get this error
Fatal error: Uncaught Error: Class 'Phalcon\Loader' not found in D:\xampp\htdocs\php-learning\public\index.php:4 Stack trace: #0 {main} thrown in D:\xampp\htdocs\php-learning\public\index.php on line 4
I also tried to follow the steps from tutorial on Phalcon ofical website where code looks somehow like this
<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Url;
// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
// Register an autoloader
$loader = new Loader();
$loader->registerDirs(
[
APP_PATH . '/controllers/',
APP_PATH . '/models/',
]
);
$loader->register();
$container = new FactoryDefault();
$container->set(
'view',
function () {
$view = new View();
$view->setViewsDir(APP_PATH . '/views/');
return $view;
}
);
$container->set(
'url',
function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
}
);
$application = new Application($container);
try {
// Handle the request
$response = $application->handle(
$_SERVER["REQUEST_URI"]
);
$response->send();
} catch (\Exception $e) {
echo 'Exception: ', $e->getMessage();
}
But didnĀ“t help. What I am doing wrong?
You have Phalcon installed successfully. However the namespace should be changed:
Moved Phalcon\Loader to Phalcon\Autoload\Loader #15797
please refer to the change log
enter link description here
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 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