目录

PHP-python-Apache-在PHP-项目中-调用-python-接口

目录

PHP +python +Apache (在PHP 项目中 调用 python 接口)

在PHP 项目中 调用 python 接口:

安装 Python36-32.exe;然后做如下的配置:

1.cd easy_install.exe 所在目录(C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\Scripts)

2.easy_install.exe pip   – 安装pip 模块

3.cd 项目所在目录  — 接口项目

4.通过pip安装依赖库: pip install -r requirements.txt   — 接口 项目相关的依赖包

5.运行项目

  1. 配置python 环境   —-解决 浏览器中 python 不是命令的问题

httpd.conf  中

找到“Options Indexes FollowSymLinks ”这句,在后面加上”ExecCGI “。(那个”Indexes “最好也能去掉,因为如果不去掉的话,目录下面没有index.html这种文件,就会显示出目录列表下的所有文件,所以有安全漏洞问题。如果去掉Indexes,而且没有index.html文件的话,就会显示一个Forbidden页面)

找到”AddHandler cgi-script .cgi “,在后面加上 .py。如果没有这句,可以自己添加 “AddHandler cgi-script .py”

重新启动Apache即可

访问testpy.php  :

<?php
/**
 * Created by PhpStorm.
 * User: yl1295
 * Date: 2017-10-19
 * Time: 9:13
 */
$cmd="python -h";
$descriptorspec = array(
    0 => array("pipe", "r"),    // stdin
    1 => array("pipe", "w"),    // stdout
    2 => array("pipe", "w")     // stderr
);
$proc = proc_open($cmd, $descriptorspec, $pipes, null, null);
if (is_resource($proc)) {
    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);
    $status = proc_close($proc);  // 释放proc
} else {
    $stderr = "返回非资源文件";
    $status = -1;
}
$msg = array(
    'out' => $stdout,
    'info' => $stderr,
    'code' => $status
);

echo $msg["out"]."a\n";
echo $msg["info"]."b\n";
echo $msg["code"]."c\n";

能够正常显示如图即可!

https://img-blog.csdn.net/20171026133221524?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIwNzEwMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

https://img-blog.csdn.net/20171026133201237?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIwNzEwMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center