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.
Related
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.
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.
When we install our payment plugin from zip package, it doesn't work and shows a fatal error in our log files. We trace the error in the vmplugin.php file at line 69 "$this->loadJLangThis($filename); //TODO remove this is not allowed here, else systemplugins derived from vmPlugin throw errors on a multilanguage pages".
Is it possible that this function has not been removed by accident (as marked as TODO in the line's comment)?
We can trace further the error and see that the problem is in vmplugin.php in the function loadJLang($fname,$type,$name) at line 108 $tag = vmLanguage::$currLangTag;
//$jlang = JFactory::getLanguage();
//$tag = $jlang->getTag();
//if(empty($tag)) {
$tag = vmLanguage::$currLangTag;
//}
where $tag appears to be empty.
Can anybody help me with this problem, or tell me why $tag is empty? I would really appreciate any help.
Edit: The error log is:
PHP Fatal error: Call to a member function load() on a non-object in /administrator/components/com_virtuemart/plugins/vmplugin.php on line 124, referer: /administrator/index.php?option=com_installer
I have a wordpress custom import plugin which was working fine, but for some reason it is not working in a new wordpress install Dropbox link. Plugin looks for two feed files barneys_feed.txt & barneyswarehouse_feed.txt (included with plugin) which should be configured in plugin settings page Detailed Description. On activation a custom table has to be created but not creating and on product run following is displayed in log:
[Tue Nov 22 18:28:08.931419 2016] [:error] [pid 11484] [client 38.140.212.19:64906] PHP Catchable fatal error: Argument 1 passed to sjr\\product_import\\SJR_Product_Import::get_line() must be an instance of SplFileObject, null given, called in /nas/content/live/testwindow/wp-content/plugins/sjr-product-import/includes/class-sjr-product-import.php on line 334 and defined in /nas/content/live/testwindow/wp-content/plugins/sjr-product-import/includes/class-sjr-product-import.php on line 372, referer: http://testwindow.wpengine.com/wp-admin/options-general.php?page=sjr_product_import_settings
Not able to reach out plugin developers. Please help. Thanks.
Update:
Following is the error i am getting now.
PHP Fatal error: Uncaught TypeError: Argument 1 passed to sjr\\product_import\\SJR_Product_Import::get_line() must be an instance of SplFileObject, null given, called in /nas/content/live/testwindow/wp-content/plugins/sjr-product-import/includes/class-sjr-product-import.php on line 334 and defined in /nas/content/live/testwindow/wp-content/plugins/sjr-product-import/includes/class-sjr-product-import.php:372\nStack trace:\n#0 /nas/content/live/testwindow/wp-content/plugins/sjr-product-import/includes/class-sjr-product-import.php(334): sjr\\product_import\\SJR_Product_Import->get_line(NULL, 1)\n#1 [internal function]: sjr\\product_import\\SJR_Product_Import->csv_to_database()\n#2 /nas/content/live/testwindow/wp-includes/plugin.php(600): call_user_func_array(Array, Array)\n#3 /nas/content/live/testwindow/wp-cron.php(117): do_action_ref_array('sjr_product_imp...', Array)\n#4 {main}\n thrown in /nas/content/live/testwindow/wp-content/plugins/sjr-product-import/includes/class-sjr-product-import.php on line 372, referer: http://testwindow.wpengine.com/wp-cron.php?doing_wp_cron=1480412579.4279830455780029296875
Something I find helpful for debugging these kinds of issues is looking at the stack trace of where your error is. You can print the stack trace at each of the debug_backtrace() at each of the locations you call get_line to see what is on the stack when you get the failed call.
I don't have your project up at this time, but it looks like your $file object is no longer in scope (and therefore null) for this call to get_line
foreach( $lines as $line_item ){
if( $file_name != $line_item['file'] ){
$file_name = $line_item['file'];
$basename = basename( $file_name );
// get columns
$file = new \SplFileObject( $file_name, 'r' );
$file->setFlags( \SplFileObject::READ_CSV );
$file->setCsvControl( "\t", " " );
// get first row with column names
$first_row = $this->get_line( $file, 0 );
} // You lose scope on $file here
$row = $this->get_line( $file, $line_item['line_number'] - 1 ); // <-- This one is the likely cause
You should do two things here:
Define $file before the if statement (likely as $file = null)
Handle the case of still having a null $file object after the if statement and before the problem get_line call.
The error message tells you, that at some point, the get_line function of sjr\product_import\SJR_Product_Import gets instead of an instance of SplFileObject just a null.
I'm guessing the get_line function looks something like this:
public function get_line(SplFileObject $file) { /* do some stuff */ }
What you can either do (assuming you wrote that code on your own), is to change the function to something like this:
public function get_line(SplFileObject $file=null) { /* do some stuff */ }
telling PHP that besides the object, you also accept null as a default value. In that case, you would have to update your function, to check if the given variable is null. You can check this for example with
if (!is_null($file)) {
/* every thing is fine */
} else {
/* there was an error, maybe write it to a error log */
}
A completely guess from my side, as the error states, that the location contains /nas/ I'm assuming, that the PHP code is run on a NAS and has some drives mapped, from which you try to import some stuff periodically, as the import is started by a cron job. Maybe one of those mapped drives somehow lost the connection and therefore PHP wasn't able to read the files. But as I said, that's just a wild guess. To "debug" this, you could check the status of the mapped drives manually when the cron job is run and see, if the problem still happens. Another thing could be, that PHP (or more precise, Apache or Nginx) doesn't have rights to access those files/shares.
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