I'm trying to export a CrystalReports report using PHP and save it to the server. I'm using PHP's COM class. I'm not able to get it to work, it just hangs on Export(true). My code is as follows:
$obj = new COM('CrystalReports13.ObjectFactory.1') or die('1');
$get = $obj->CreateObject('CrystalDesignRunTime.Application') or die('2');
$report = $get->OpenReport('C:\\xampp\htdocs\\crystal\\Packslip_RepSrv.rpt', 1) or die('3');
try {
$report->Database->ConvertDatabaseDriver('crdb_odbc.dll', false);
$report->Database->Tables(1)->SetLogOnInfo('REMOVED', 'REMOVED', 'REMOVED','REMOVED');
$report->EnableParameterPrompting = 0;
$report->DiscardSavedData;
$report->ParameterFields->Item(1)->AddCurrentValue('9455');
$report->ExportOptions->DiskFileName='report.pdf';
$report->ExportOptions->FormatType=31;
$report->ExportOptions->DestinationType=1;
$report->Export(false);
$report = null;
$get = null;
$obj = null;
print "<embed src=\"report.pdf\">";
} catch(Exception $e) {
var_dump($e);
}
Does anything above look out of the usual? To my knowledge I'm doing everything properly, but it hangs on the $report->Export(false); part.
Don't use CrystalDesignRunTime
Use CrystalRunTime
Related
php foreach - try catch, I don't know why I get 500 Error.
in my code, I used this way to call activeMQ, but I will get 500 Error. I don't know what's happen.
This code only run to $url = "tcp://".$url; cannot run to uder try{}. Where is issues?
$stomp = null;
$mqUser='aaa';
$mqPasswd='aaa';
foreach ($urls as $url) {
$url = "tcp://".$url;
//echo $url." stomp status: null;<br />";
try {
$stomp = new Stomp($url);
} catch(Exception $e) {
}
if ($stomp) {
break;
}
}
I am running a web app to get a list of users with Google API PHP Client Library 2.0.3. and save them to a CSV file and at the same time, I am tracking the process on screen. The code I am using is the following:
$pageToken = null;
$optParams = array(
"customer" => "my_customer",
"maxResults" => 500,
"orderBy" => "email",
"sortOrder" => "ASCENDING"
);
try {
$usernum = 0;
do {
if ($pageToken){
$optParams['pageToken'] = $pageToken;
}
$results = $service->users->listUsers($optParams);
$pageToken = $results->getNextPageToken();
$users = $results->getUsers();
foreach ($users as $user) {
$usernum++;
$csvfileusers = array($user->getPrimaryEmail());
fputcsv($savecsv, $csvfileusers);
$usersemails = $user->getPrimaryEmail();
print "<li>".$usernum." - <font color='#9dd7fb'>".$usersemails."</font></li>";
}
} while($pageToken);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
Everything works fine. The problem is that from time to time I am getting { error: { errors: [ { domain: global, reason: backendError, message: Service unavailable. Please try again } ], code: 503, message: Service unavailable. Please try again } }
I know this means that I am sending requests to Google Server too fast hence I need to implement an exponential backoff solution. My problem is that I don't know how to do that. Would someone be kind enough to provide me an example on how to do that using the PHP Client Library? I know that I might be able to figure this out at the long term but if I can get some help I will greatly appreciate it.
Unfortunately, the documentation is lacking for the actual backoff implementation. However, the Google_Task_Runner class outlines the backoff implementation process. You can see how it does it here.
However, based on what you're doign you don't actually want to implement a exponential backoff procedure in general networking terms. You're really wanting to just throttle the request so you don't slam the API. Depending on how many $pageToken you're iterating over, you could just do a sleep before doing the next while iteration.
Additionally, what does $pageToken = $results->getNextPageToken(); become after one request? Becuase you're setting that from the response rather than decrementing it programatically, which may be causing an infinute loop or something of that nature.
So after 20 days of trying and investigating and thanks to the information provided by #kyle, I came up with this exponential backoff solution.
$attemptNum = 1; // retry attempt
$expBackoff = false; // exponential backoff rety indicator
do {
if($attemptNum <= 4) {
try {
$usernum = 1;
do {
if ($pageToken){
$optParams['pageToken'] = $pageToken;
}
$results = $service->users->listUsers($optParams);
$pageToken = $results->getNextPageToken();
$users = $results->getUsers();
foreach ($users as $user) {
$csvfileusers = array($user->getPrimaryEmail());
fputcsv($savecsv, $csvfileusers);
$usersemails = $user->getPrimaryEmail();
print "<li>".$usernum." - <font color='#9dd7fb'>".$usersemails."</font></li>";
$usernum++;
}
} while($pageToken);
} catch (Exception $e) {
$err503ReasonA = "Service unavailable"; // Service unavailable.
$err503ReasonB = "Backend Error"; //Backend Error
$exception = $e->getMessage();
if(strpos($exception, $err503Reason) !== false || strpos($exception, $err503ReasonB) !== false){
$expBackoff = true;
$sleeptime = $retrynum * 1; //retrynum * seconds
sleep($sleeptime);
$retrynum++;
} else {
$expBackoff = false;
print "There was an error -> $exception";
}
}
} else {
$expBackoff = false;
}
} while ($expBackoff);
I have been trying this solution for two days now and it has worked like a charm. Hopefully this might be helpful for someone else. I´m happy now. :)
so I tried to get a fix for this earlier but I think we were all going in the wrong direction. I'm trying to check two servers to make sure that at least one of them are active to make a call to. The service provides me with a page for each that simply has "OK" under a div with id="server_status". When I try to loadHTMLFile into a variable, it returns true, but I can never pull the element I need from it. After doing some output testing with saveHTML(), it appears that the variable holding the DOMDocument is empty. Here's my code:
servers = array('tpeweb.paybox.com', // primary URL
'tpeweb1.paybox.com'); // backup URL
foreach($servers as $server){
$doc = new DOMDocument();
$doc->validateOnParse = true;
$doc->loadHTMLFile('https://'.$server.'/load.html');
$server_status = "";
$docText = $doc->saveHTML();
if($doc) {
echo "HTML should output here: ";
echo $docText;
}
if(!$doc) {
echo "HTML file not loaded";
}
$element = $doc->getElementById('server_status');
if($element){
$server_status = $element->textContent;
}
if($server_status == "OK"){
// Server is up and services are available
return array(true, 'https://'.$server.'/cgi/MYchoix_pagepaiement.cgi');
}
}
return array(false, 'e404.html');
All I get as output is "HTML should output here: " twice, and then it returns the array at the bottom. This is the code that they provided:
$servers = array('tpeweb.paybox.com', // primary URL
'tpeweb1.paybox.com'); // backup URL
$serverOK = "";
foreach($servers as $server){
$doc = new DOMDocument();
$doc->loadHTMLFile('https://'.$server.'/load.html');
$server_status = "";
$element = $doc->getElementById('server_status');
if($element){
$server_status = $element->textContent;
}
if($server_status == "OK"){
// Server is up and services are available
$serverOK = $server;
break;
}
// else : Server is up but services are not available .
}
if(!$serverOK){
die("Error : no server found");
}
echo 'Connecting to https://'.$server.'/cgi/MYchoix_pagepaiement.cgi';
This also seems to be having the same problem. Could it be something with my PHP configuration? I'm on version 5.3.6.
Thanks,
Adrian
EDIT:
I tried it by inputting the HTML as a string instead of calling it to the server and it worked fine. However, calling the HTML into a string to use in the PHP function results in the same issue. Fixes??
I'm trying to get rid of a document by _id, but the PHP process stops without error, and no error in access.log, and no output if i assign a variable to remove(). Using mongodb:
version v2.4.9
$connection = new MongoClient();
if($connection == NULL) {
return "some msg";
}
$db = $connection->main;
$collection = $db->users;
$document = $collection->findOne(array('email' => $user->email));
if($document == NULL) {
// do some stuff, works fine.
} else {
$id = $document["_id"];
echo "id=".$id // outputs: 5469a22600a8ebe8418b4567
if($document["confirm"] != "true") {
echo "confirmed not true" // outputs fine
$collection->remove(array('_id' => new MongoId($id)), true);
echo "hello!"; // never occurs
}
}
EDIT: I tried this, and no output:
try {
$collection->remove(array('_id' => new MongoId($id)), true);
} catch(Exception $e) {
echo $e->getMessage();
}
EDIT: I found an answer, but I don't understand the answer because every example I've seen includes the true argument. So I won't self answer this question and leave it up to an expert.
This doesn't work:
$collection->remove(array('_id' => new MongoId($id)), true);
This doesn't work:
$collection->remove(array('_id' => new MongoId($id)), false);
This works:
$collection->remove(array('_id' => new MongoId($id)));
I can explain your problem but I cannot explain why you are not seeing errors, there must be something odd in your PHP setup.
Anyway, as to why your boolean only value for the second parameter does not work: it is because the PHP driver expects an array: http://php.net/manual/en/mongocollection.remove.php the examples you worked from were either incredibly old (Since the change happened 1.0.5 http://php.net/manual/en/mongocollection.remove.php#refsect1-mongocollection.remove-changelog) or incorrect.
I'm parsing RSS feeds and have a (sub)function that looks like this:
function nSpace($nSp, $type, $i){
$namespaces = $i->getNameSpaces(true);
$exp = explode(':', $nSp);
try{
$nameSp = $i->children($namespaces[$exp[0]]);
$item[$type] = (string)$nameSp->$exp[1];
return $item[$type];
}
catch (Exception $e) { }
}
I'm trying to retrieve the namespace value, and will pass common RSS feed namespaces like "dc:date" or "content:encoded" for example as $nSp. The function works fine should the namespace exist in the XML, however the try{} is producing an 'Undefined Index' error in the first line when it doesn't.
Personally I'd rather run an isset($i->children($namespaces[$exp[0]])){} check instead of the try/catch, as I'm more familiar with that workflow, however this doesn't work (get a 'Can't use return value' error).
Few questions:
Shouldn't the try{} not produce error messages?
Is try/catch the best way?
Is there a way to do it with an if() instead?
Thanks.
Update:
Here is the (abreviated) call/usage for this function:
$rawFeed = file_get_contents($url);
try { $rss = new SimpleXMLElement($rawFeed); } catch (Exception $e) { }
foreach ($rss->channel->item as $i) {
$item['link'] = isset($i->link) ? (string)$i->link : nSpace('dc:link', 'link', $i);
$item['dateRaw'] = isset($i->pubDate) ? (string)$i->pubDate : nSpace('dc:date', 'date', $i);
// etc...
}