Trying to post details to a third party form

I am trying to automate form submission and struggling to work out why it simply isn't working as I believe I have replicated what the browser does using PHP & curl.

Unfortunately the browser always ends up redirecting to an empty form.

Any pointers would be gratefully received!

Thanks in advance

The web form can be found https://www.abelliogreateranglia.co.uk/contact-us/contact-forms/delay-repay

The form itself posts to https://www.abelliogreateranglia.co.uk/_webformhandler

I believe my code successfully captures the cookies and "_token" required for posting.

My Curl Request looks OK

POST /_webformhandler HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36 Host: www.abelliogreateranglia.co.uk Referer: https://www.abelliogreateranglia.co.uk/contact-us/contact-forms/delay-repay Cookie: coanda_session=eyJpdiI6IjZmdXpKV2pGMWVEVlZaeE9rOHFFeUxKVDVMUUpJWUdQUkZqTDd3bUcwZlE9IiwidmFsdWUiOiJ6TGFsYWg5Z2I5N3E3YkRvaGR2eFBMXC9iWUoxalhsOEtvdGtLTXpMdEszWThmV1Y5OXJGSkhsczRQdERLVERYYUZIeFlKZ0RBSG5oTjk3dWdRZjd6U0E9PSIsIm1hYyI6IjdmNDRmOTZkODFmNDEyYzA3YTAwMmZhZmQ5OTkzN2VjMGMyMDdmYmJkZmVmNjNjMmUzMzVhYzQyZDEyOWJjOTYifQ%3D%3D; X-Mapping-fjhppofk=49656D7E5E95FCEF85BAACF2976B580C Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Connection: keep-alive Accept-Language: en-US,en;q=0.8 Cache-Control: no-cache Origin: https://www.abelliogreateranglia.co.uk Pragma: no-cache Content-Length: 2606 Expect: 100-continue Content-Type: multipart/form-data; boundary=----------------------------e09ebcd7f8fd

And the response is always

string(754) "HTTP/1.1 100 Continue HTTP/1.1 302 Found Server: nginx/1.4.6 (Ubuntu) Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.5.9-1ubuntu4.6 Cache-Control: no-cache Date: Mon, 22 Jun 2015 09:55:28 GMT Location: https://www.abelliogreateranglia.co.uk/contact-us/contact-forms/delay-repay Set-Cookie: coanda_session=eyJpdiI6InpFWmJ1Tk9yVkd6NmVUSW1qYXlxOVRRejNQS0dpY3dHd1J6bGJuSW9iRFU9IiwidmFsdWUiOiJTbUFSRDBIaEZ1V0RpQzFmUmJndDlrU0NaNG9NV2dMcGg0ajZPQStsSW1Ic1c4RXA4aEZiY2xRekVwMGZ5OEZZRHJFSWpOQ01udFl3QTl3TTA5UHJ3QT09IiwibWFjIjoiZmEyMTM0MGE4OWQ5NDViZDA5OWRkOGU1MmI1NTM1ZDRmMDRmYmZlZjAxNTNiNzcwZGY5NTIzNDMxNDlkMDEyZSJ9; expires=Mon, 22-Jun-2015 11:55:28 GMT; Max-Age=7200; path=/; httponly

Below is a snippet of my code:

<?php $debug = isset($_GET['debug']); // options $cookie_file_path = "/tmp/cookies".rand().".txt"; $LOGINURL = "https://www.abelliogreateranglia.co.uk/contact-us/contact-forms/delay-repay"; $POSTURL = "https://www.abelliogreateranglia.co.uk/_webformhandler"; $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36"; // begin script $ch = curl_init(); // extra headers $headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; $headers[] = "Connection: keep-alive"; // curl options curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLINFO_HEADER_OUT, 1); curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //curl_setopt($ch, CURLOPT_COOKIESESSION, 0); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // set first URL curl_setopt($ch, CURLOPT_URL, $LOGINURL); // execute session to get cookies and required form inputs $content = curl_exec($ch); $input_fields = array(); $doc = new DOMDocument(); $doc->loadHTML($content); $elements = $doc->getElementsByTagName( "input" ); if ($elements->length==0) { // no results echo "No dom input nodes"; } else { foreach ($elements as $element) { $input_fields[ $element->getAttribute('name') ] = $element->getAttribute('value'); } } $token = ""; $elements = $doc->getElementsByTagName( "select" ); if ($elements->length==0) { // no results echo "No dom select nodes"; } else { foreach ($elements as $element) { $input_fields[ $element->getAttribute('name') ] = $element->getAttribute('value'); } } ksort( $input_fields, SORT_STRING | SORT_FLAG_CASE ); foreach( $input_fields as $key => $val ) { if ( $key == "_token" ) { $token = $val; } } // grab the inputs for the form $fields = array(); $fields['_token']=$token; $fields['form_id']="6"; $fields['page_id']="1134"; $fields['field_132'] = "Other"; $fields['field_133'] = "John"; $fields['field_134'] = "Doe"; $fields['field_135'] = ""; $fields['field_136'] = ""; $fields['field_137'] = "30 mins+"; $fields['field_138'] = ""; $fields['field_139'] = ""; $fields['field_140'] = ""; $fields['field_257'] = ""; $fields['field_143'] = ""; $fields['field_144[hour]'] = "0"; $fields['field_144[minute]'] = "0"; $fields['field_147'] = ""; $fields['field_259'] = ""; $fields['field_260'] = ""; $fields['field_150'] = "Standard Class"; $fields['field_258'] = ""; $fields['field_149'] = "Annual Season Ticket"; $fields['field_152'] = ""; //$fields['field_153'] = "@"; $fields['submit_form'] = ""; // change URL curl_setopt($ch, CURLOPT_URL, $POSTURL); // set post options curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_REFERER, $LOGINURL); curl_setopt($ch, CURLINFO_HEADER_OUT, 1); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); //$headers[] = "Accept-Encoding: gzip, deflate"; $headers[] = "Accept-Language: en-US,en;q=0.8"; $headers[] = "Cache-Control: no-cache"; $headers[] = "Origin: https://www.abelliogreateranglia.co.uk"; $headers[] = "Pragma: no-cache"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // perform post $result = curl_exec($ch); if (curl_errno($ch)) { echo "Curl Error<BR><pre>"; var_dump( curl_error($ch)); echo "</pre><BR><BR>"; } if ( $debug ) { echo "Curl POST Header is<BR><pre>"; var_export(curl_getinfo($ch,CURLINFO_HEADER_OUT)); echo "</pre><BR><BR>"; } $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($result, 0, $header_size); $body = substr($result, $header_size); if ( $debug ) { echo "Curl RESPONSE Header is<BR><pre>"; var_dump( $header ); echo "</pre><BR><BR>"; } /* */ if ( $debug ) { echo "Curl RESPONSE body is<BR><pre>"; } print_r( $body ); if ( $debug ) { echo "</pre><BR><BR>"; } /* */ ?>

Category:php Views:5 Time:2019-02-11

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.174 (s). 11 q(s)