{"id":128,"date":"2014-11-26T05:04:11","date_gmt":"2014-11-26T05:04:11","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=128"},"modified":"2014-11-28T15:23:16","modified_gmt":"2014-11-28T15:23:16","slug":"simulating-a-form-post-using-curl","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/php\/simulating-a-form-post-using-curl\/","title":{"rendered":"Simulating a form submit using POST method through CURL"},"content":{"rendered":"<p>Below piece of code can simulate a form submission through post method.<\/p>\n<pre class='wp-code-highlight prettyprint'>\r\n\/******** The below header simulates Chrome Browser ********\/\r\n\r\n$header[0] = \"Accept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,*\/*;q=0.8\";\r\n\r\n$header[1] = \"Accept-Language: en-US,en;q=0.8\"; \r\n$header[2] = \"Cache-Control: max-age=0\";\r\n$header[3] = \"Connection: keep-alive\";\r\n$header[4] = \"Host: www.doamin-name.com\";\r\n\r\n$header[5] = \"User-Agent: Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/38.0.2125.104 Safari\/537.36\";\r\n\r\n$header[6] = \"Referer: http:\/\/www.referrer-domain.com\/\"; \r\n\r\n\/***********  end of header **********************\/\r\n\r\n\r\n$url = \"http:\/\/www.doamin-name.com\/page.php\";\r\n$param = \"param=value1&submit=Submit\"; \/\/ params that need to be passed\r\n       \r\n$ch = curl_init() or die(curl_error()); \r\ncurl_setopt($ch, CURLOPT_POST, 1);\r\ncurl_setopt($ch, CURLOPT_POSTFIELDS, $param);\r\ncurl_setopt($ch, CURLOPT_URL,$url);\r\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $header); \r\n\r\n\/\/curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); \/\/ would be required if the return is going to be a binary file like image. From PHP ver 5.1.3 onwards the return is always binary so need not set this if PHP ver is >= 5.1.3\r\n\r\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \/\/returns the received data in a variable. Must be on if CURLOPT_BINARYTRANSFER is set to 1 or true\r\n\r\n\/\/curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']); \/\/ if required. This sets Cookies to be sent with the request\r\n\r\n$data1=curl_exec($ch) or die(curl_error($ch));\r\ncurl_close($ch);\r\n<\/pre>\n<p>If PHP Session Cookie needs to be retrieved from the result of the above request:<\/p>\n<pre class='wp-code-highlight prettyprint'>\r\npreg_match('\/^Set-Cookie:\\s*([^;]*)\/mi', $data1, $m);\r\nparse_str($m[1], $cookies);\r\n\r\necho $cookies['PHPSESSID'];\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below piece of code can simulate a form submission through post method. \/******** The below header simulates Chrome Browser ********\/ $header[0] = &#8220;Accept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,*\/*;q=0.8&#8221;; $header[1] = &#8220;Accept-Language: en-US,en;q=0.8&#8221;; $header[2] = &#8220;Cache-Control: max-age=0&#8221;; $header[3] = &#8220;Connection: keep-alive&#8221;; $header[4] = &#8220;Host: www.doamin-name.com&#8221;; $header[5] = &#8220;User-Agent: Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/38.0.2125.104 Safari\/537.36&#8221;; $header[6]&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/php\/simulating-a-form-post-using-curl\/\">Continue reading <span class=\"screen-reader-text\">Simulating a form submit using POST method through CURL<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-php","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":5,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/128\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}