PHP JasperReports Server API Error - php

I am trying to ultimately run and display reports from a remote Jasper Server in a PHP application. What I am trying to do this with is the jrs-rest-php-client project on github.
The error:
Fatal error: Uncaught exception 'Jaspersoft\Exception\RESTRequestException' with message 'An unexpected HTTP status code was returned by the server' in C:\xampp\htdocs\jrs\vendor\src\Jaspersoft\Tool\RESTRequest.php:409
Stack trace:
#0 C:\xampp\htdocs\jrs\vendor\src\Jaspersoft\Tool\RESTRequest.php(479): Jaspersoft\Tool\RESTRequest->handleError(0, Array, false)
#1 C:\xampp\htdocs\jrs\vendor\src\Jaspersoft\Service\ReportService.php(40): Jaspersoft\Tool\RESTRequest->prepAndSend('https://jasper....', Array, 'GET', NULL, true)
#2 C:\xampp\htdocs\jrs\report.php(30): Jaspersoft\Service\ReportService->runReport('/Reports/Distri...', 'html')
#3 {main} thrown in C:\xampp\htdocs\jrs\vendor\src\Jaspersoft\Tool\RESTRequest.php on line 409
My PHP:
require_once __DIR__ . "/vendor/autoload.php";
use Jaspersoft\Client\Client;
$d = new Client(
"http://jasper.server.com/jasperserver-pro",
"username",
"password",
"organization"
);
$info = $d->serverInfo();
Any ideas?

Looking into the code of RESTRequest.php there are two cases in which this exception is thrown:
A JSON result set with an unknown error code is returned (unknown meaning different from 200 OK)
No JSON result set is returned at all
So I suspect the connection isn't working properly. To find out more, you should catch the exception in your code and evaluate it:
It could look something like this (I'm more familiar with Java):
try {
$d = new Client(
"http://jasper.server.com/jasperserver-pro",
"username",
"password",
"organization"
);
$info = $d->serverInfo();
} catch (RESTRequestException $e) {
echo 'RESTRequestException:';
echo 'Exception message: ', $e->getMessage(), "\n";
echo 'Set parameters: ', $e->parameters, "\n";
echo 'Expected status code:', $e->expectedStatusCodes, "\n";
echo 'Error code: ', $e->errorCode, "\n";
}
If there is still an error, you can check the following:
Can you reach the jasper server from the server where this code is deployed? Sometimes e.g. firewall settings can interfere.
Is the organization called exactly like defined in the properties of the organization in the server? (Open repository / right click on organization / properties / resource-id)

Related

Print receipe with php

This is my scenario:
I have a web server running php
I have a client with a receipt printer
I have a HTML form that retrieves various data from the user and stores them in a database. Then I can access it as I want.
What I want to do is that when I click on a print button, the server sends a command to the printer to print the ticket.
I'm trying to do this with Mike32's escpos-php library but I can't do it.
Here is the php file I made
<?php
require 'vendor/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
$data = "Hello world !";
$printerName = 'Custom';
$clientName = 'LAPTOP-J3MTKIN3';
$connector = new WindowsPrintConnector("\\\\$clientName\\$printerName");
try {
$printer = new Printer($connector);
$printer->text($data);
$printer->cut();
$printer->close();
} catch (Exception $e) {
echo "Erreur : " . $e->getMessage() . "\n";
}
?>
Here are the 2 errors I get:
Warning: preg_match(): Compilation failed: invalid range in character
class at offset 29 in
/var/www/html/vendor/mike42/escpos-php/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php
on line 126
and
Fatal error: Uncaught BadMethodCallException: Printer
'\LAPTOP-J3MTKIN3\Custom' is not a valid printer name. Use local port
(LPT1, COM1, etc) or smb://computer/printer notation. in
/var/www/html/vendor/mike42/escpos-php/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php:155
Stack trace: #0 /var/www/html/test_impression.php(22):
Mike42\Escpos\PrintConnectors\WindowsPrintConnector->__construct() #1
{main} thrown in
/var/www/html/vendor/mike42/escpos-php/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php
on line 155

Why is PDO throwing an ErrorException rather than a PDOException?

I have Sentry keeping track of uncaught exceptions in my PHP application, and I noticed a peculiar uncaught exception from PDO. The code looks like this:
/**
* #return boolean TRUE if the connection to the database worked; FALSE otherwise.
*/
public function verifyDatabase() {
try{
$this->pdo->query('SELECT 1');
return true;
}
catch (\PDOException $e) {
echo 'Lost connection to database: ' . $e->getMessage() . PHP_EOL;
return false;
}
}
This should catch errors like "MySQL server has gone away", and it indeed works on my development machine. However, Sentry recently recorded this error:
ErrorException
PDO::query(): MySQL server has gone away
According to sentry, this was thrown by the $this->pdo->query('SELECT 1'); statement above. Errors like this should have been caught by the try/catch. Why is PDO throwing an ErrorException rather than a PDOException?
I can't reproduce an ErrorException.
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test', ..., ...);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
sleep(20); // during this sleep, I stop my MySQL Server instance.
$result = $pdo->query("SELECT 1");
Output:
Warning: PDO::query(): MySQL server has gone away
Warning: PDO::query(): Error reading result set's header
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Stack trace:
#0 /Users/bkarwin/Documents/SO/pdo.php(8): PDO->query('SELECT 1')
This shows it throws a PDOException when the server has gone away, not an ErrorException.
Tested with MySQL 5.6.37 and PHP 7.1.23.
I wonder if the code you show in your question is actually the code that is deployed and throwing the exception to Sentry. Perhaps you have some code like:
catch (\PDOException $e) {
throw ErrorException($e->getMessage());
}
Either in your verifyDatabase() function, or else in the code that calls verifyDatabase(). In other words, what does your app do when verifyDatabase() returns false?
Okay, I think I've figured it out. It appears that this is related to a bug in which the PDO MySQL driver emits warnings even when they are supposed to be disabled (See also: this answer). I believe Sentry is then catching these as errors.
I was finally able to replicate, and solve this, by modifying Bill Karwin's test script:
// Initialize the Sentry reporting client
$ravenClient = new \Raven_Client(SENTRY_KEY);
$ravenClient->install();
echo 'Connecting...' . PHP_EOL;
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected. Waiting...' . PHP_EOL;
sleep(20); // during this sleep, I stop my MySQL Server instance.
echo 'Querying...' . PHP_EOL;
try {
$result = $pdo->query("SELECT 1");
}
catch(\PDOException $e) {
echo 'Caught PDOException ' . $e->getMessage() . PHP_EOL;
}
This will print the following:
Connecting...
Connected. Waiting...
Querying...
PHP Warning: PDO::query(): MySQL server has gone away in /home/xxx/src/test.php on line 37
PHP Stack trace:
PHP 1. {main}() /home/xxx/src/test.php:0
PHP 2. PDO->query() /home/xxx/src/test.php:37
Caught PDOException SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Done.
And Sentry will record an ErrorException on the query() line.
I was able to solve this issue by implementing the "solution" posted by jferrer on the PHP bug report.
// Convert NOTICE, WARNING, ... in Exceptions
$convertErrorToException = function ($level, $message, $file, $line){
throw new ErrorException($message, 0, $level, $file, $line);
};
// The $previousErrorHandler will contain Sentry's handler
$previousErrorHandler = set_error_handler($convertErrorToException);
try {
$result = $pdo->query("SELECT 1");
}
catch(\PDOException $e) {
echo 'Caught PDOException ' . $e->getMessage() . PHP_EOL;
}
catch(\ErrorException $e) {
echo 'Caught ErrorException ' . $e->getMessage() . PHP_EOL;
}
// Restore Sentry as the default handler
set_error_handler($previousErrorHandler);
This results in just the ErrorException being thrown and caught:
Connecting...
Connected. Waiting...
Querying...
Caught ErrorException PDO::query(): MySQL server has gone away
Done.

Why is my twitter-api-php not working? [duplicate]

This is the first time I use parse.com php SDK and I try to execute the following code
<?php require '../autoload.php';
use Parse\ParseObject;
use Parse\ParseClient;
ParseClient::initialize( "Zsr...", "BzM..", "D..." );
$gameScore = new ParseObject("GameScore");
$gameScore->set("score", 1337);
$gameScore->set("playerName", "Sean Plott");
$gameScore->set("cheatMode", false);
try {
$gameScore->save();
echo 'New object created with objectId: ' . $gameScore->getObjectId();
} catch (ParseException $ex) {
// Execute any logic that should take place if the save fails.
// error is a ParseException object with an error code and message.
echo 'Failed to create new object, with error message: ' + $ex->getMessage();
}
?>
But I get that error
Fatal error: Uncaught exception 'Parse\ParseException' with message 'SSL certificate problem: unable to get local issuer certificate' in /opt/lampp/htdocs/parse/src/Parse/ParseClient.php:250 Stack trace: #0 /opt/lampp/htdocs/parse/src/Parse/ParseObject.php(925): Parse\ParseClient::_request('POST', '/1/classes/Game...', NULL, '{"score":1337,"...', false) #1 /opt/lampp/htdocs/parse/src/Parse/ParseObject.php(836): Parse\ParseObject::deepSave(Object(Parse\ParseObject), false) #2 /opt/lampp/htdocs/parse/src/hola.php(11): Parse\ParseObject->save() #3 {main} thrown in /opt/lampp/htdocs/parse/src/Parse/ParseClient.php on line 250
The code it's the tutorial code, iI haven't changed anything anyone knows what's the problem?
I am also getting same issue. Now I resolve using some other forums answer.
Open your ParseClient.php and find:
curl_init();
And after that add line add:
curl_setopt($rest, CURLOPT_SSL_VERIFYPEER, false);
It will work.

Basic PHP / SOAP error, can't debug

I'm currently having a really frustrating time with some really simple SOAP / PHP at the moment. I've spent about a week trying EVERYTHING I can think of, on multiple different servers with different versions of PHP and they all still throw the same error. Here's the code:
function test() {
$client = new SoapClient('http://xxx', array("login" => "sandbox", "password" => "password"));
print_r($client->__getFunctions());
$ap_param = array();
// it dies here. CheckServiceAvailable is a valid function returned in __getFunctions()
$result = $client->__soapcall('CheckServiceAvailable', $ap_param);
if (is_soap_fault($result)) {
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
}
It faults before the error catching can capture anything. Apache logs show the same fault as below:
Fatal error: Uncaught SoapFault exception: [s:Processing error] in C:\soap.php:13 Stack trace: #0 C:\soap.php(13): SoapClient->__soapCall('CheckServiceAva...', Array) #1 C:\soap.php(23): test() #2 {main} thrown in C:\soap.php on line 13
Without being able to catch the fault I'm totally stuck as to what to do. I don't really want to try nusoap.
Any ideas?
use:
try
{
$result = $client->__soapcall('CheckServiceAvailable', $ap_param);
}
catch (SoapFault $e)
{
echo "Cannot call method CheckServiceAvailable: {$e->getMessage()}";
}

Unable to delete phone numbers in Twilio REST API in PHP

Hi I keep geting the following error when I try to delete a number from my twilio subaccount using REST API in PHP
my code is;
$number = $twClient->account->incoming_phone_numbers->get($number_Sid);
$twClient->account->incoming_phone_numbers->delete($number->sid);
The error that I am getting is;
[22-Aug-2013 09:40:17 UTC] PHP Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The requested resource was not found' in C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php- 732e6f6\Services\Twilio.php:226
Stack trace:
#0 C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php- 732e6f6\Services\Twilio.php(145): Services_Twilio->_processResponse(Array)
#1 C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php-732e6f6\Services\Twilio.php(179): Services_Twilio->_makeIdempotentRequest(Array, '/2010-04-01/Acc...', 1)
#2 C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php-732e6f6\Services\Twilio\ListResource.php(71): Services_Twilio->deleteData('/2010-04-01/Acc...', Array)
#3 C:\Program Files (x86)\Zend\Apache2\htdocs\testers\web2call\application\controllers\clientphonenos_controller.php(518): Services_Twilio_ListResource->delete('PN397fc000ce6f8...')
#4 [internal function]: ClientPhoneNos_controller->data_form('delete', '+14139926551_AC...')
#5 C:\Program Files (x86)\Zend\Apache2\htdocs\system\core\Cod in C:\Program Files (x86)\Zend\Apache2\htdocs\twilio-twilio-php-732e6f6\Services\Twilio.php on line 226
If you have already successfully deleted the phone number, Twilio will return a 404 Not Found to you, which the PHP library will interpret as a RestException. You can only delete the phone number one time :)
You can override exception by using this syntax:
try
{
// do something that can go wrong
}
catch (Exception $e)
{
throw new Exception( 'Something really gone wrong', 0, $e);
}
Here's another live sample for sending messages with an exception catcher:
<?PHP
require "Services/Twilio.php";
// Set our AccountSid and AuthToken from twilio.com/user/account
$AccountSid = "{ACCOUNTSID}";
$AuthToken = "{AUTHTOKEN}";
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
/* Your Twilio Number or Outgoing Caller ID */
$from = '2126404004';
$people = array("212-716-1130");
$body = "Enter your text message here";
$errorIds = array(); //user ids array which had broken phones
foreach ($people as $to) {
try
{
$client->account->sms_messages->create($from, $to, $body);
echo "Sent message to: $to \n <br>";
}
catch (Exception $e)
{ //on error push userId in to error array
$count++;
array_push($errorIds, $to);
}
}
print_r($errorIds);
?>
Without catching the exception, the script will die with an error like:
<br>PHP Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The message From/To pair violates a blacklist rule.' in /var/www/vhosts/httpdocs/twilio/Services/Twilio.php:149

Categories