工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表分类:php

php命令行,多进程

  1. /** 确保这个函数只能运行在SHELL中 */
  2. if (substr(php_sapi_name(), 0, 3) !== 'cli') {
  3.     die("This Programe can only be run in CLI mode");
  4. }
  5.  
  6. /** 关闭最大执行时间限制, 在CLI模式下, 这个语句其实不必要 */
  7. set_time_limit(0);
  8.  
  9. $pid = posix_getpid(); //取得主进程ID
  10. $user = posix_getlogin(); //取得用户名
  11.  
  12. echo <<<EOD
  13. USAGE: [command | expression]
  14. input php code to execute by fork a new process
  15. input quit to exit
  16.  
  17.         Shell Executor version 1.0.0 by laruence
  18. EOD;
  19.  
  20. while (true) {
  21.  
  22.         $prompt = "\n{$user}$ ";
  23.         $input = readline($prompt);
  24.  
  25.         readline_add_history($input);
  26.         if ($input == 'quit') {
  27.                break;
  28.           }
  29.         process_execute($input . ';');
  30. }
  31.  
  32. exit(0);
  33.  
  34. function process_execute($input) {
  35.         $pid = pcntl_fork(); //创建子进程
  36.         if ($pid == 0) {//子进程
  37.                 $pid = posix_getpid();
  38.                 echo "* Process {$pid} was created, and Executed:\n\n";
  39.                 eval($input); //解析命令
  40.                 exit;
  41.         } else {//主进程
  42.                 $pid = pcntl_wait($status, WUNTRACED); //取得子进程结束状态
  43.                 if (pcntl_wifexited($status)) {
  44.                         echo "\n\n* Sub process: {$pid} exited with {$status}";
  45.                 }
  46.         }
  47. }

 

http://www.laruence.com/2009/06/11/930.html

php curl 获取状态码

<?php

ini_set('display_errors','1');
ini_set('display_startup_errors','1');
//error_reporting(E_ALL);
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(60);
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
date_default_timezone_set('PRC');

$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 20);
curl_setopt($curlHandle, CURLOPT_URL, 'http://www.163.com');
curl_setopt($curlHandle, CURLOPT_HEADER, 1);
$strRet = curl_exec($curlHandle);
if ($strRet === false) {    /* 失败或超时 */
    die('false');
} else {
    $status = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
}
curl_close($curlHandle);
echo $status;

?>

不同编译方式下php扩展的调用区别

php的一些组件,既可以静态编译进php本身,也可以用so的方式来动态加载。静态编译的模式不说,如果是共享库so的方式,肯定是用dlopen的方式,那就需要这个so有一个方法名已知的函数暴露出来,使用dlsym来调用。仔细看扩展预先生成的源码,以hanks这个扩展为例:

 

#ifdef COMPILE_DL_HANKS

ZEND_GET_MODULE(hanks)

#endif

 

COMPILE_DL_HANKS 决定了要不要以dll(.so)的方式来编译hanks这个扩展,如果dll方式,则ZEND_GET_MODULE这个宏就会发生作用。


ZEND_GET_MODULE定义如下:

 

#define ZEND_GET_MODULE(name) \

    BEGIN_EXTERN_C()\

ZEND_DLEXPORT zend_module_entry *get_module(void) { return &name##_module_entry; }\

    END_EXTERN_C()

 

BEGIN_EXTERN_C 就是extern "C" {,其实这里暴露出来的就是 get_module 方法,dlsym(dl,'get_module')就会返回这个函数指针,调用后就返回 hanks_module_entry 这个zend_module_entry结构体,包含这个模块的相关信息和方法,这样php就能完全掌握这个模块,融入内部的体系中。

php调用so库和a库方法

  • (一)调用so方法

(引自:http://tech.idv2.com/2007/07/06/use-local-so-in-php/)

某个功能被编译到so文件中,那么如何通过php来调用它?一个方法是写一个php模块(php extension),在php中调用该模块内的函数,再通过该模块来调用so中的函数。下面做一个简单的例子,使用的操作系统是Fedora Core 6。

首先做一个简单的so文件:

/**
 * hello.c
 * To compile, use following commands:
 *   gcc -O -c -fPIC -o hello.o hello.c 
 *   gcc -shared -o libhello.so hello.o
 */

int hello_add(int a, int b)
{
    return a + b;
}

然后将它编译成.so文件并放到系统中:

$ gcc -O -c -fPIC -o hello.o hello.c
$ gcc -shared -o libhello.so hello.o
$ su
# echo /usr/local/lib > /etc/ld.so.conf.d/local.conf
# cp libhello.so /usr/local/lib
# /sbin/ldconfig

写段小程序来验证其正确性:

/**
 * hellotest.c
 * To compile, use following commands:
 *   gcc -o hellotest -lhello hellotest.c
 */
#include <stdio.h>
int main()
{
    int a = 3, b = 4;
    printf("%d + %d = %d\n", a, b, hello_add(a,b));
    return 0;
}

编译并执行:

$ gcc -o hellotest -lhello hellotest.c
$ ./hellotest
3 + 4 = 7

OK,下面我们来制作PHP模块。首先确保你安装了 php-devel 包,没有的话请自行从安装光盘上找。然后下载php源代码。我使用的是php-5.2.3.tar.gz,解压缩。

$ tar xzvf php-5.2.3.tar.gz
$ cd php-5.2.3/ext

然后通过下面的命令建立一个名为 hello 的模块。

$ ./ext_skel --extname=hello

执行该命令之后它会提示你应当用什么命令来编译模块,可惜那是将模块集成到php内部的编译方法。如果要编译成可动态加载的 php_hello.so,方法要更为简单。

$ cd hello

首先编辑 config.m4 文件,去掉第16行和第18行的注释(注释符号为 dnl 。)

16:  PHP_ARG_ENABLE(hello, whether to enable hello support,
17:  dnl Make sure that the comment is aligned:
18:  [  --enable-hello           Enable hello support])

然后执行 phpize 程序,生成configure脚本:

$ phpize

然后打开 php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函数声明:

PHP_FUNCTION(confirm_hello_compiled);   /* For testing, remove later. */
PHP_FUNCTION(hello_add);

打开 hello.c,在 PHP_FE(confirm_hello_compiled, NULL) 下方加入以下内容。

zend_function_entry hello_functions[] = {
    PHP_FE(confirm_hello_compiled,  NULL)       /* For testing, remove later. */
    PHP_FE(hello_add,   NULL)       /* For testing, remove later. */
    {NULL, NULL, NULL}  /* Must be the last line in hello_functions[] */
};

然后在 hello.c 的最末尾书写hello_add函数的内容:

PHP_FUNCTION(hello_add)
{
    long int a, b;
    long int result;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {
        return;
    }

    result = hello_add(a, b);

    RETURN_LONG(result);
}

保存退出,编译并安装:

$ ./configure
$ make LDFLAGS=-lhello
$ su
# cp modules/hello.so /usr/lib/php/modules

然后在 /var/www/html 下建立一个 hello.php 文件,内容如下:

<?php
    dl("hello.so");
    echo hello_add(3, 4);
?>

然后在浏览器中打开hello.php文件,如果显示7,则说明函数调用成功了。

参考URL

 

  • 调用a库方法:

(引自:http://group.qqread.com/thread-22305-1-1.html)

把静态库加入PHP:
       把要编译的静态库链接存入环境变量。假设静态库的文件名叫libnpc.a,放在/home目录下。在PHP的安装目录下输入如下命令:export LDFLAGS=”–L/home –lnpc”
       这个环境变量的作用就是让PHP在编译时知道要把这个库也一起编译进去。

编译PHP:
       和普通编译PHP没什么大区别,就是要在./configure的时候加上你的扩展,具体见编写PHP扩展部分。
       当编译好之后可以通过unset LDFLAGS来删除之前的环境变量。

总结:

动态库和静态库相同的地方就是上面橘色的地方,不同的地方so库是紫色的方法,而a库

s#export LDFLAGS=”–L/home –lnpc”

s#./configure

s#make

s#unset LDFLAGS

 

Linux下把静态库编译进PHP的方法

本方法适用于PHP4.3版本。
  
  总体思路:
   一般要调用C语言的函数需要自己写PHP扩展,而以filename.a命名的静态库的函数是以C语言的方式来调用的,所以如何编写PHP扩展也是本文涉及到的重点。编译PHP的步骤是:写PHP扩展->把静态库加入PHP->编译PHP。
   我的环境是:Linux Redhat7.3 Apache1.3.29 PHP4.3.4
  
  写PHP扩展:
   这部分内容请参考CSDN上Yorgo sun写的文档:http://dev.csdn.net/develop/article/12/12404.shtm。上面的内容很全,说得也非常清楚,即使从来没有写过PHP扩展的人看了也能明白。但在这个文档中没有如何调用静态库的方法,所以在最终编译PHP前要加入以下一步。
  
  把静态库加入PHP:
   把要编译的静态库链接存入环境变量。假设静态库的文件名叫libnpc.a,放在/home目录下。在PHP的安装目录下输入如下命令:export LDFLAGS=”–L/home –lnpc”
   这个环境变量的作用就是让PHP在编译时知道要把这个库也一起编译进去。
  
  编译PHP:
   和普通编译PHP没什么大区别,就是要在./configure的时候加上你的扩展,具体见编写PHP扩展部分。
   当编译好之后可以通过unset LDFLAGS来删除之前的环境变量。

php_screw

需要修改php_screw.c

把第78,84,93行的org_compile_file(file_handle, type);
修改为:
org_compile_file(file_handle, type TSRMLS_CC);

官方BUG列表:http://sourceforge.net/tracker/i ... 025&atid=465440

php反编译之路

http://hi.baidu.com/wizardchilde/blog/item/5d5b8738ffebd236b9998f26.html

file_get_contents获取返回状态

function  gethttpstatus ($Domain){ 
//请求文件    
$html = file_get_contents($Domain); 
//返回HTTP状态码 
return $http_response_header ; 
} 
Records:71123456789