简体   繁体   English

为什么PHP COM对象没有返回正确的结果?

[英]Why PHP COM objects are not returning correct results?

I'm trying to access Windows SAPI5 or Text to speech (TTS) using PHP. 我正在尝试使用PHP访问Windows SAPI5或文本转语音(TTS)。 The standard approach is to create a COM object for "SAPI.SpVoice", then get the installed voices. 标准方法是为“ SAPI.SpVoice”创建一个COM对象,然后获取已安装的语音。

Sample PHP code: 示例PHP代码:

<?php

$obj = new COM('SAPI.SpVoice');
$voices = $obj->GetVoices;
$count = $voices->Count;

print $count; #prints "1"

Unfortunately the output returned from PHP's COM object is incorrect because I have 5 voices installed on my system, but PHP only returns 1. 不幸的是,PHP的COM对象返回的输出不正确,因为我的系统上安装了5种声音,但是PHP仅返回1。

So, just to check if this a PHP specific issue, I wrote the same code in Perl 5.8 (strawberry). 因此,仅检查这是否是特定于PHP的问题,我在Perl 5.8(草莓)中编写了相同的代码。

Sample Perl code: 示例Perl代码:

#!/usr/bin/perl
use Win32::OLE;

my $obj = Win32::OLE->new('SAPI.SpVoice');
my $voices = $obj->GetVoices;
my $count = $voices->Count;

print $count; #print "5" which is correct.

So the perl code correctly returns that I have 5 TTS voices on my system, but PHP returns only 1? 因此,perl代码正确返回了我的系统上有5个TTS语音,但是PHP仅返回了1个?

Is this a bug or am I doing something wrong? 这是一个错误还是我做错了什么? What could be the possible cause of this? 这可能是什么原因?

PS I've tried this on two different computers and results are the same. PS我已经在两台不同的计算机上尝试过,结果是相同的。

I figured this after some trial error. 经过一些试验错误,我发现了这一点。 It looks like if I use the 32-bit version of PHP then I get the correct results (5 voices). 看来,如果我使用32位版本的PHP,那么我会得到正确的结果(5个声音)。 But since I had installed the 64-bit version by default I only get 1 voice. 但是由于默认情况下我安装了64位版本,所以我只能听到1条声音。

I think the TTS voices are mostly 32 bit (like those installed on my system) and so when running with a 64-bit php.exe it only returns 64-bit voices. 我认为TTS声音大多为32位(就像安装在系统上的声音一样),因此当使用64位php.exe运行时,它只会返回64位声音。 With 32 php.exe it returns all voices. 使用32 php.exe,它将返回所有声音。

Posting this as answer in case someone faces a similar issue in future. 将其发布为答案,以防将来有人遇到类似问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM