Can't solve the Undefined index: outputBuffering in /vendor/slim/slim/Slim/App.php on line 252 error - slim

I am following the Destructy tutorial and, after setting up and testing out the database connection I get the following error:
So far, this are my settings:
return [
'settings' => [
'displayErrorDetails' => true,
],
'url' => 'http://localhost',
'db' => [
'mysql' => [
'host' => 'localhost',
'dbname' => 'destructy',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]
],
];
This is how the container is defined:
/*Set Slim up*/
$container = new \Slim\Container();//Ver si le afecta o no los paréntesis ()
$container['settings'] = function($c){
return $c['config']->get('settings');
};
$container['config'] = function($c){ //c= current state of our container
return new \Noodlehaus\Config('../config/app.php');//We could pass more parameters ... as an array ['param1','param2','etc']
};
$container['view'] = function($c){//c= current state of our container
$view = new Slim\Views\Twig('../resources/views');//Ubicación de las vistas
//Ability to generate URLs to routes from within our views
$view->addExtension(new \Slim\Views\TwigExtension(
$c['router'],
$c['config']->get('url')
));
return $view;
};
//base de datos:
$container['db'] = function($c){
//return new PDO('mysql:host=127.0.0.1;dbname=destructy','root','root');
return new PDO(
'mysql:host='.$c['config']->get('db.mysql.host').';dbname='.$c['config']->get('db.mysql.dbname'),
$c['config']->get('db.mysql.username'),
$c['config']->get('db.mysql.password'));
};
$app = new Slim\App($container);
require_once 'routes.php';
The route:
$app->get('/',function($request,$response,$args){
echo 'Destructy - Home.';
echo '<p style="color: darkblue">Welcome to the admin area</p>';
echo '<p>Testing access to config parameters ...</p>';
echo $this->config->get('db.mysql.host').'<br>';
echo $this->config->get('url');
/*Testing out DB connection*/
var_dump($this->db);
$this->view->render($response,'home.twig');
});
$app->run();
Does anybody know what the problem is in order to fix it?

You're not able to use the dot syntax to access the config values, should be something like:
$container['db'] = function($c){
$conf = $c->db;
return new PDO(
'mysql:host='.$conf['mysql']['host'].';dbname='.$conf['mysql']['dbname'],
$conf['mysql']['username'],
$conf['mysql']['password'];
};

Related

Request timeout - integrate third party library with codeigniter 3

Im working on API integration InPost API Create shippment. I try integrate third party library inpost with codeigniter 3 from GitHub.
https://github.com/imper86/php-inpost-api
I install this library via composer.
View:
<?php echo form_open('inpost_controller/inpost_shippment_post'); ?>
<div class="form-group">
</div>
</div>
<?php echo form_close(); ?><!-- form end -->
Then I call in controller:
require FCPATH . 'vendor/autoload.php';
Full code file Controller:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
require FCPATH . 'vendor/autoload.php';
use Imper86\PhpInpostApi\Enum\ServiceType;
use Imper86\PhpInpostApi\InpostApi;
class Inpost_controller extends Admin_Core_Controller
{
public function __construct()
{
parent::__construct();
}
/**
* Create shippment Inpost Post
*/
public function inpost_shippment_post()
{
$token = 'xxxxx';
$organizationId = 'xxxxx';
$isSandbox = true;
$api = new InpostApi($token, $isSandbox);
$response = $api->organizations()->shipments()->post($organizationId, [
'receiver' => [
'name' => 'Marek Kowalczyk',
'company_name' => 'Company name',
'first_name' => 'Jan',
'last_name' => 'Kowalski',
'email' => 'test#inpost.pl',
'phone' => '888888888',
'address' => [
'street' => 'Malborska',
'building_number' => '130',
'city' => 'Kraków',
'post_code' => '30-624',
'country_code' => 'PL',
],
],
'sender' => [
'name' => 'Marek Kowalczyk',
'company_name' => 'Company name',
'first_name' => 'Jan',
'last_name' => 'Kowalski',
'email' => 'test#inpost.pl',
'phone' => '888888888',
],
'parcels' => [
['template' => 'small'],
],
'insurance' => [
'amount' => 25,
'currency' => 'PLN',
],
'cod' => [
'amount' => 12.50,
'currency' => 'PLN',
],
'custom_attributes' => [
'sending_method' => 'parcel_locker',
'target_point' => 'KRA012',
],
'service' => ServiceType::INPOST_LOCKER_STANDARD,
'reference' => 'Test',
'external_customer_id' => '8877xxx',
]);
$shipmentData = json_decode($response->getBody()->__toString(), true);
while ($shipmentData['status'] !== 'confirmed') {
sleep(1);
$response = $api->shipments()->get($shipmentData['id']);
$shipmentData = json_decode($response->getBody()->__toString(), true);
}
$labelResponse = $api->shipments()->label()->get($shipmentData['id'], [
'format' => 'Pdf',
'type' => 'A6',
]);
file_put_contents('/tmp/inpost_label.pdf', $labelResponse->getBody()->__toString());
}
}
When I post form, after 30 sec I get error 500 Internar Error Server Request timout.
And now im not sure how to debug now. I enable error log in CI3 application/logs/ I open this file but I not see any error related to this.
Could be a defect, or missing http2 setup/cfg.
Since the header in https2 protocol has shorter header on packs.
Not sure doe
https://caniuse.com/http2 < short http2 (TLS, HTTPS) overview
https://factoryhr.medium.com/http-2-the-difference-between-http-1-1-benefits-and-how-to-use-it-38094fa0e95b < http2 as protocol overview in 5 min

Argument 1 passed to GuzzleHttp\Client::send() must implement interface

hi I am new to laravel and I am trying to consume the api with laravel 8 I have a problem with my POST and I do not understand
public function storeEntreprise(Request $request){
$request->validate([
'name' => 'required',
'email' => 'required',
'phone_number'=>'required',
'address' => 'required',
'password' => 'required',
'password_confirmation' => 'required'
]);
$client = new Client();
$post = $request->all();
$url = "http://flexy-job.adsyst-solutions.com/api/entreprises-store";
$create = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'text/html; charset=UTF8',
],
'form-data' => [
'name' => $post['name'],
'email' => $post['email'],
'phone_number' => $post['phone_number'],
'address' => $post['address'],
'logo' => $post['logo'],
'password' => $post['password'],
'password_confirmation' => $post['password_confirmation']
]
]);
//dd($create->getBody());
echo $create->getStatusCode();
//echo $create->getHeader('Content-Type');
echo $create->getBody();
$response = $client->send($create);
return redirect()->back();
}
Can you help me please
You calling (accidentally?) $response = $client->send($create); where $create is response of API request you made ($create = $client->request('POST', $url, ...).
So PHP reports you that you can't pass ResponseInterface where RequestInterface required.
Also, you echo's body of response, and return redirect response at same time. So browser will not show you API response (because of instance back redirect).

when selecting an option, show data from the database - Woocommerce custom fields (checkout)

I have the following code to generate a select and bring the values inside the options:
add_action('woocommerce_after_order_notes', 'cliente_woocommerce');
function cliente_woocommerce($checkout)
{
global $wpdb;
/// in tab_clientes have id, nome, cpf, cnpj, ie, email, data_since columns
$results = $wpdb->get_results("SELECT * FROM tab_clientes");
$options = ['' => __('Selecione o cliente')];
foreach ($results as $result) {
$options[$result->nome] = $result->razao_social;
}
echo '<div id="cliente_woocommerce"><h2>' . __('Cliente') . '</h2>';
woocommerce_form_field(
'cliente',
[
'type' => 'select',
'class' => ['cliente form-row-wide'],
'label' => __('Campo de Teste (Cliente)'),
'options' => $options,
],
$checkout->get_value('cliente')
);
woocommerce_form_field(
'nome',
[
'type' => 'text',
'class' => ['nome form-row-wide'],
'label' => __('Razão Social'),
'default' => '',
],
$checkout->get_value('nome')
);
woocommerce_form_field(
'cnpj',
[
'type' => 'text',
'class' => ['cnpj form-row-wide'],
'label' => __('CNPJ'),
'default' => '',
],
$checkout->get_value('cnpj')
);
echo '</div>';
}
with the following script:
$(document).ready(function()
{
$('#cliente').change(function() {
$('#nome').val( $( this ).val() );
});
$('#nome').change(function() {
$('#cnpj').val( $( this ).val() );
});
});
When I select the client, the #nome field (razão social - in the table = razao_social) appears with the correct value, but the value repeats within CNPJ field.
what am I doing wrong?

How to send email with phpmailer in phalcon php

Im new in phalcon want a working example of phpmailer that send email from my site. currently i just figure out like this. i just follow this link: github.com/asyamedya/phalcon-phpmailer.
[config.php]
$config = new Phalcon\Config(array(
"app" => array(
"controllersDir"=> "app/controllers/",
"modelsDir" => "app/models/",
"viewsDir" => "app/views/",
"pluginsDir" => "app/plugins/",
"libraryDir" => "app/library/",
"helpersDir" => "app/helpers/",
"formsDir" => "app/forms/",
"cacheDir" => "cache/volt/",
"logsDir" => "cache/logs/",
"encryptKey" => "",
"baseUri" => "/demo/",
"StaticBaseUri" => "http://localhost/demo/",
"debug" => '0',
),
"mail" => array(
"driver" => "smtp",
"host" => "smtp.gmail.co",
"username" => "2munax#gmail.com",
"password" => "*****",
"security" => "tls", //ssl:465, tls:587
"port" => 587,
"charset" => "UTF-8",
"email" => "2munax#gmail.com",
"name" => "webmaster",
),
));
[loader.php]
use Phalcon\Loader;
$loader = new Loader();
$loader->registerDirs(
array(
APP_PATH . $config->app->controllersDir,
APP_PATH . $config->app->modelsDir,
APP_PATH . $config->app->pluginsDir,
APP_PATH . $config->app->libraryDir,
APP_PATH . $config->app->helpersDir,
APP_PATH . $config->app->formsDir,
APP_PATH . $config->app->cacheDir,
)
)->register();
[service.php]
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\Router;
use Phalcon\Security;
use \PHPMailer as PHPMailer;
use Phalcon\Mvc\View\Engine\Volt;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Flash\Session as FlashSession;
use Phalcon\Logger\Adapter\File as Logger;
use Phalcon\Mvc\Model\Metadata\Apc as ApcMetaData;
use Phalcon\Session\Adapter\Files as SessionAdapter;
require(APP_PATH . $config->app->libraryDir . 'PHPMailer/PHPMailerAutoload.php');
$di->set('pmailer', function() use ($config){
$pmailer = new PHPMailer();
//$mail->setLanguage('fr', '/optional/path/to/language/directory/');
$pmailer->isSMTP(true);
$pmailer->SMTPDebug = 2; // Enable verbose debug output
$pmailer->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$pmailer->Charset = $config->mail->charset;
$pmailer->Host = $config->mail->host;
$pmailer->SMTPAuth = true;
$pmailer->Username = $config->mail->username;
$pmailer->Password = $config->mail->password;
$pmailer->SMTPSecure = $config->mail->security;
$pmailer->Port = $config->mail->port;
$pmailer->addAddress($config->mail->email,$config->mail->name);
$pmailer->isHTML(true);
return $pmailer;
});
[controller index]
public function SendmailAction()
{
$this->pmailer->From = "2munax#gmail.com";
$this->pmailer->FromName = "user";
$this->pmailer->addReplyTo("2munax#gmail.com", "user");
$this->pmailer->addAddress('babymachinery83#gmail.com');
$this->pmailer->Subject = "email test !";
$this->pmailer->Body = "success!";
$this->pmailer->WordWrap = 70;
if(!$this->pmailer->send()){echo 'Message could not be sent.';
echo 'Mailer Error: ' . $this->pmailer->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
In my library i have this files.
[Library/PHPMailer/]
class.phpmailer.php
class.pop3.php
class.smtp.php
PHPMailerAutoload.php
class.phpmaileroauthgoogle.php
I'm not sure, but you I think this is wrong in your service.php
use Phalcon\PHPMailer;
Change it to
use \PHPMailer as PHPMailer;

Connect With PostgreSQL in Phalcon Framework

Phalcon is not able to connect to postgrsql. Here are my settings in config.php
return new \Phalcon\Config(array(
'database' => array(
'adapter' => 'Postgresql',
'host' => 'localhost',
'username' => 'postgres',
'password' => 'root',
'dbname' => 'mydb',
'charset' => 'utf8',
),
'application' => array(
'controllersDir' => __DIR__ . '/../../app/controllers/',
'modelsDir' => __DIR__ . '/../../app/models/',
'viewsDir' => __DIR__ . '/../../app/views/',
'pluginsDir' => __DIR__ . '/../../app/plugins/',
'libraryDir' => __DIR__ . '/../../app/library/',
'cacheDir' => __DIR__ . '/../../app/cache/',
'baseUri' => '/test/',
)
));
Page is blank showing no errors.
DI service implementation
use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;
$di->set('db', function () use ($config) {
return new DbAdapter(array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
"charset" => $config->database->charset
));
});
There is no port in db connection array you used. Default postgresql port is 5432. so use this in your db connection and try to pass array directly to postgresql adapter constructor.
or
install postgres pdo /php pdo again.
I think that code is fine.
I was working a project where i had to use MySQL and PostGres.I connected my Postgresdb by following-
Below default 'db' connection string i wrote the following code inside services.php-
$di->setShared('pgphalcon', function () {
$config = $this->getConfig();
$adaptar = "PostgreSQL";
$class = 'Phalcon\Db\Adapter\Pdo\\' . $adaptar;
$params = [
'host' => "localhost",
'port' => "5432", // for localhost no need to put this line unless u change the port
'username' => "postgres",
'password' => "12345",
'dbname' => "phalcon",
];
$connection = new $class($params);
return $connection;
});
Then your controller function
do the following-
public function yourcontrollernameAction()
{
$con = $this->di->get('pghalcon');
$post = $con->fetchOne("SELECT * FROM blog ORDER BY id DESC LIMIT 1", Phalcon\Db::FETCH_ASSOC);
$this->view->data= $post;
}
For better Understanding I'd like to suggest visit this url
One thing to clarify- I was failed to do db operation by Model for PostGreSQL. That's why I used this way.