承接 raeen/phpy 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

raeen/phpy

Composer 安装命令:

composer require raeen/phpy

包简介

Use python in php very easy

README 文档

README

icon

phpy is library for php

You can use python in php with it

Latest Stable Version Total Downloads Latest Unstable Version LicenseMonthly Downloads Daily Downloads

Features

  • Show all output from python file
  • Show last line output from python
  • Send data to python file
  • Get data from python file
  • show image in php from python
  • path Genrate
  • write python line code
  • mange python line code
  • write python part
  • require python part

Install

git clone https://github.com/Raeen123/phpy
composer require raeen/phpy

Prerequisites

This is mine config:

php = 8.0.1 (Your php version must be at least 7.4 )
python = 3.9.1
numpy = 1.19.3
python-opencv =  4.4.0.46

Python

You must if you don't add python in your path environment , add yor python path in core/Python.php in const python_path

We use these libraries for create phpy.py:

import sys
import json
import base64
import numpy as np
import cv2

Import phpy.py file in Python/include/library/php.py

Get Data from php

Get datas to python file For Get data you must

phpy.get_data( Number Of Send data )

For return data you must use print function

Push data from python

phpy.push_data(data)

It's just

json.dumps(data)

Push image from python

It function for pushing data from python to php

Example for reading image

cv2.imread()
videoCaptureObject = cv2.VideoCapture(0)
ret, frame = videoCaptureObject.read()

Pushing image

phpy.push_image(img,type)

Send data from php

Create and Send data from php to python

You can send infinite data

require_once "../vendor/autoload.php";

use app\core\App;

$app = new App();
$python = $app->python;

$data1 = [
    'name' => 'raeen',
    'library' => 'phpy'
];
$data2 = "test";
$output = $python->set("../Python/test2.py")->send($data1, $data2)->gen();

live output example:

$site = "google.com";
$python->set('../Python/test6.py')->send($data)->live()->gen();

Show result

$output = $python->set(path)->send(datas..)->gen()

Show Img

You can use this function to genrate what's return from phpy.push_img()

$app->img($output,$type,$show,$style)

$type must be same type in php.push_img()

If $show is true , show image in img tag

Also you can set style for this

Example

$app->img($output,$type,true,
[
    'border' => '1px solid red'
])

Path

For example , I have image file in this diractory but python file in Python/**.py and I want to send path to it . for this in must to send this path ../my-img Or use this functiuon for send path file or directory

$app->path(__Dir__,path)

Ini

If you have loop in php file it's very good to add this function in top of file

$app->ini()

Snippet

One Line

If you want to run a python line , you should use this function

$Snippet->set($code,function(){ 
    // controller 
})->gen();

For control varable , you should it's name in |&name|

For fill varable you should return array , it must be in order

You must use " in python code and use ; in the end of each line

$Snippet = $app->snippet;
$Snippet->set("print(f'hello world {|&data|*7*|&test|}'); print('--Hello')",
function ($data,$test) {
    $data = 2;
    $test = 9;
    $data2 = $data*5;
    return [$data2 , $test];
})->gen();

Lines

start Before start writing codes you should use this

$Snippet->start(name);

For write your codes , you should use this

$Snippet->line(code)
.
.
.

For end of lines

$Snippet->end(name,save_last)

name in start function must be same this name

For get output anywhere you should use this

$Snippet->select(name)->gen()

Example , index.php :

$Snippet->start("test");
$Snippet->line("a = 'Hello world'");
$Snippet->line("print(a)");
$Snippet->end("test");
$Snippet->select("test")->gen();
Output : Hello world 

You can use this code in another file

$Snippet->select("test")->gen();
Output : Hello world 

Live Output Any where (Snippet and Python class) you can use this so easy

just before use gen() use live() then use ```gen(/* you can write function here to control output */)

$python->set("../Python/test6.py")->send($site)->live()->gen(
    function ($res) {
        return "<pre>$res</pre>";
    }
);

you can also set time without each reading ouput (defult is 1)

$python->set("../Python/test6.py")->send($site)->live()->ini(3)->gen(
    function ($res) {
        return "<pre>$res</pre>";
    }
);
$Snippet->set(
    "import subprocess as sub; sub.call('ping |&site|') ",function($site){ $site = "google.com"; return [$site];}
)->live()->ini(3)->gen();
$Snippet->select('snippet-test8')->live()->gen(function($res){ return "<b><pre>$res</pre></b>";});

License

MIT License

Copyright (c) 2021 Raeen Ahani Azari

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Example

There are examples in phpy-examples

BY RAEEN AHANI AZARI

raeen/phpy 适用场景与选型建议

raeen/phpy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 198 次下载、GitHub Stars 达 26, 最近一次更新时间为 2021 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 raeen/phpy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 raeen/phpy 我们能提供哪些服务?
定制开发 / 二次开发

基于 raeen/phpy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 198
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-16