I am attempting to use https://github.com/amzn/amazon-instant-access-sdk-php for amazon instant access via php. I am not super familiar with phar files; but am starting to figure it out.
I included https://github.com/amzn/amazon-instant-access-sdk-php/blob/master/phar-stub.php in a file as shown below. (Modified a bit as I renamed phar file)
Phar::mapPhar('amazon-instant-access-sdk-php.phar');
require_once 'phar://amazon-instant-access-sdk-php.phar/vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$classLoader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$classLoader->registerNamespaces(array(
'Amazon' => 'phar://amazon-instant-access-sdk-php.phar/src',
'Psr' => 'phar://amazon-instant-access-sdk-php.phar/vendor/psr/log',
'Monolog' => 'phar://amazon-instant-access-sdk-php.phar/vendor/monolog/monolog/src'
));
$classLoader->register();
return $classLoader;
__HALT_COMPILER();
Then when I do in another file:
require_once('amazon-instant-access-phar-stub.php');
I get error:
Fatal error: Uncaught exception 'PharException' with message 'internal corruption of phar "/Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php" (truncated manifest at stub end)' in /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php:17 Stack trace: #0 /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php(17): Phar::mapPhar('amazon-instant-...') #1 /Applications/MAMP/htdocs/phppos/amazon_link_account.php(3): require_once('/Applications/M...') #2 {main} thrown in /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php on line 17
I have tried re-downloading phar file and running different versions of php (5.3, 5.6)
I am not sure what is causing this error.
You could directly download the .phar file from the release.
If you have the .phar file, you don't need the source code from github. The source code on github is used to compile the .phar file.
In your php code, you could do something like this:
<?php
//import the phar file directly like you did
require_once('amazon-instant-access-sdk-php.phar’);
//before you initiate the class, you need to use the right name space:
use Amazon\InstantAccess\Signature as signature;
$credentialStore = new signature\CredentialStore();
?>
This one will work.
If you want to check exactly what's in the .phar file, you could extract it:
php -r '$phar = new Phar("amazon-instant-access-sdk-php.phar"); $phar->extractTo("/tmp/phar/");'
then you could browse the /tmp/phar/ for all the files in the .phar.
Related
I am using this for ocr. This is wrapper for Tesseract ocr(I previosly installed Tesseract itself).
At first i dowloaded it via composer and followed examples in repo and also several posts on SO itself. And all can show is in network tab failed to load response data.
My alternate approaches is that i tried downloading repo itself, then tried to call it from my index.php which for test purposes is situated in same folder where class TesseractOCR is. I tried with images in repo and also tried with black letters on white background images with simple text.
This SO post looks promising, but i'm unsure where OP's file with example code is residing...
use thiagoalessio\TesseractOCR\TesseractOCR;
//or//require "TesseractOCR.php";//if it's in the same dir as test.php
$content = new TesseractOCR('text.png');
$text = $content->run();
echo $text;
Did i miss something obvious? Any help is appreciated.
EDIT1: I tried using in win powershell cli. By putting text.png in directory where tesseract is installed, then calling shell with administrator privileges, subsequently typing in it tesseract text.png output which creates output.txt in same directory with recognized text from that image.
So tesseract its working, my implementation with php wrapper is not.
EDIT2:
Forgot to add, page itself shows:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
Not sure why it happens.
Edit3:
My code:
try{
//use thiagoalessio\TesseractOCR\TesseractOCR;
require "./vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php";
echo $temp;//It's value is set in TesseractOCR.php
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
Value set in $temp variable is visible through state file path, so why TesseractOCRclass itself isn't?
Edit4:
Even if i put absolute path to TesseractOCR.php which holds class, in include statement, it doesn't work.
It throws this error:
Fatal error: Uncaught Error: Class 'TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
This is TesseractOCR.//echoed text from file that holds TesseractOCR class.
Inclusion path:
include ("C:/xampp/htdocs/myocr/vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php");
If i use(which is suggested in repo readme, use thiagoalessio\TesseractOCR\TesseractOCR;, then it throws:
Fatal error: Uncaught Error: Class 'thiagoalessio\TesseractOCR\TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
My question is: How it hits test message, but won't hit TesseractOCR class?
EDIT5:
If i require_once "./vendor/autoload.php"; , it throws:
Fatal error: Uncaught thiagoalessio\TesseractOCR\TesseractNotFoundException: Error! The command "tesseract" was not found. Make sure you have Tesseract OCR installed on your system: https://github.com/tesseract-ocr/tesseract The current $PATH is C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Eddie\AppData\Local\Microsoft\WindowsApps;;C:\Users\Eddie\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Eddie\AppData\Roaming\npm;C:\Users\Eddie\AppData\Roaming\Composer\vendor\bin;C:\Program Files\heroku\bin in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0 C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\ht in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
Btw, i added its patch to env variable:
I solved it!. My problem occurs that i didn't know about autoloaders in php.
Link that helped me is this.
My project structure is this:
Created project folder myocr.
After previous, downloaded latest stable version of Tesseract and installed it.
Depending on your system, you may be required to add value to your system env variable. You need to do that here
then
then
then
I'm assuming it self explanatory with images provided.
Next is getting TesseractOCR via composer:
composer require thiagoalessio/tesseract_ocr
Finally, before using code sample in repo, you need to call autoloader.
Index.php:
require_once('./vendor/autoload.php');//<-This!
use thiagoalessio\TesseractOCR\TesseractOCR;
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
This worked for me. Crucial thing to look after is directory structure.
localhost > myocr > index.php with its code, and after using composer you'll get vendor dir. and it's content. Image path in new TesseractOCR('text1.png'); is directory where both index.php and image are located.
Overview
I'm trying to directly download an Excel spreadsheet created using PHPExcel. I don't have server-level access so I can't install or enable mods (such as the Zip module).
The data is a guestlist for an event.
Code
<?php
if(isset($_GET["event_id"])&&
!empty($_GET["event_id"])){
//Include PHPExcel, Excel2007, classes
require_once("inc/PHPExcel/PHPExcel.php");
require_once("inc/PHPExcel/PHPExcel/Writer/Excel2007.php");
require_once("inc/classes.php");
//Zip not installed - change settings to use local compression
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
//Get event data
$event_id = intval($_GET["event_id"]);
$event = new Event($event_id);
$guests = $event->getGuests();
//Create new PHPExcel object
$spreadsheet = new PHPExcel();
//Add data
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->SetCellValue("B2", "TMC Gateway");
$spreadsheet->getActiveSheet()->SetCellValue("B3", "Event register");
$spreadsheet->getActiveSheet()->SetCellValue("B5", "Name");
$spreadsheet->getActiveSheet()->SetCellValue("C5", "Member/Guest");
$spreadsheet->getActiveSheet()->SetCellValue("D5", "Checkin Time");
foreach($guests as $guest){
if($guest["degree"]=="guest"){
$arr[] = [$guest["name1"]." ".$guest["name2"], "Guest", $guest["checkintime"]];
} else {
$arr[] = [trim($guest["name2"]), "Member", $guest["checkintime"]];
}
}
$currentCell = 6;
foreach($arr as $a){
$spreadsheet->getActiveSheet()->SetCellValue("B$currentCell",$a[0]);
$spreadsheet->getActiveSheet()->SetCellValue("C$currentCell",$a[1]);
$spreadsheet->getActiveSheet()->SetCellValue("D$currentCell",$a[2]);
$currentCell++;
}
//Rename sheet
$spreadsheet->getActiveSheet()->setTitle("TMC Gateway");
//Open writer
$writer = new PHPExcel_Writer_Excel2007($spreadsheet);
//Set headers and force download
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=\"TMC_Gateway_Attendees-".$event_id.".xls\"");
$writer->save("php://output");
//Kill script
exit;
}
Issue
When processing originally and opening the file, I saw this error:
Fatal error: Class 'ZipArchive' not found in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php on line 227
I realised this is probably because the Zip module was either not installed or not enabled, so I followed these instructions at Class 'ZipArchive' not found error while using PHPExcel:
If you don't have ZipArchive installed/enabled for your PHP, and can't enable it yourself, then you can use
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
However, now when opening the file, this error appears:
Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Error zipping files : PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file '/tmppclzip-56df08ee0384c.tmp' in binary write mode' in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php:108
Stack trace:
#0 /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php(278): PHPExcel_Shared_ZipArchive->addFromString('_rels/.rels', '<?xml version="...')
#1 /home/loqui/public_html/doorapp/xls.php(66): PHPExcel_Writer_Excel2007->save('php://output')
#2 {main}
thrown in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php on line 108
Question
As I don't have the Zip module enabled, and seemingly limited permissions in the working folder, how can I make this script download the correctly created Excel file?
If you are going to continue using PCLZIP, I would recommend checking the tmp directory that it is attempting to write to and see what user that directory is assigned to. More than likely Apache does not have write access to that tmp directory and thus is failing to write files inside of it. I am struggling with a similar problem on the ZipArchive front, but if you have sufficient permissions chown -R user:user foldername might alleviate your write issues.
I'm trying to create a tar file using PHP and I'm running into issues.
Using class PharData, ::addFile and ::buildFromDirectory both throw the same exception:
exception 'PharException' with message 'tar-based phar
"C:/Apache24/htdocs/_frames/storage/glacier/archive_cache/9.tar"
cannot be created, contents of file "MVI_4492.MOV" could not be
written, seek failed' in
C:/Apache24/htdocs/_frames/storage/glacier/cron/upload_archives.php:212
Stack trace: #0
C:/Apache24/htdocs/_frames/storage/glacier/cron/upload_archives.php(212):
PharData->addFile('C://Apache24//htd...', 'MVI_4492.MOV') #1 {main}
Thoughts?
You are running out of disk space, during operation. After it - space is restored (because tmp files are filled with data and then cleaned).
If not, then another reason is that you are using 32bit php, which does not support files larger than 2GB
I am developing a custom application in PHP for Mdaemon Mail Server. There are no API samples in PHP, all are in CPP and VB.
I know that we can work with dll in files in PHP using COM and I am also in windows environment. To get everything started I need to load MDUser.dll file.
So far I have downloaded php_com_dotnet.dll and have placed it in extensions directory in my wamp server C:\wamp\bin\php\php5.3.13\ext. I have enabled the extension in php.ini extension=php_com_dotnet.dll
This is my code
$oMDUser = new COM("MDUserCOM.MDUser") or die("Unable to instantiate MDUSER COM object");
This line throws an error.
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object MDUserCOM.MDUser' in C:\wamp\www\mdaemon\test.php on line 20
( ! ) com_exception: Failed to create COM objectMDUserCOM.MDUser': Invalid syntax in C:\wamp\www\mdaemon\test.php on line 20
Actually I don't know what MDUserCOM.MDUser refers to. How should I instantiate the COM class. Ex $obj = new COM("Application.ID") and what is application and ID in my scenario.
I haven't worked with dll files before. Where I am going wrong and where should I place MDUser.dll file. Anyway it is in my working directory.
Thanks in advance
I'm trying to make my PHAR archive marked as PHP (I don't know correct term for this) so that I can use it from cli without php part. So instead of calling php /path/to/my/archive.phar ARGUMENTS_HERE I can just move it to /usr/local/bin and call it archive from anywhere. (Just like PHPUnit or Composer).
If I'm right, to do that, I just need to add #!/usr/bin/env php on top of my PHAR archive file?
How do I do that? I tried to mimic composer compiler in my build script, but it just crashes PHP_CLI (segmentation fault):
$archiveName = 'phpbenchmark.phar';
$phar = new Phar($archiveName);
$phar->buildFromDirectory(__DIR__, '/\/lib\/|\/vendor\//');
$phar->setStub(createStub());
function createStub()
{
$stub = <<<EOD
#!/usr/bin/env php
<?php
Phar::mapPhar('phpbenchmark.phar');
require 'phar://phpbenchmark.phar/lib/app.php';
__HALT_COMPILER();
EOD;
return $stub;
}
As you can see, I only need stub to run /lib/app.php file and that's it.
Prepending to compiled PHAR
Of course, first thing I tried was manually prepending it to my archive, but it looks like that corrupts it:
PHP Fatal error: Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar"' in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:9
Stack trace:
#0 /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar(9): Phar::webPhar(NULL, 'index.php')
#1 {main}
thrown in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
Fatal error: Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar"' in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
PharException: manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar" in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
Call Stack:
0.0010 306272 1. {main}() /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:0
0.0011 307840 2. Phar::webPhar() /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:9
Note that exactly the same archive works before I manually modify it. (But needs php /path/to/arch of course)
Aaaand, solved (kinda) after few hours of breaking my head. I still have no idea why, this build script seems to work:
$archiveName = 'phpbenchmark.phar';
$phar = new Phar($archiveName);
$phar->buildFromDirectory(__DIR__, '/\/lib\/|\/vendor\//');
$phar->setStub(createStub());
function createStub()
{
$stub = <<<ENDSTUB
#!/usr/bin/env php
<?php
Phar::mapPhar('phpbenchmark.phar');
require 'phar://phpbenchmark.phar/lib/app.php';
__HALT_COMPILER();
ENDSTUB;
return $stub;
}
Except.....I can't really see any differences to original script posted in my question? Why would original one crash PHP Cli and this one works?
All I needed to do to get that to work is update the line:
$phar->setStub($phar->createDefaultStub("main.php"));
to :
$phar->setStub("#!/usr/bin/env php\n\n" . $phar->createDefaultStub("main.php"));
This takes the default stub that phar creates for you, and adds the requires #! line to it. Then just chmod 700 the resulting phar file, and you are good to go.