1

I want to call PHP script from my Python script

I have this code

subprocess.run(['php', "script.php", long_string], stdout=subprocess.PIPE)

but I am getting error

OSError: [Errno 7] Argument list too long: 'php'

I have read online threads that I should always use subprocess.run for Python 3+

I have also tried

subprocess.run(['ulimit', '-s', 'unlimited', 'php', "script.php", long_string], stdout=subprocess.PIPE, shell=True)

But then I get

OSError: [Errno 7] Argument list too long: '/bin/sh'

My string is 141,664 characters = 141,706 bytes and that can get larger too

What should I do? How to surpass length error for my Python script?

My uname -a output is

Linux mani 2.6.32-042stab123.9 #1 SMP Thu Jun 29 13:01:59 MSK 2017 x86_64 x86_64 x86_64 GNU/Linux

Umair Ayub
  • 19,358
  • 14
  • 72
  • 146

1 Answers1

1

Thanks to @Andras Deak for pointing me to right direction, solution was to to send and read data from STDIN instead of Command Line

WORKING SOLUTION

Python code

subprocess.run(['php', "script.php"], input=long_string.encode("utf-8"), stdout=subprocess.PIPE)

PHP code

//to receive data from our Python scrapers
if (defined('STDIN')) {
    $post = json_decode(fgets(STDIN), true);
} 

Old code (not working)

Python code

subprocess.run(['php', "script.php", long_string], stdout=subprocess.PIPE)

PHP code

//to receive data from our Python scrapers
if (defined('STDIN')) {
    $post = json_decode($argv[1], true);
} 
Umair Ayub
  • 19,358
  • 14
  • 72
  • 146