Home > front end >  Execute php locally and pass $_POST
Execute php locally and pass $_POST

Time:09-23

I have a php script on a web server that's called over the internet and then accesses the $_POST array to retrieve the passed data. Beside calling this script from the web page (<form action="form.php" method="post">) I'm also able to call it from a local bash script running on the server:

postData='form_name=Georg&[email protected]&form_message=Something new&id_button=true'
curl -X POST -d $postData https://example.com/form.php

Now I want to get rid of curl and call the script directly with the data I have prepared, like:

php /var/www/example/form.php -- $postData

How do I prepare and pass the $postData correctly, such that I don't have to change the script and can access the passed data, like $_POST['form_email'], as though called over the internet?

CodePudding user response:

You can't. Not without modifying your code that is; $_POST is automatically populated by PHP. Without changing too much code what you can do is add the following to the top of form.php:

if (php_sapi_name() === 'cli') {
    parse_str($argv[1], $_POST);
}

For the remainder of your script you should be able to use $_POST as if it was called via web.

  •  Tags:  
  • php
  • Related