{"id":5013,"date":"2023-03-24T18:39:00","date_gmt":"2023-03-24T17:39:00","guid":{"rendered":"https:\/\/ekiwi.de\/?p=5013"},"modified":"2023-03-24T18:40:35","modified_gmt":"2023-03-24T17:40:35","slug":"php-catch-and-forward-all-post-variables-from-script","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/en\/index.php\/5013\/php-catch-and-forward-all-post-variables-from-script\/","title":{"rendered":"PHP: Catch and forward all post variables from script"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5013\/php-catch-and-forward-all-post-variables-from-script\/#The_Script\" >The Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5013\/php-catch-and-forward-all-post-variables-from-script\/#How_the_script_works_and_how_to_use_it\" >How the script works and how to use it<\/a><\/li><\/ul><\/nav><\/div>\n<p>A PHP proxy script, e.g. for debugging.<\/p>\n<p><!--more--><\/p>\n<p>Suppose we have a form that sends data to a PHP script. The whole thing via POST. For certain purposes, it may be useful if we interpose. For example, this would allow us to evaluate the data, save it to a database, and then call the original script.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2023\/03\/proxy_1.png\" alt=\"\" width=\"580\" height=\"123\" class=\"alignnone size-full wp-image-5011\" srcset=\"\/wp-content\/uploads\/2023\/03\/proxy_1.png 580w, \/wp-content\/uploads\/2023\/03\/proxy_1-300x64.png 300w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_Script\"><\/span>The Script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here is an example of such a script:<\/p>\n<pre>&lt;?php\r\n\/\/ Set the URL of the destination PHP script\r\n$destination_url = &quot;https:\/\/example.com\/destination_script.php&quot;;\r\n\r\n\/\/ Check if any POST variables were sent\r\nif ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {\r\n  \/\/ Get all POST variables\r\n  $post_variables = $_POST;\r\n\r\n  \/\/ Create a new cURL resource\r\n  $curl = curl_init();\r\n\r\n  \/\/ Set the cURL options\r\n  curl_setopt($curl, CURLOPT_URL, $destination_url);\r\n  curl_setopt($curl, CURLOPT_POST, true);\r\n  curl_setopt($curl, CURLOPT_POSTFIELDS, $post_variables);\r\n  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);\r\n\r\n  \/\/ Execute the cURL request\r\n  $response = curl_exec($curl);\r\n\r\n  \/\/ Close the cURL resource\r\n  curl_close($curl);\r\n\r\n  \/\/ Output the response from the destination script\r\n  echo $response;\r\n} else {\r\n  \/\/ No POST variables were sent, output an error message\r\n  echo &quot;Error: No POST variables were sent.&quot;;\r\n}\r\n?&gt;<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"How_the_script_works_and_how_to_use_it\"><\/span>How the script works and how to use it<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this script, we first set the URL of the destination PHP script. We then check if any POST variables were sent using $_SERVER[&#8220;REQUEST_METHOD&#8221;], which will be &#8220;POST&#8221; if any POST variables were sent. If POST variables were sent, we use cURL to send a POST request to the destination script with the CURLOPT_POSTFIELDS option set to the $post_variables array, which contains all of the POST variables. We also set CURLOPT_RETURNTRANSFER to true to return the response from the destination script. Finally, we output the response from the destination script.<\/p>\n<p>Note that you may need to modify the cURL options depending on the specific requirements of your application.<\/p>","protected":false},"excerpt":{"rendered":"<p>A PHP proxy script, e.g. for debugging.<\/p>\n","protected":false},"author":1,"featured_media":4244,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[882],"tags":[],"class_list":["post-5013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-en"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/5013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/comments?post=5013"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/5013\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media\/4244"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media?parent=5013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/categories?post=5013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/tags?post=5013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}