jamacoe
jamacoe

Reputation: 549

Execute php locally and pass $_POST

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&form_email=george%40example.com&form_message=Something%20new&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?

Upvotes: 0

Views: 213

Answers (1)

Timo
Timo

Reputation: 165

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.

Upvotes: 2

Related Questions