sys.argv variable passed from shell_exec got short

2019-03-05 12:07发布

i am having quite a trouble when i want to execute my python script from php shell_exec() as the title says

This is my code
exec.php

<?php
$my_url="http://reviews.femaledaily.com/moisturizer-36/lotions/nature-republic/aloe-vera-92-soothing-gel-06?tab=reviews&cat=&cat_id=0&age=&order=nrd&page=1";
echo shell_exec("python hello.py '".$my_url."'");
?>

hello.py

import sys
x = sys.argv[1]
print(x)

output

'http://reviews.femaledaily.com/moisturizer-36/lotions/nature-republic/aloe-vera-92-soothing-gel-06?tab=reviews

what i want expect from the output

'http://reviews.femaledaily.com/moisturizer-36/lotions/nature-republic/aloe-vera-92-soothing-gel-06?tab=reviews&cat=&cat_id=0&age=&order=nrd&page=1'

Is there any solution to this ? or i should split it into 2 argument ?

1条回答
Juvenile、少年°
2楼-- · 2019-03-05 12:41

Use escapeshellarg() to properly escape the argument to a command.

echo shell_exec("python hello.py " . escapeshellarg($my_url));
查看更多
登录 后发表回答