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

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.

Related

PEAR HTML_Template_IT/HTML_Template_ITX with PHP 7.4 result in empty page

One of my projects utilizes the PEAR package HTML_Template_IT plus its extension HTML_Template_ITX. The code worked until a long overdue shift from PHP 5 to PHP 7.4. Now we're getting issues with referencing HTML_Template_ITX() from the extending class.
The implementation in a nutshell looks as sketched below:
<?php
//=== somesite.php ===//
[...]
$template = new siteMainTemplate("article.tpl");
$template->setCurrentBlock("ARTICLE");
$template->setVariable("ARTICLE_HEAD", "some headline");
$template->setVariable("ARTICLE_BODY", "some text");
[...]
$template->parseCurrentBlock();
$template->showSite();
[...]
?>
The class definition extending the PEAR package:
<?php
//=== classdefs.php ===//
require_once "../PEAR/ITX.php";
class siteMainTemplate extends HTML_Template_ITX {
function __construct($pageBody, $pageTitle) {
$this->template = $this->HTML_Template_ITX("../templates/");
[...]
} // ... end of function
function showSite() {
$this->show();
} // ... end of function
} // ... end of class definition
?>
This results in the error "Call to undefined method siteMainTemplate::HTML_Template_ITX()".
The line throwing the error is
$this->template = $this->HTML_Template_ITX("../templates/");
and it looks odd to begin with, but has worked for a very long time. Changing it to something like
$this->template = new HTML_Template_ITX("../templates/");
results in an empty/blank page - well, with errors turned on the following is really happening:
Warning: preg_match_all(): Empty regular expression in /.../PEAR/IT.php on line 1020
Warning: preg_match(): Empty regular expression in /.../PEAR/ITX.php on line 296
Notice: Undefined property: siteMainTemplate::$parseCurrentBlock in /.../classes/classdefs.php on line ...
This clearly points towards a faulty class definition right there.
Can someone advice on what I am doing wrong here?
Edit: Typo corrected in second code snippet - the second function meant to complete the example as "showSite()", not as a second constructor function.
Some more tests revealed the correct line is indeed:
$this->template = new HTML_Template_ITX("../templates/");
After that, the warnings posted in the question were simply covering up further inconsistencies rooted in handling of the "template directory" handed over.

Using $this when not in object context - ZendFramework

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.

[swagger-codegen][php-symfony] No entrypoint in generated code?

I am attempting to generate a php-symfony server stub using swagger-codgen but I am not getting an index.php (or app.php or anything like that).
When I generate a server stub using the php-silex preset I get an index.php. If I google "silex .htaccess" I can easily find the mod_rewrite directives to get it working. However, when I generate code using the php-symfony preset I cannot find the entry point.
I attempted to combine the php-silex and php-symfony generated codebases (since silex is a symfony project) like so:
(index.php):
use Swagger\Server\Controller\GetTypesListController;
$app->GET('/image/list/types', function (Application $app, Request $request) {
$tlc = new GetTypesListController();
$response = $tlc->getTypeListAction($request);
return $response;
//new Response('How about implementing getTypeList as a GET method ?');
});
However, then I get errors like:
"Fatal error: Uncaught Error: Call to a member function getApiHandler() on null"
I modified the getApiHandler function of my generated GetTypesListController to instantiate the apiServer variable:
public function getApiHandler()
{
if (!isset($this->apiServer)){
$this->apiServer = new \Swagger\Server\Api\ApiServer();
}
return $this->apiServer->getApiHandler('getTypesList');
}
but this just gave me another error:
Undefined property: Swagger\Server\Controller\GetTypesListController::$container
(referencing SymfonyBundle-php/Controller/Controller.php on line 149)
I am obviously doing something wrong. I would really appreciate being pointed in the right direction.
Edit: I do realize that I'm not supposed to call controllers inside controllers.. I assume that I'm not supposed to combine these two generated codebases at all.

PHP CallFire SDK - Authentication and Request/Response problems

Disclaimer: php novice student working really hard here.
I'm trying to build an app that interacts with the CallFire API.
At this point all I'm trying to achieve is establishing a communication with the API and succesfully getting a request/response cycle going.
I have installed the Callfire SDK dependency with Composer on my XAMPP local server. I want to get this very basic example running: it instantiates the API, sends a request and parses the response.
<?php
use CallFire\Api\Rest\Request;
require 'vendor/autoload.php';
$client = CallFire\Api\Client::Rest("<api-login>", "<api-password>", "Broadcast"); //I'm using my valid CallFire API keys here.
$request = new Request\QueryBroadcasts;
$response = $client->QueryBroadcasts($request); //LINE 10
$broadcasts = $client::response($response);
foreach($broadcasts as $broadcast) {
var_dump($broadcast);
}
I put this code in a index.php file and when I run it in my browser I get:
Notice: Trying to get property of non-object in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 39
Notice: Trying to get property of non-object in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 39
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Response type not recognized' in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php:50
Stack trace: #0 C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\AbstractClient.php(59): CallFire\Api\Rest\Response::fromXml(false)
#1 C:\xampp\htdocs\cfireapi1\index.php(10): CallFire\Api\Rest\AbstractClient::response(false)
#2 {main} thrown in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 50
Line 59 in AbstractClient.php is
public static function response($data, $type = self::FORMAT_XML)
{
if (is_string($data) && strlen($data) === 0) {
return true; // Many operations return an empty string for success
}
switch ($type) {
case static::FORMAT_XML:
return AbstractResponse::fromXml($data); // LINE 59
case static::FORMAT_JSON:
return AbstractResponse::fromJson($data);
}
throw new InvalidArgumentException("Type must be 'xml' or 'json'");
}
And lines 39 and 50 in Response.php are
public static function fromXml($document)
{
$document = static::getXmlDocument($document);
// return print_r($document);
switch ($document->firstChild->nodeName) { // LINE 39 error for this line is Notice: Trying to get property of non-object
case 'r:ResourceList':
return Response\ResourceList::fromXml($document);
case 'r:Resource':
return Response\Resource::fromXml($document);
case 'r:ResourceReference':
return Response\ResourceReference::fromXml($document);
case 'r:ResourceException':
return Response\ResourceException::fromXml($document);
}
throw new UnexpectedValueException('Response type not recognized'); // LINE 50: this is what throws the fatal error exception
}
Line 10 is highlighted in the example code.
So I know that the API is sending back an response with an unidentified type. Why, if I'm literally using the SDK and one of the basic examples in the documentation? How can I check the response that I'm getting to see what it is exactly? I tried printing the response with r_print inside the Response.php file but I got a "1" printed in the browser. Which makes not sense to me. Is it because I'm running this in the browser?
SOLVED: Somehow running the program in the localhost environment was causing the problem. Everything worked once I uploaded the application into my server.

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