0

I have an online form to submit data as http post. If no data is send as post , it will return the following error message

We are sorry, the form that you have submitted is invalid or no longer exists.

I am using the following code to send http post data using curl.But I am always getting the error message as output. How to resolve this issue.Did I miss anything.I printed the curl request, and the post parameters are not sent.How can I fix this? My code is given below

function curlUsingPost($url, $data)
{
  $content_type = 'application/x-www-form-urlencoded';

    if(empty($url) OR empty($data))
    {
        return 'Error: invalid Url or Data';
    }

    
    //url-ify the data for the POST
    $fields_string = '';
    foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    $fields_string = http_build_query($data).'\n';
    echo $fields_string;


    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
 //   curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: ' . $content_type]);
 
 curl_setopt($ch, CURLOPT_HTTPHEADER,array($content_type));
    curl_setopt($ch,CURLOPT_URL,$url);
    
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); # timeout after 10 seconds, you can increase it
   //curl_setopt($ch,CURLOPT_HEADER,false);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  # Set curl to return the data instead of printing it to the browser.
   curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); # Some server may refuse your request if you dont pass user agent

   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
   
   
   $info = curl_getinfo($ch);
    
   print_r($info );
    //execute post
    $result = curl_exec($ch);



    //close connection
    curl_close($ch);
    return $result;
}


$url = 'office.promptip.com:8443/his1.max/Services/Webform.aspx?request=submitwebform&token=202D36560E444E466658461615120F75411B09634C696C6F79454E795E7C7A5154505645414F456C584C17101B443D431D0B654D66686D7C454E795E2D7E5152565341404145625E4213141E4273421E5E66486B666F2A464B7A5B79785F54520645444F15635C454516134422421A0D664A3D68687C454E79587F7D5F52575714424043635E1611181D16704D1D0E654F69696F2A474A7A0A78785807525E45414E406350424016134422421A0D374C6F686C7C44482F59787D5E530150104218426D584316181A4177411B0F';
//echo $url;
$data = [
'C0IFirstName' => 'John Doe',
'C1ILastName' => 'Doe John',
'C2IPosition' =>'Father-Guardian',
'U3I80'=>'8129020464',
'U4I150'=>'[email protected]',
'U5I79'=>'This is the message buddy',
'U6I102' => 'Ann',
'U7I105' =>'Doe',
'U8I52' =>'FS1',
'C9IClientId'=> rand ( 0,100000 ),
'U10I21'=>'Website-Enquiry Form'

];

echo curlUsingPost($url,$data);

6
  • curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, array( 'Expect:' ) ); is wrong - the handle should be $ch in this case Commented Aug 13, 2015 at 9:38
  • ya I removed Expect: .Still it is not working.(Expect was added because of suggestion from an online link) Commented Aug 13, 2015 at 9:39
  • You can use implode function to concatenate the postfields instead of putting '&' at end, as this will end up in a extra '&' at last. Commented Aug 13, 2015 at 9:40
  • you can increase the timeout time. Commented Aug 13, 2015 at 9:40
  • I'm unable to see the particular form that you are trying to submit on the url given - is it beyond the login page? Commented Aug 13, 2015 at 9:44

1 Answer 1

1

Solution

Add this to your curl request:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

And replace & with & in URL you're requesting:

$url = 'office.promptip.com:8443/his1.max/Services/Webform.aspx?request=submitwebform&token=202D36560E444E466658461615120F75411B09634C696C6F79454E795E7C7A5154505645414F456C584C17101B443D431D0B654D66686D7C454E795E2D7E5152565341404145625E4213141E4273421E5E66486B666F2A464B7A5B79785F54520645444F15635C454516134422421A0D664A3D68687C454E79587F7D5F52575714424043635E1611181D16704D1D0E654F69696F2A474A7A0A78785807525E45414E406350424016134422421A0D374C6F686C7C44482F59787D5E530150104218426D584316181A4177411B0F';

Explenation

At first, you're requesting wrong URL, which was probably copied from some HTML documentation. Normally a & entity would be replaced by & char by HTML interpreter, but this does not work with curl and you have to replace the entity manually.

Also the server returns

We are sorry, the form that you have submitted is invalid or no longer exists.

for each end every request in response body. But for successfull request it also provides a Location header. Normally browser would follow this location, make a second request and display success message. You have to tell cURL to do exactly the same with curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true).

Sign up to request clarification or add additional context in comments.

4 Comments

Hi pamelus.Thank you for the response.Let me try this.Actually I was travelling.That's why I couln't respond immedietely
Hi Pamelus.One more issue..It is working in my local mamp.But on server, it is returning nothing
What does the curl_error($ch) say? Remember to put curl_error after curl_exec.
It says couldn't connect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.