Using $this when not in object context - ZendFramework - php

This is driving me nuts.
Downloaded and installed ZendFramework. Trying to send a DKIM signed mail but it is giving me this error(which i copied directly off their documentation).
Error: Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\Project\send_form_email.php:12 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Project\send_form_email.php on line 12
// sign message with dkim
$signer = $this->getServiceLocator()->get('DkimSigner');
$signer->signMessage($mail);
What am I doing wrong here?
Edit: Added all coding
<?php
require 'vendor/autoload.php';
$mail = new \Zend\Mail\Message();
$mail->setBody("Test world!");
$mail->setFrom('noreplytest#sanscalc.co.za');
$mail->addTo('jr.swart49995#gmail.com');
$mail->setSubject('le subject');
// sign message with dkim
$signer = $this->getServiceLocator()->get('DkimSigner');
$signer->signMessage($mail);
// send message
$transport = new \Zend\Mail\Transport\Sendmail();
$transport->send($mail);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>

$this is only valid when called from inside an object. Your code is not within an object, which is why calling $this throws the error.
$this->getServiceLocator()->get('DkimSigner');
your code is expecting to call a getServiceLocator() method defined within an object, which you have not done as your code is more procedural in nature.
I am guessing you're trying to use the example code from here: https://github.com/joepsyko/zf-dkim
If you check that page it tells you the code should be within a controller (object) where $this is in a valid context, and also where the getServiceLocator method is defined.
try and Install the standard "getting started" zend framework and try the snippet within a controller (https://docs.zendframework.com/tutorials/getting-started/skeleton-application/)
or do away with the use of the service locator in your code and instantiate the DKimm Signer yourself within your script manually.

Related

BigQuery [PHP API] get list of accessible projects

I used below PHP code based on the GCP documentation here to fetch a list of all the projects which can be accessed with the selected service account.
require __DIR__ . '/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=acc-key.json');
$client = new Google_Client();
$client->setApplicationName('SampleApp');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$service = new Google_Service_Bigquery($client);
$optParams = [];
do {
$response = $service->projects->listProjects($optParams);
foreach ($response['projects'] as $project) {
print_r($project);
}
$optParams['pageToken'] = $response->getNextPageToken();
} while ($optParams['pageToken']);
This does work and fetches only one or two projects from a total of 25 projects which can be accessed with this key.
I confirmed the permission on other projects by accessing them while providing an exclusive project name.
Any hints on what could be the missing part?
EDIT: [Based on Mikhail's answer]
I get below error if I change
$service = new Google_Service_Bigquery($client);
to
$service = new Google_Service_CloudResourceManager($client);
I get below error
PHP Notice: Undefined property: Google_Service_CloudResourceManager::$projects in /tests/ListFullSchema.php on line 18
Notice: Undefined property: Google_Service_CloudResourceManager::$projects in /tests/ListFullSchema.php on line 18
PHP Fatal error: Uncaught Error: Call to a member function listProjects() on null in /tests/ListFullSchema.php:18
Stack trace:
#0 {main}
thrown in /tests/ListFullSchema.php on line 18
Fatal error: Uncaught Error: Call to a member function listProjects() on null in /tests/ListFullSchema.php:18
It is because Cloud Resource Manager v2 doesn't have projects.list method, but v1 does. If you'd like to use Google APIs Client Library for PHP for this task, you can download the v1.1.8 and install it following the instructions here. Then you can run the PHP code like in here.
You should use:
$service = new Google_Service_CloudResourceManager($client);
Alternatively, you can use Python, like in the example. The service version is specified inside the code so you don't need custom installations.

How to get "lavary/crunz" to work with twig

Ok, ill try to explain as easy as possible:
In my PHP Web Application in which i have setup some automated Tasks with "lavary/crunz".
The Problem i am facing is when trying to use "Twig Templating engine" to create my Email Body, somehow it wont work and i the error message i get when debugging is not helpfull.:
class MyClass
{
public static function _testTask()
{
$receiver = ['email'=>COMPANY_EMAIL,'name'=>COMPANY_NAME];
return function() use($receiver)
{
$mail = new \MailerCtrl();
$loader = new \Twig_Loader_Filesystem("views/templates");
$twig = new \Twig_Environment($loader,["cache" => "views/cache"]);
$content = array('name'=>'My Name','age'=>25);
$subject = 'Something';
$mail->send($subject,$receiver,$twig->render('report.html.twig', $content));
};
}
}
$schedule->run(MyClass::_testTask())->cron('* * * * *');
My MailerCtrl class is a simple PHPMailer representation which expects the html body i expect to render from $twig->render('report.html.twig', $content);
Is there another or even a better way to accomplish this? When i call this script from http it works as expected but when running it through "crunz" i am getting this error:
PHP Catchable fatal error:
Argument 1 passed to Twig_Filter::__construct() must be an instance
of string, string given, called in
/home/httpd/vhosts//httpdocs/vendor/twig/twig/lib/Twig/Extension/Core.php
on line 139 and defined in
/home/httpd/vhosts//httpdocs/vendor/twig/twig/lib/Twig/Filter.php on
line 35
HTTP is running PHP 7
and
"lavary/crunz"
via PHP command line isn't running PHP 7 as Twig 2.0 is required at least PHP 7 as describe here: https://stackoverflow.com/a/41888528/1865829
Update your PHP command line to 7 and it should work.

Webtools and developer tools issue on Phalcon framework

I installed phalcon 3.0.1-14 on an Ubuntu 14.04 box. Also installed Phalcon DevTools (3.0.1).
Initially, I enabled the webtools and when I visit that page, some warnings appear all the time:
Cannot bind an instance to a static closure in /home/pish/vendor/phalcon/devtools/scripts/Phalcon/Web/Tools.php
Cannot bind an instance to a static closure in /home/pish/vendor/phalcon/devtools/scripts/Phalcon/Web/Tools/views/index.phtml
I just ignored them and tried to create a model out of an existing table in the database. When I clicked on "Generate" button
I get the following error:
Phalcon\Mvc\Dispatcher\Exception: ModelsController handler class cannot be loaded
and the model is not created. I tested creating a controller as well, but a similar error occurred and the controller
was not created either.
Finally, I created the model via the console phalcon model users and it was created successfully.
I noticed, though, that the validation function created by the developer tools doesn't work and causes the following
error when I try to create a user:
Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Email given in...
My question is basically, is there something bad with the version of developer tools I installed that causes the problems
with the Webtools and the functions that are generated for models/controllers, etc.? Or I might have something wrong
in my system?
Cannot bind an instance to a static closure
https://github.com/phalcon/cphalcon/issues/11029
Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate()
Fixed in the 3.0.x branch (will be released soon)
Regarding your second error message:
Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Email given in...
Model validation has changed in Phalcon 3.0. In Phalcon v2 you had to do
public function validation()
{
$this->validate(
new Phalcon\Mvc\Model\Validator\Email(['field' => 'email']);
);
if ($this->validationHasFailed() == true) {
return false;
}
}
But the Phalcon\Mvc\Model\Validation is deprecated in v3 and you should use Phalcon\Validation instead. Just alter your code to the following:
public function validation()
{
$validator = new Validation();
$validator->add(
'email', //your field name
new Phalcon\Validation\Validator\Email([
'model' => $this,
'message' => 'Please enter a correct email address'
])
);
return $this->validate($validator);
}
Maybe the DevTools haven't updated this part yet, I am not sure.

Call to undefined method Mail::setMessageType()

I have a website and it has a member registry (Way of Life). It uses phpmailer which I am not very familiar with. When you enter data and click register, it returns this:
Fatal error: Call to undefined method Mail::setMessageType() in
/home/u536535282/public_html/classes/phpmailer/phpmailer.php on line
989
The lines that this is referring to are shown below:
$this->error_count = 0; // reset errors
$this->setMessageType();
if (!$this->AllowEmpty and empty($this->Body)) {
throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL);
}
If someone colud tell me what i need to change, I would appreciate it, as it would get my site going again

Fatal error: Call to undefined method Zend_XmlRpc_Value::getGenerator() magento

I was upgrading magento when something went wrong and now when I try to login to admin, I am unable to log in to back end admin of magento and I get the following error
Fatal error: Call to undefined method Zend_XmlRpc_Value::getGenerator() in /home/boutique/public_html/app/code/core/Zend/XmlRpc/Request.php on line 413
and the code on respective lines is
/**
* Create XML request
*
* #return string
*/
public function saveXml()
{
$args = $this->_getXmlRpcParams();
$method = $this->getMethod();
$generator = Zend_XmlRpc_Value::getGenerator();
$generator->openElement('methodCall')
->openElement('methodName', $method)
->closeElement('methodName');
I cant understand why this issue is happening, I tried replacing request.php and response.php files from fresh download of magento..
can body help me? why this eror is popping?
There's something about your installation of PHP and Magento that's broken — for some reason the Zend_XmlRpc_Value object that's instantiated doesn't contain a getGenerator method. The class for this object is normally defined in
lib/Zend/XmlRpc/Value.php
However, it's possible there may be a class override in place at
app/code/core/Zend/XmlRpc/Value.php
app/code/community/Zend/XmlRpc/Value.php
app/code/local/Zend/XmlRpc/Value.php
It's also possible your system may have another version of the zend framework installed somewhere in the PHP include path.

Categories