Quickly develop a PHP extended graphics tutorial _php Example

Source: Internet
Author: User
Requirements: such as the development of an extension called Heiyeluren, the extension of a function heiyeluren_test (), enter a string, function return: Your input string:xxxxx.
Requirements: To understand C + + programming, familiar with PHP programming
Environment: Download a PHP corresponding version of the source code, I am here is php-5.2.6, the first normal installation of PHP, assuming that our PHP installed in the/usr/local/php directory, the source code in/root/soft/php/php-5.2.6/, now start!
Step One: Build the extension framework


Cd/root/soft/php/php-5.2.6/ext
./ext_skel--extname=heiyeluren
Cd/root/soft/php/php-5.2.6/ext/heiyeluren
VI CONFIG.M4
After opening the file, remove the DNL and get the following information:
Php_arg_enable (Heiyeluren, whether to ENABLE Heiyeluren support,
[--enable-heiyeluren enable Heiyeluren support])
Save exit.
(Figure 01)

Step Two: Write code

VI Php_heiyeluren.h
Found: Php_function (confirm_heiyeluren_compiled); , add a new line:
php_function (heiyeluren_test);
Save exit.
(Figure 02)


VI heiyeluren.c
Add our function to the array, find Zend_function_entry heiyeluren_functions[], add:
Php_fe (Heiyeluren, NULL)
(Figure 03)

Then add the following code to the end of the Heiyeluren.c file:
Php_function (Heiyeluren_test)
{
char *arg = NULL;
int Arg_len, Len;
Char *STRG;

if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "s", &arg, &arg_len) = = failure) {
Return
}

Len = spprintf (&STRG, 0, "Your input string:%s\n", ARG);
Return_stringl (STRG, Len, 0);
}
Save exit.
(Figure 04)

Step three: Compile and install

Cd/root/soft/php/php-5.2.6/ext/heiyeluren
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
Make
Make Test
Make install

Now let's see if there's a/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/heiyeluren.so.
Edit the php.ini and add the extensions:
Vi/usr/local/php/lib/php.ini
Add under [PHP] module:
Extension = heiyeluren.so
Save exit.
(Figure 05)

Note: If you do not have an extended file directory, or install an error, then you can set up this directory, and then copy the extension to the directory, and then remember the php.ini file Extension_dir modified to the directory:
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
(Figure 06)

Step Fourth: Check the installation results
Now look at the module loaded without:
/usr/local/php/bin/php-m, it should print out:
[PHP Modules]
...
Heiyeluren
...
[Zend Modules]


Then restart Apache, Output phpinfo () and should be able to see:
Heiyeluren
Heiyeluren Support Enabled
(Figure 07)

To see if the function exists and call it, set it up in the Web directory: heiyeluren.php
<?php
echo "<pre>";
Print_r (Get_loaded_extensions ());
Print_r (Get_extension_funcs (' Heiyeluren '));
echo heiyeluren_test (' My A-php extension ');
echo "</pre>";
?>
To access Apache, you should be able to see:
Array
(
...
[=>] Heiyeluren
)
Array
(
[0] => confirm_heiyeluren_compiled
[1] => heiyeluren_test
)
Your input String:heiyeluren
(Figure 08)


Extended Production Success!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.