How can I give PHP access to a COM object? - php

I am running PHP 5.3 on my local Windows 7 Laptop (have tried this all on our development server with no success, so I tried to see if I could get it to work on my laptop successfully first). When I call the following script:
$objAltovaXML = new COM('AltovaXML.Application');
I get the error:
Uncaught exception 'com_exception' with message 'Failed to create COM object `AltovaXML.Application': Access is denied.
I have checked using a PowerShell script I have found elsewhere on StackOverflow and have confirmed tht AltovaXML.Application is properly registered.
But how do I give my PHP script access to it?

While it should work I would just wrap a script around COM object and invoke that script via exec. Saves you a lot of trouble and is easy to debug separately and understand what exactly is going on.

Related

Google Cloud Vision - PHP from terminal - error loading credentials

I'm trying to work with the Google Cloud Vision API with PHP. I created my first test page and when run from a website, the PHP works correctly with no issues. I have a new twist on my project and I want to run the same PHP code from the terminal.
The issue I'm having now though is that I get an error that my script doesn't have the credentials loaded.
Here is the code I'm using to load my credentials file that works in my website version...
putenv('GOOGLE_APPLICATION_CREDENTIALS=Credential-file.json');
This doesn't work on my Terminal run version of the code. So I've tried a couple other options like...
putenv('GOOGLE_APPLICATION_CREDENTIALS=/Full/Path/To/Credential-file.json');
and
$_ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "/Full/Path/To/Credential-file.json";
The exact error is...
PHP Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /Library/WebServer/Documents/google_vision/vendor/google/auth/src/ApplicationDefaultCredentials.php:156
My question is... Is there a better way to load the credentials file when I'm trying to run my PHP from the terminal? Or what mistake am I making in my version?
I should mention that I'm running this on MacOS with the Google Cloud client libraries.
Any help/suggestions would be greatly appreciated.
Thanks in advance!
The error message indicates that the variable is not set properly (as opposed to it being set to an invalid path).
putenv only lasts the duration of the current script. Setting values on $_ENV does not make them available to getenv, the method used by the Google auth library. Are you setting the credentials environment variable within the same script you're executing?
You can provide the value in the invocation as well:
GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json php my-script.php
Or set the variable in your terminal session:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json
php my-script.php
If you're using Google\Cloud\Vision\V1\ImageAnnotatorClient, you can authenticate with a keyfile directly:
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
$vision = new ImageAnnotatorClient([
'credentials' => '/path/to/keyfile.json
]);
Or with Google\Cloud\Vision\VisionClient:
use Google\Cloud\Vision\VisionClient;
$vision = new VisionClient([
'keyFilePath' => '/path/to/keyfile.json'
]);

Google Calendar API PHP Installation

I have been trying to get the Google Calendar API working with PHP, I even contacted my host (1and1) to figure things out, nothing is working
I've followed the examples on the PHP Quickstart, tried to run it just by going to the page and it gave me an error to run it in command line. So I logged into my SSH, navigated to the file and ran the command php5.4 CRON-temp.php. Now, I get the same error:
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'This application must be
run on the command line.' in /homepages/41/d159654153/htdocs/mysite
/admin/php/CRON-temp.php:16
Stack trace:
#0 {main}
thrown in <b>/homepages/41/d159654153/htdocs/mysite/
admin/php/CRON-temp.php</b> on line <b>16</b><br />
So, I've run it in the command line, but it still tells me to run it there. The code throwing the error comes from this:
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
I've been held up here... I've sent the code to a friend to try out and he gets a link to return where I'm supposed to go authorize this, but I'm not able to see any of that.
The tech person at 1and1 said
Now, if you will look at the error, the error changes from "parse error" to "Uncaught exception 'Exception". It appears that this application will run in server environment and not on a shared hosting. This is a limitation on the package that you have. You might need to go to a server package for this API to run.
Is this really the case? Do I really need to switch over to server hosting? Is there a way around this?
All I want to do is have a 2 way sync between Google Calendar and a mySQL server.
According to the Google code and instructions i.e. https://developers.google.com/google-apps/calendar/quickstart/php
first you try to run this using cmd.
Go to start type cmd and then black screen will appear type here as:
php yourfilename.php, then it will provide you a url and ask for verification code you just visit the said url and allow the Google and get key and enter the said key in cmd.
Your code is ready to use. Now comment the same code which is restricting for cli i.e:
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
I found your post from a project that I'm currently working on. You can run quickstart.php from the command line which then gives you a link to paste into your browser to authorize access.
Once you have authorized access, you can run the script in the command line again and it displays upcoming events.
There is no browser output - that is what I am looking for.
I hope this helps.

PHP fatal error on creating Com Object

I have a php web application that needs to access a PI-Datasource using an application-library written for windows on dll form to fetch the data. Due to the non-polimorphism of PHP we are using a wrapper written in C#-Dotnet in order to use the polimorphism library.
PHP->Wrapper->Lib->PI-System
The problem: PHP crashes, without leaving a log, on the creation of the com object (almost always on every second request). My thought was that probably something in the existing php code could be wrong that causes this fatal-error and after a lot of debugging and trying I simplified the code to this:
$connection = new Com('Something.SomethingClass');
with the variable $connection not being used NEVER! and still every 2nd time I get php-crash (documented in windows error log with an 1000-Error and an 1001-Information)
>Faulting application name: php-cgi.exe, version: 5.4.11.0, time stamp: 0x511a30ec
>Faulting module name: KERNELBASE.dll, version: 6.1.7601.17932, time stamp: 0x503275ba
>Exception code: 0xc0000005
>Fault offset: 0x0000d3cf
>Faulting process id: 0x14d0
>Faulting application start time: 0x01ce89dd0ae23748
>Faulting application path: C:\Program Files\Zend\ZendServer\bin\php-cgi.exe
>Faulting module path: C:\Windows\system32\KERNELBASE.dll
So i tried to generate even more com objects..
$connArray = array();
for($i = 0; $i < 50 ; $i++){
Core_System_Log::getInstance()->logWithoutMessageId('Before: ' . $i ,Core_System_Log::DEBUG);
$connArray[i] = new Com('Something.SomethingClass');
Core_System_Log::getInstance()->logWithoutMessageId('After: ' . $i, Core_System_Log::DEBUG);
}
All 50 were generated with no problem, and again every 2nd time i tried i got a php fatal error.
I tried to used all 50 of them, and they all read values from the PI-System with no problem.
I tried to unset the variables and call also the gc, call the destructor from C#, check the constructor from C# (which just makes an object of the library and had no exception, the object was normally created and still php crashed) but the problem did not disappear.
So, is there any idea? am i doing something wrong (how can it be wrong when every 2nd time it is properly reading the values)?
Environments Tested:
OS:Windows Server 2008 R2 64Bit / Windows 7 Prof SP1 32/64Bit
PHP: 5.3.9/ 5.3.14 / 5.3.21 / 5.4.11
WS: Apache and IIS (few different versions)
UPDATE: The problem was finally in the C# code. There was a call to GC which did not allow the COM object to be closed/deleted correctly having as a result C# to hang (again with no exception) and "triggering" the php fatal error.
Thanks for the responses.
If returns crashed without logging into file it look that in code contains sign #.
Example:
#some_function()
This will call function, but if has any errors it will not showing to you, just skipping. Due to some errors PHP can be stopped.
But, can you try upgrade PHP to 5.5 on Windows server?
If you know in which of lines of code throws an error and want to skip, just put isset()
If isset($somevar) or isset(function()) returns true that means are not errors, but if you don't want to stop a function on errors put isset() on a line of code where errors occurred.
I'm not sure how looks class and function inside PHP->Wrapper->Lib->PI-System code so I can tell fully corrected answer.
UPDATE: The problem was finally in the C# code. There was a call to GC which did not allow the COM object to be closed/deleted correctly having as a result C# to hang. The object was somewhere kept in Ram and the second time the wrapper was called it just hanged/exited (again with no exception) which "triggered" the php fatal error.

Codeigniter Command line error - PHP Fatal error: Class 'CI_Controller' not found

After following the user guide instructions found here: http://ellislab.com/codeigniter/user-guide/general/cli.html I'm unable to run the test script via command line.
My controller located at /var/www/mysite/application/controllers/
class Tools extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
In my browser I can access
http://mysite/tools/message/ben
And the function correctly outputs "Hello ben"
From terminal I should be able to run:
$ php index.php tools message "Ben"
My terminal should print: "Hello Ben"
However I get the following error:
PHP Fatal error: Class 'CI_Controller' not found in /var/www/mysite/system/core/CodeIgniter.php on line 233
My server is pretty standard; ubuntu LAMP. Codeigniter is pretty standard too and I have no problem running non CI scripts via command line
My PHP binary is only located in /usr/bin/php <-- This post suggests an issue running CI directly from usr/bin/php, however I'm not operating a shared PHP service, and I don't see why this would make a difference to how PHP executes a CI script.
Any help or just an indication on how to debug this would be greatly appreciated.
Thanks in advance.
Solved! (partly) the issue was CodeIgniters error logging.
In application/config/config.php, I modified the following config property:
$config['log_threshold'] = 0;
This disables logging, and allows $ php index.php to execute.
If anyone can explain why CI only shows this error on CLI PHP - might help anyone else who has this issue and needs it resolved with error logging on.
To solve error "Class 'CI_Controller' not found" try going to Application -> Config -> database.php then check the database details like hostname, username, password and database.
To Mijahn:
I had this same problem, and after about two hours of tracing through code to figure out the problem, it seems that there is some sort of conflict with loading the CI_Controller when utilizing the native PHP load_class function.
I worked around this issue by making the following changes to the Common.php file (hack, I know).
//$_log =& load_class('Log');
require_once('system/libraries/Log.php');
$_log = new CI_Log();
My logs then where created exactly like I wanted. Hope this hack helps.
This site says to run codeigniter from the command line, one must set the $_SERVER['PATH_INFO'] variable.
$_SERVER['PATH_INFO'] is usually supplied by php when a web request is made. However, since we are calling this script from the command line, we need to emulate this small part of the environment as a web request.
The answer provided in this Stack Overflow post worked for me.
Within system/core/CodeIgniter.php, on around line 75, change:
set_error_handler('_exception_handler');
to...
set_exception_handler('_exception_handler');
Other users have reported that this gave them a better backtrace with which to debug the underlying issue, but for me, this actually removed the problem altogether.

Codeigniter command line not working with database class / driver?

Has anyone else out there had this problem and found a possible solution. Im not getting any error msg's, simply that no data is been returned from the command line when the database class is either loaded automatically or from the controller.
(This is for CodeIgniter Reactor version.)
Update:
Reported this as a bug:
https://bitbucket.org/ellislab/codeigniter-reactor/issue/85/
Im using cURL to run the script for now and not direct php,
so this:
curl http://localhost/~derrick/mywebsite/index.php/task/run
instead of this:
php Users/derrick/sites/mywebsite/index.php task run
for now until the problem has been resolved.

Categories