การใช้งาน function exec ของ PHP

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
Muzashi
PHP Newbie
PHP Newbie
โพสต์: 8
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

การใช้งาน function exec ของ PHP

โพสต์ที่ยังไม่ได้อ่าน โดย Muzashi »

ผมอยากรู้วิธีการใช้ฟังก์ชัน exec คับ คือว่าผมต้องการรันโค้ด java ยกตัวอย่างโค้ดง่ายๆคือ Hello world
ผมอยากรู้ว่าถ้าจะเขียนโค้ด php ให้มันรันตัวโปรแกรม java ต้องเขียนยังไงคับ แล้วก็จะรู้ได้ยังไงว่ามันรันได้ ใครที่พอรู้ช่วยแนะนำด้วยน่ะคับ แล้วก็การใช้ฟังก์ชัน exec ต้องแก้อะไรใน php.ini ด้วยรึป่าว


ผมมีลองอันนี้แต่ว่าไม่เห็นมันขึ้นอะไรเลย (ผมใช้ winxp แล้วก้อ php5 กับ iis)
exec ('C:\\Program Files\\Java\\jdk1.5.0_11\\bin\\java.exe C:\\Inetpub\\wwwroot\\HWorld.class');
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41251
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ไม่ ต้อง ใช้ exec ครับ ลอง อ่าน บท ความนี้ดู ครับ
ผมเคยเล่นนาน แล้ว ทำแบบนี้ได้

PHP / Java Integration
Introduction
There are two possible ways to bridge PHP and Java: you can either integrate PHP into a Java Servlet environment, which is the more stable and efficient solution, or integrate Java support into PHP. The former is provided by a SAPI module that interfaces with the Servlet server, the latter by this Java extension.

The Java extension provides a simple and effective means for creating and invoking methods on Java objects from PHP. The JVM is created using JNI, and everything runs in-process.


?????
This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.


Requirements
You need a Java VM installed on your machine to use this extension.

Installation
This PECL extension is not bundled with PHP.

In PHP 4 this PECL extensions source can be found in the ext/ directory within the PHP source or at the PECL link above. In order to use these functions you must compile PHP with Java support by using the --with-java[=DIR] where DIR points to the base install directory of your JDK. This extension can only be built as a shared extension. Additional build extensions can be found in php-src/ext/java/README.

Windows users will enable php_java.dll inside of php.ini in order to use these functions. In PHP 4 this DLL resides in the extensions/ directory within the PHP Windows binaries download. You may download this PECL extension DLL from the PHP Downloads page or at http://snaps.php.net/.

????: In order to enable this module on a Windows environment with PHP <= 4.0.6, you must make jvm.dll available to your systems PATH. No additional DLL is needed for PHP versions > 4.0.6.

Runtime Configuration
The behaviour of these functions is affected by settings in php.ini.


???? 1. Java configuration options

Name Default Changeable Changelog
java.class.path NULL PHP_INI_ALL
java.home NULL PHP_INI_ALL
java.library.path NULL PHP_INI_ALL
java.library JAVALIB PHP_INI_ALL

For further details and definitions of the PHP_INI_* constants, see the ???? G.

Resource Types
This extension has no resource types defined.

Predefined Constants
This extension has no constants defined.

Examples
????? 1. Java Example

<?php
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');

// demonstrate property access
echo 'Java version=' . $system->getProperty('java.version') . '<br />';
echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br />';
echo 'OS=' . $system->getProperty('os.name') . ' ' .
$system->getProperty('os.version') . ' on ' .
$system->getProperty('os.arch') . ' <br />';

// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");

echo $formatter->format(new Java('java.util.Date'));
?>

????? 2. AWT Example

<?php
// This example is only intended to be run as a CGI.

$frame = new Java('java.awt.Frame', 'PHP');
$button = new Java('java.awt.Button', 'Hello Java World!');

$frame->add('North', $button);
$frame->validate();
$frame->pack();
$frame->visible = True;

$thread = new Java('java.lang.Thread');
$thread->sleep(10000);

$frame->dispose();
?>

Notes:


new Java() will create an instance of a class if a suitable constructor is available. If no parameters are passed and the default constructor is useful as it provides access to classes like java.lang.System which expose most of their functionallity through static methods.

Accessing a member of an instance will first look for bean properties then public fields. In other words, print $date.time will first attempt to be resolved as $date.getTime(), then as $date.time.

Both static and instance members can be accessed on an object with the same syntax. Furthermore, if the java object is of type java.lang.Class, then static members of the class (fields and methods) can be accessed.

Exceptions raised result in PHP warnings, and NULL results. The warnings may be eliminated by prefixing the method call with an "@" sign. The following APIs may be used to retrieve and reset the last error:


java_last_exception_get()

java_last_exception_clear()


Overload resolution is in general a hard problem given the differences in types between the two languages. The PHP Java extension employs a simple, but fairly effective, metric for determining which overload is the best match.

Additionally, method names in PHP are not case sensitive, potentially increasing the number of overloads to select from.

Once a method is selected, the parameters are coerced if necessary, possibly with a loss of data (example: double precision floating point numbers will be converted to boolean).

In the tradition of PHP, arrays and hashtables may pretty much be used interchangably. Note that hashtables in PHP may only be indexed by integers or strings; and that arrays of primitive types in Java can not be sparse. Also note that these constructs are passed by value, so may be expensive in terms of memory and time.


Java Servlet SAPI
The Java Servlet SAPI builds upon the mechanism defined by the Java extension to enable the entire PHP processor to be run as a servlet. The primary advantage of this from a PHP perspective is that web servers which support servlets typically take great care in pooling and reusing JVMs. Build instructions for the Servlet SAPI module can be found in php4/sapi/README. Notes:


While this code is intended to be able to run on any servlet engine, it has only been tested on Apache's Jakarta/tomcat to date. Bug reports, success stories and/or patches required to get this code to run on other engines would be appreciated.

PHP has a habit of changing the working directory. sapi/servlet will eventually change it back, but while PHP is running the servlet engine may not be able to load any classes from the CLASSPATH which are specified using a relative directory syntax, or find the work directory used for administration and JSP compilation tasks.


????
java_last_exception_clear -- Clear last Java exception
java_last_exception_get -- Get last Java exception
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
Muzashi
PHP Newbie
PHP Newbie
โพสต์: 8
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

โพสต์ที่ยังไม่ได้อ่าน โดย Muzashi »

ท่าน mindphp ครับ ถ้าผมต้องการแปลงโค้ด java ข้างล่างให้รันกับ php ต้องแปลงยังไงครับ ผมอ่านแล้วไม่ค่อยเข้าใจ

********************************************************************
import org.smslib.*;
import javax.comm.*;
import java.io.*;

public class Noname10
{

public static void main(String[] args)

{

CService srv = new CService("COM3", 57600, "Nokia", "");

try
{
srv.setProtocol(CService.Protocol.TEXT);

srv.connect();

COutgoingMessage msg = new COutgoingMessage("0815422669", "text message");

msg.setStatusReport(true);

msg.setValidityPeriod(8);
srv.sendMessage(msg);
srv.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}


}
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41251
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ได้อ่าน วิธี ข้างบน และ ลอง ทำหรือ ยังครับ ติดขัดตรงไหน มาคุยต่อกันได้ ครับ
ลองทำ ตามดูก่อน นะครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
Muzashi
PHP Newbie
PHP Newbie
โพสต์: 8
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

โพสต์ที่ยังไม่ได้อ่าน โดย Muzashi »

ผมไม่เข้าใจวิธีการลง PECL คับ ผมใช้ PHP5.2.1 แบบ msi กับ IIS ครับ แล้วผมดาวน์โหลด pecl-5.2.1-Win32.zip มาจะลงยังไงคับ แล้วก็ต้องไป config php.ini ยังไง แล้วก็ช่วยยกตัวอย่างง่ายๆ ในการแปลงโค้ดข้างล่างให้เป็น php ด้วยน่ะคับ ผมอ่านที่ให้มาแล้วไม่เข้าใจน่ะคับ

import java.io.*;

class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41251
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

วิธี แก้ไข ไฟล์ php.ini ไฟล์ นี้ ขึ้นอยู่กับ การ ติกตั้ง ว่าเรา ติดตั้ง php แบบไหน
หาไฟล์ php.ini แล้ว เปิด กับ notepad หรือ โปรแกรมอะไร ก็ได้
แก้
;extension=php_java.dll
เป็น
extension=php_java.dll

ผม ก็ ไม่ได้ เล่น php กับ java นาน แล้ว นะครับ
ตอนนั้นที่ ทำ ทำกับ php4 แต่เดี๋ยว ว่างๆ จะลอง เล่น กับ php 5 ดู ครับ
โดย หลังการ แล้ว เราไม่ต้องแปลง โค้ด แต่ อย่างใด เรา เขียน java ก็ เขียน ไป ตามปกติ ตัว engin ของ จะจัดการ ผ่าน ทาง pecl ให้เอง เดี๋ยว ลอง ดูด้วยกัน ก็ได้ครับ ได้ผล ยังไง มาดู กัน
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41251
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ลองดูตามรูปนี้ประกอบ นะครับ
[img]http://dev2dev.bea.com/images/2007/02/WithApache.jpg[/img]

ส่วนนี่เป็น วิธี แต่ ค่อนข้างเก่า นะ ต้งอเอามาประยุกนิดหน่อย
PHP & Java
Mark Nold
Joost Soeterbroek
The Java extension is an extremely exciting tool. By learning how to use this module, you can extend PHP by the power of all available Java classes. To show you the basics of the Java extension, this article will cover installation and a few code examples of using PHP and Java together.
Windows Installation
The following configuration has been tested with Apache 1.3.12, PHP 4.0.3 binaries from www.php4win.de plus the 4.0.3 Zend Optimiser and JDK 1.2.2 from java.sun.com. We have also tested this configuration with older versions of the JDK and the various MS webservers (PWS and IIS) on Windows 95, Windows 98 and NT4.
Step 1: Install the JDK. This is fairly simple, as the JDK installs without many questions. It might be useful to check your environment (autoexec.bat in Windows 9x and System under Control Panel in NT) and make sure the jdk1.x.x\bin directory is in your path.
This will make compiling your Java Classes easier. On Win9x add
PATH=%PATH%;C:\jdk1.2.2\bin
to your autoexec.bat. On NT add
;C:\jdk1.2.2\bin
to the end of the PATH environment variable. It is also important to note in your autoexec.bat file, the PHP Java extension ignores the JAVA_HOME and CLASSPATH set up in the environment. This is important because these items must be set correctly in your php.ini file for the Java extension to work.
Step 2: Modifying your php.ini. You need to add something similiar to your php.ini file.

[java]
extension=php_java.dll
java.library.path=c:\web\php4\extensions\
java.class.path="c:\web\php4\extensions\jdk1.2.2\php_java.jar;c:\myclasses"

Typically, people put the extension=php_java.dll directive with the rest of the extensions, but it can sit happily under [java]. The java.library.path must be set in the location of the php_java.dll, and java.class.path must include the location of php_java.jar. The java.class.path should also include the path to other classes you may wish to use (note the double quotes!). In these examples, we will be talking about c:\myclasses. You should also note that the single period is ignored by PHP and Java. As far as we know, you cannot set PHP to look in the current directory for its Java classes.
Step 3: Testing your Install. Now, you're ready to go. Create a PHP file that looks something like this:

<?php

$system = new Java("java.lang.System");
print "Java version=".$system->getProperty("java.version")." <br>\n";
print "Java vendor=".$system->getProperty("java.vendor")." <p>\n\n";
print "OS=".$system->getProperty("os.name")." ".
$system->getProperty("os.version")." on ".
$system->getProperty("os.arch")." <br>\n";

$formatter = new Java("java.text.SimpleDateFormat","EEEE,
MMMM dd, yyyy 'at' h:mm:ss a zzzz");
print $formatter->format(new Java("java.util.Date"))."\n";

?>

NB: This is taken directly from Sam Ruby's examples. If you have correctly installed everything, you should see some results like:

Java version=1.2.2
Java vendor=Sun Microsystems Inc.
OS=Windows 95 4.10 on x86
Wednesday, October 18, 2000 at 10:22:45 AM China Standard Time

A very simple example, but it shows you can access currently available Java classes. Once you have this example working, you have successfully set up the PHP Java extension.
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
Muzashi
PHP Newbie
PHP Newbie
โพสต์: 8
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

โพสต์ที่ยังไม่ได้อ่าน โดย Muzashi »

ผมลง PHP5.2.1 แบบ msi แล้วก็ใช้กับ IIS แล้วผมก็ไปดาวน์โหลดตัว pecl-5.2.1-Win32.zip

พอลงเสร็จมันจะไปสร้างโฟรเดอร์ที่ C:\Program Files\PHP แต่ว่าในโฟรเดอร์นี้จะไม่มีโฟรเดอร์ ext ผมก็เลย upzip pecl-5.2.1-Win32.zip แล้วก็ไปสร้างโฟรเดอร์ ext แล้วcopy ที่ upzip ไว้ไปใส่

ส่วนใน php.ini ก็จะอยู่ที่ C:\Program Files\PHP ผมก็แก้โดยใส่

extension_dir = "C:\Program Files\PHP\ext"
แล้วก็
[java]
extension=php_java.dll
java.library.path=C:\Program Files\PHP\ext
java.class.path="C:\Program Files\PHP\ext\php_java.jar;C:\Inetpub\wwwroot"
ที่ท้ายไฟล์
แต่ผมไม่รู้ว่า java.class.path="c:\web\php4\extensions\jdk1.2.2\php_java.jar;c:\myclasses" ไอ c:\myclasses คืออะไร ผมคิดว่าคงเป็นที่เก็บไฟล์บนwebserver ก้อเลยใส่ C:\Inetpub\wwwroot ลงไปแทน ไม่รู้ว่าถูกรึป่าวช่วยดูให้หน่อยน่ะคับ

ผมลองรัน example โค้ดแล้วแต่ไม่ขึ้นอะไรเลย เด่วจะลองรีสตาร์เครื่องดูก่อน
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41251
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ถ้าผมจำไม่ผิด คิดว่า c:\myclasses เป็น Folder ที่ ใช้เก็บ ไฟล์ java ที่คอมไฟล์ เป็น .class น่ะครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
Muzashi
PHP Newbie
PHP Newbie
โพสต์: 8
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

โพสต์ที่ยังไม่ได้อ่าน โดย Muzashi »

ช่วยดูให้หน่อยน่ะคับ ด่วนมากๆ ส่วน example code สรุปว่ารันแล้วไม่ขึ้น
ไม่รู้ว่าเซ็ตอะไรผิด หรือว่าต้องเซ็ตอะไรเพิ่มรึป่าว
ตอบกลับโพส

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 11