I am using the php method getTraceAsString() which will pring out my error messages like:
#0 /var/www/wordpress/wp-content/themes/Aisis-Framework/AisisCore/Template/Builder.php(147): AisisCore_Template_Builder->_render_template_array(Array, 'navigation') #1 /var/www/wordpress/wp-content/themes/Aisis-Framework/index.php(3): AisisCore_Template_Builder->render_view('navigation') #2 /var/www/wordpress/wp-includes/template-loader.php(47): include('/var/www/wordpr...') #3 /var/www/wordpress/wp-blog-header.php(16): require_once('/var/www/wordpr...') #4 /var/www/wordpress/index.php(17): require('/var/www/wordpr...') #5 {main}
As you can see is one giant string. The example they gave prints out the trace as separate lines. is there something I have to do to get that?
It's a string with line breaks, not HTML. Look at the source code of your page.
Either wrap the output in <pre></pre> tags or replace the line break with HTML line breaks.
preg_replace("/\n/", '<br>', $trace);
I have made some function for ZF2 error log , along with formatting of,
$e->getTraceAsString();
Hope this might help you.
public function Errorcatch(\Exception $e) {
$log = "<div class='error_main'>";
$log.= "<br /><b>Error Time :</b>" . date('Y-m-d H:i:s A');
$log.= "<br /><b>Error Code :</b>" . $e->getCode();
$log.= "<br /><b>Error Message :</b>" . $e->getMessage();
$log.="<br /><b>Error File :</b>" . $e->getFile();
$log.="<br /><b>Error File Line :</b>" . $e->getLine();
$log.="<br /><b>Error Trace :</b><br />" . preg_replace("/\n/", '<br>', $e->getTraceAsString());
$log.= "</div>";
$this->getServiceLocator()->get('Zend\Log')->info($log); // This line write the log file.
}
Related
I'm trying to do some very basic parsing of XML data but am failing miserably.
I have a metadata.xml file as such:
<?xml version="1.0" encoding="UTF-8" ?>
<metadata>
<page>
<filename>products.php</filename>
<title>Best selection of products in Anytown, USA</title>
<description>We sell quality products</description>
</page>
<page>
<filename>services.com</filename>
<title>Great services anywhere within Anytown</title>
<description>Our services are pretty good</description>
</page>
</metadata>
I'm attempting to get a result for a specific XML entry using the code below:
<?php
$str = simplexml_load_file("metadata.xml") or die("Couldn't load file");
$data = new SimpleXMLElement($str);
// Find the element with specific filename
$nodes = $data->xpath('//metadata/page/filename[.="products.php"]/parent::*');
$result = $nodes[0];
echo "Title: " . $result->title . "\n";
echo "Description: " . $result->description . "\n";
?>
This results in an error:
Warning: SimpleXMLElement::__construct(): Entity: line 4: parser error : Start tag expected, '<' not found in /var/www/html/php_xml_test.php on line 10
Fatal error: Uncaught Exception: String could not be parsed as XML in /var/www/html/php_xml_test.php:10 Stack trace: #0 /var/www/html/php_xml_test.php(10): SimpleXMLElement->__construct('\n\t\n\t\n') #1 {main} thrown in /var/www/html/php_xml_test.php on line 10
If I load the content of the XML file right into the php file everything works fine.
I've read through a bunch of related posts here but can't figure out where I'm going wrong.
Thanks!
As per http://php.net/manual/en/simplexmlelement.construct.php I adjusted the code like that:
<?php
$data = new SimpleXMLElement('metadata.xml', 0, TRUE);
// Find the element with specific filename
$nodes = $data->xpath('//metadata/page/filename[.="services.php"]/parent::*');
$result = $nodes[0];
echo "Title: " . $result->title . "\n";
echo "Description: " . $result->description . "\n";
?>
I use function system() to run system process.
$buff = system('python excel.py ' . $handle->file_dst_pathname, $retval);
It displays messages in line without separations.
In Python I use this line to print data:
print "# %d - article \"%s\" was inserted!" % (i, article)
Precede the output of system(...); with echo "<pre>"; and follow with echo "</pre>";
Or shorter:
$buff = "<pre>" . system('python excel.py ' . $handle->file_dst_pathname, $retval) . "</pre>";
You could also change all the "\n" chars of your output into "<br/>".
Using the following code:
// get the file contents
$file = new Google_Service_Drive_DriveFile();
$fileId = 'The File ID';
try {
echo 'try part <br>';
$result3 = $service->files->get($fileId);
echo 'Contents: ' . $result3->getContent().'<br>';
echo "Title: " . $result3->getTitle() . '<br>';
echo "Description: " . $result3->getDescription() . '<br>';
echo "MIME type: " . $result3->getMimeType() . '<br>';
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage();
};
Everything works except for this line:
echo 'Contents: ' . $result3->getContent().'<br>';
In the drive.php file in the API, there is a getContent() function, but it's not working. How do I get the content out of the file?
If you refer to the Google documentation at https://developers.google.com/drive/v2/reference/files/get you'll see that there are two forms of the Get method. The default fetches the metadata (eg. title, description, etc), whereas alt=media fetches the content. Alternatively, you can also fetch the content via the downloadUrl property. I don't know if the PHP library exposes the alt=media option (I don't like libraries), but the downloadUrl approach has an example on the page I linked, just scroll down and click "php"
I'm trying to download all files from a specific directory in my Dropbox to a directory on my web server, if a file of the same name doesn't already.
I'm using this SitePoint Tutorial as the guide.
The problem I'm having is that the Dropbox API returns an Invalid Signature error when it gets to certain files. I can't see anything odd or special about these files. Some are PDFs, some are PNGs.
I've read this could relate to OAuth, but I can't figure it out.
I believe errors at the getFile() within the foreach(), depending on the file:
<?php
require_once "bootstrap.php";
if (!isset($access_token)) {
header("Location: authorize.php");
exit;
}
try {
$session = new DropboxSession($config["dropbox"]["app_key"], $config["dropbox"]["app_secret"], $config["dropbox"]["access_type"], $access_token);
$client = new DropboxClient($session);
$path = "/Stuff/folder_to_download";
$home = $client->metadata($path);
foreach($home['contents'] as $item) {
echo "<pre>" . print_r($item, true) . "</pre>";
$path = $item['path'];
if ( $item['is_dir'] == '1' )
continue;
// where to save it
$dest = $config["app"]["datadir"] . "/" . basename($path);
if ( file_exists($dest) ) {
echo "<p>File <code>". $dest . "</code> already exists. Skipped.</p>";
continue;
}
// Download file
if ($file = $client->getFile($path, $dest)) {
if (!empty($dest)) {
unset($file["data"]);
echo "<p>File saved to: <code>" . $dest . "</code></p>";
}
}
}
}
catch (Exception $e) {
echo "<strong>ERROR (" . $e->getCode() . ")</strong>: " . $e->getMessage();
}
?>
The authorize.php file looks like this. bootstrap.php looks like this.
Update 1: I've still not been able to resolve this error. Is there a different set of Dropbox/ PHP code I could use instead of this sitepoint one?
Here's the raw error when I remove the try/catch:
Fatal error: Uncaught exception 'Exception' with message 'Invalid signature.' in /myapp/lib/dropbox/rest.php:162
Stack trace:
#0 /myapp/lib/dropbox/rest.php(85): DropboxRESTClient->request('https://api-con...', 'GET', NULL, NULL, NULL, true)
#1 /myapp/lib/dropbox/session.php(278): DropboxRESTClient->get('https://api-con...', NULL, true)
#2 /myapp/lib/dropbox/client.php(106): DropboxSession->fetch('GET', 'https://api-con...', '/files/dropbox/...', Array, true)
#3 /myapp/download.php(54): DropboxClient->getFile('/Stuff/Droppod/...', '/myapp/...')
#4 {main}
thrown in /myapp/lib/dropbox/rest.php on line 162
The lib/dropbox/rest.php file is on GitHub for reference.
"I've still not been able to resolve this error. Is there a different set of Dropbox/ PHP code I could use instead of this sitepoint one?"
As to that question, you could try using the official Dropbox PHP SDK: http://dropbox.com/developers/core/sdks/php
Ran across this same issue, has to do with some folders/files that have special characters in the path like '&' or '$' and '(' or ')'. Recommend that you encode the path BEFORE you make GET queries to the api otherwise you'll end up with a url like this
https://api.dropbox.com/1/metadata/dropbox/Photos & Stuff ~ Jan 2013
instead of properly encoded like this
https://api.dropbox.com/1/metadata/dropbox/Photos%20%26%20Stuff%20~%20Jan%202013
edit: Here is the code (javascript) that I used for encoding the path (works like a charm)
encodeURIComponent(p)
.replace(/%2F/g, '/')
.replace(/\)/g, '%29')
.replace(/\(/g, '%28');
I am trying to convert .docx files to pdf using PHP on a Windows server. I tried several of the solutions from other posts, including phpdocx (which does a very poor conversion that doesn't keep any formatting) and php's Com object. I only have Office 2003, so there is no pdf converter available using Com.
I thought of using OpenOffice/LibreOffice, but haven't found any information about installing and using Com for these on a windows server (I know it can be installed, but I can't figure out how to set up Com for it).
Using a webservice is not an option due to the data on the forms (they have to remain on our server). This means that Zend Framework cannot be used.
Any suggestions would be helpful, or information about using Com with Open Office.
I was finally able to get this working. OUr problem was that Word 2003 didn't have a PDF converter in it. We ended up using a trial version of Office 2010 for now (assuming that everything works correctly, we will purchase the full version). Word 2007 would have worked also. Below is the code I used to get this working:
//Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application') or die('MS Word could not be loaded');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0;
$word->DisplayAlerts = 0;
try{
$doc = $word->Documents->Open(DOC_LOCATION. 'test_image.docx');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "doc opened";
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "test_image.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "created pdf";
$word->Quit();
$word = null;