Re-creating CLI by Wrapping Embed

Now that PHP is accessible from your application, it's time to make it do something. The remainder of this chapter centers around re-creating portions of the CLI SAPI's behavior within the framework of this test application.

Easily, the most basic functionality of the CLI binary is the ability to name a script on the command line and have it interpreted by PHP. Implement that in your application by replacing embed1.c with the code in Listing 19.3.

Listing 19.3. embed1.c

#include 
#include 

int main(int argc, char *argv[])
{
 zend_file_handle script;

 /* Basic parameter checking */
 if (argc <= 1) {
 fprintf(stderr, "Usage: embed1 filename.php 
");
 return -1;
 }

 /* Set up a File Handle structure */
 script.type = ZEND_HANDLE_FP;
 script.filename = argv[1];
 script.opened_path = NULL;
 script.free_filename = 0;
 if (!(script.handle.fp = fopen(script.filename, "rb"))) {
 fprintf(stderr, "Unable to open: %s
", argv[1]);
 return -1;
 }

 /* Ignore argv[0] when passing to PHP */
 argc;
 argv++;

 PHP_EMBED_START_BLOCK(argc,argv)
 php_execute_script(&script TSRMLS_CC);
 PHP_EMBED_END_BLOCK()

 return 0;
}

Of course, you'll need a file to test this out with, make a short PHP scriptanything you likein a file called test.php, and then execute it using your embed1 binary:

$ ./embed1 test.php

If you pass additional arguments onto the command line, you'll see that they reach your script in the $_SERVER['argc'] and $_SERVER['argv'] variables.

Note

You might have noticed that the code placed between PHP_EMBED_START_BLOCK() and PHP_EMBED_END_BLOCK() was indented. This is a subtle homage to the fact that these macros form a C block scope. That is, the PHP_EMBED_START_BLOCK() contains an opening curly brace { with a matching closing curly brace } that is hidden within PHP_EMBED_END_BLOCK(). What's important about this is that these macros can't be buried in separate utility startup/shutdown functions. You'll see this problem resolved in the next chapter.



The PHP Life Cycle

Variables from the Inside Out

Memory Management

Setting Up a Build Environment

Your First Extension

Returning Values

Accepting Parameters

Working with Arrays and HashTables

The Resource Data Type

PHP4 Objects

PHP5 Objects

Startup, Shutdown, and a Few Points in Between

INI Settings

Accessing Streams

Implementing Streams

Diverting the Stream

Configuration and Linking

Extension Generators

Setting Up a Host Environment

Advanced Embedding

Appendix A. A Zend API Reference

Appendix B. PHPAPI

Appendix C. Extending and Embedding Cookbook

Appendix D. Additional Resources

show all menu



Extending and Embedding PHP
Extending and Embedding PHP
ISBN: 067232704X
EAN: 2147483647
Year: 2007
Pages: 175
Authors: Sara Golemon
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net