{"id":13700115,"url":"https://github.com/bullsoft/PHPython","last_synced_at":"2025-05-04T18:34:23.763Z","repository":{"id":146481329,"uuid":"90715277","full_name":"bullsoft/PHPython","owner":"bullsoft","description":"PHPython: An extension to eval python3 codes in PHP","archived":false,"fork":false,"pushed_at":"2019-03-15T03:32:26.000Z","size":712,"stargazers_count":16,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-13T06:33:04.610Z","etag":null,"topics":["cpp11","php","php-cpp","php-extension","php-python","phppython","phpython","pybind11","python3"],"latest_commit_sha":null,"homepage":"http://phpython.bullsoft.org","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bullsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-09T07:22:59.000Z","updated_at":"2024-05-02T11:01:07.000Z","dependencies_parsed_at":"2023-07-14T04:45:30.665Z","dependency_job_id":null,"html_url":"https://github.com/bullsoft/PHPython","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bullsoft%2FPHPython","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bullsoft%2FPHPython/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bullsoft%2FPHPython/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bullsoft%2FPHPython/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bullsoft","download_url":"https://codeload.github.com/bullsoft/PHPython/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252382977,"owners_count":21739251,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp11","php","php-cpp","php-extension","php-python","phppython","phpython","pybind11","python3"],"created_at":"2024-08-02T20:00:48.762Z","updated_at":"2025-05-04T18:34:18.738Z","avatar_url":"https://github.com/bullsoft.png","language":"C++","readme":"# PHPPython\nAn extension to eval python codes in PHP\n\n## Requirements\n   - pybind11 V2.1.1\n   - PHP-CPP-LEGACY V1.5.7 / PHP-CPP V2.x\n   - PHP 5 / PHP 7\n   - Python 3\n   - C++ 11\n\n\n## Example\n\n### Variables defined in Python\n```php\n$code = \u003c\u003c\u003cEOD\na = [1, 2, 3]\nEOD;\n$python = new Python();\n$python-\u003eeval($code);\nvar_export($python-\u003eextract(\"a\"));\n```\n... you can use `extract` method in php to get that python variable, codes above output:\n```\narray (\n  0 =\u003e 1,\n  1 =\u003e 2,\n  2 =\u003e 3,\n)\n```\n\n### Variables defined in PHP\n```php\n$a = [\"a\" =\u003e \"b\", \"c\" =\u003e \"d\"];\n$code = \u003c\u003c\u003cEOD\nprint(a)\nEOD;\n\n$python = new Python();\n$python-\u003eassign(\"a\", $a);\n$python-\u003eeval($code);\n```\n... you can use `assign` method in php to let python know that php variable, codes above output:\n```\n{'a': 'b', 'c': 'd'}\n```\n\n### Functions defined in Python\n```php\n$a = [\"a\" =\u003e \"b\", \"c\" =\u003e \"d\"];\n$code = \u003c\u003c\u003cEOD\ndef dofunc(arg1, arg2):\n   print(\"Python Output\")\n   print(arg1)\n   print(arg2)\n   return {\"Python\": {\"a\": arg1, \"b\": arg2}}\n\nafter = \"abcd\"\n\nEOD;\n\n$python = new Python();\n$python-\u003eeval($code);\n$python-\u003eassign(\"tmp\", $a);\nvar_dump(\"PHP Here...\", $python-\u003edofunc($a, \"py::tmp\"));\n```\n... you can call functions defined in Python as-is-a `$python-\u003emethod()`, codes above output:\n\nPython Output\n```\n{'a': 'b', 'c': 'd'}\n{'a': 'b', 'c': 'd'}\nstring(11) \"PHP Here...\"\narray(1) {\n  'Python' =\u003e\n  array(2) {\n    'a' =\u003e\n    array(2) {\n      'a' =\u003e\n      string(1) \"b\"\n      'c' =\u003e\n      string(1) \"d\"\n    }\n    'b' =\u003e\n    array(2) {\n      'a' =\u003e\n      string(1) \"b\"\n      'c' =\u003e\n      string(1) \"d\"\n    }\n  }\n}\n```\nHere, We can also use these ways to call functions defined in Python:\n```php\nvar_dump(\"PHP Here...\", $python-\u003ecall(\"dofunc\", [$a, \"py::tmp\"]));\nvar_dump(\"PHP Here...\", $python-\u003ecall(\"dofunc(after, tmp)\"));\n```\nthis will output:\n```\nPython Output\n{'a': 'b', 'c': 'd'} // $a in php\n{'a': 'b', 'c': 'd'} // tmp in python which assigned by php\nstring(11) \"PHP Here...\"\narray(1) {\n  'Python' =\u003e\n  array(2) {\n    'a' =\u003e\n    array(2) {\n      'a' =\u003e\n      string(1) \"b\"\n      'c' =\u003e\n      string(1) \"d\"\n    }\n    'b' =\u003e\n    array(2) {\n      'a' =\u003e\n      string(1) \"b\"\n      'c' =\u003e\n      string(1) \"d\"\n    }\n  }\n}\nPython Output\nabcd // after in python\n{'a': 'b', 'c': 'd'} // tmp in python which assigned by php\nstring(11) \"PHP Here...\"\narray(1) {\n  'Python' =\u003e\n  array(2) {\n    'a' =\u003e\n    string(4) \"abcd\"\n    'b' =\u003e\n    array(2) {\n      'a' =\u003e\n      string(1) \"b\"\n      'c' =\u003e\n      string(1) \"d\"\n    }\n  }\n}\n```\n\n### Functions defined in PHP\n```php\n$code = \u003c\u003c\u003cEOD\ntmp = {'a': 'b', 'c': 'd'}\nprint(\"Python begin\")\nprint(dofunc(tmp))\nprint(\"Python end\")\nEOD;\n\n$python = new Python();\n$python-\u003edef(\"dofunc\", function($param) {\n    echo __function__ . \" in PHP: Get params from Python :\" . PHP_EOL;\n    echo var_export($param, true) . PHP_EOL;\n    return [\n        \"php\" =\u003e $param\n    ];\n});\n$python-\u003eeval($code);\n```\n... you can call php funciton in python as-is-a real python function, codes above output:\n```\nPython begin\n{closure} in PHP: Get params from Python :\narray (\n  0 =\u003e\n  array (\n    'a' =\u003e 'b',\n    'c' =\u003e 'd',\n  ),\n)\n{'php': [{'a': 'b', 'c': 'd'}]}\nPython end\n```\nif you like, you can also call that function using the php way, like this:\n```php\nvar_dump($python-\u003edofunc(\"py::tmp\", \"phpString\"));\n```\nthis will output:\n```\n{closure} in PHP: Get params from Python :\narray (\n  0 =\u003e\n  array (\n    'a' =\u003e 'b',\n    'c' =\u003e 'd',\n  ),\n  1 =\u003e 'phpString',\n)\narray(1) {\n  'php' =\u003e\n  array(2) {\n    [0] =\u003e\n    array(2) {\n      'a' =\u003e\n      string(1) \"b\"\n      'c' =\u003e\n      string(1) \"d\"\n    }\n    [1] =\u003e\n    string(9) \"phpString\"\n  }\n}\n```\n\n### PHP Call Python Function\n\n![PHP Call Python Function](https://raw.githubusercontent.com/bullsoft/PHPPython/master/examples/php_call_python_func.png)\n\n### Python Call PHP Function\n\n![Python Call PHP Function](https://raw.githubusercontent.com/bullsoft/PHPPython/master/examples/python_call_php_func.png)\n\n","funding_links":[],"categories":["跨语言"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbullsoft%2FPHPython","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbullsoft%2FPHPython","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbullsoft%2FPHPython/lists"}