0

I found a task to access my skills. The Task is to get a form from a given URL and fill the form. The form will have 3 input as Name, email, and a count and a square, combination of "x" & "". Fill all the fields, In the input box named count, I have to fill the count of "" and then I have to submit the form. I have to use PHP only.

The form should be submitted to the same URL where I get the form. I have tried using the cURL, but it returns the form again and not the expected output.

if(!empty($_POST)){
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    $data = $_POST;
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $out = curl_exec($ch);
    $inf = curl_getinfo($ch);
    curl_close($ch);

    echo "<pre>";
    print_r($out);
    print_r($inf);
    echo "</pre>";
    exit;
  }

After submitting the form I expect to get "Thanks for submitting." text. Instead, I get the same form as output.

4
  • That's quite amazing since $url is undefined. Commented Jul 22, 2019 at 9:50
  • Is there a requirement to actually read the form using php? If not, just send the request directly from curl. Commented Jul 22, 2019 at 9:59
  • @KIKOSoftware - Here I have posted only part of my code. If you want I'll post the entire code. Commented Jul 23, 2019 at 9:49
  • Don't bother, I think the answer by Shivendra Singh should solve your problem. Commented Jul 23, 2019 at 10:04

2 Answers 2

1

Maybe the issue is you are sending post field value in an array instead of URL encoded, use http_build_query or json_encode and check.

Check the below code.

if(!empty($_POST)){
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, true);
        $data = $_POST;

        /*** you are direct sending the array value 
        use http_build_query
         or
         $data_string = json_encode($data);
        ***/
        $data_string = http_build_query($data);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string );
        $out = curl_exec($ch);
        $inf = curl_getinfo($ch);
        curl_close($ch);

        echo "<pre>";
        print_r($out);
        print_r($inf);
        echo "</pre>";
        exit;
      }
Sign up to request clarification or add additional context in comments.

Comments

0

PHP can't do anything to forms. In that case, you should get the html form (curl is an option. Then parse the form to get the forms method (get/post), it's action (url), and all input names.

For that you could use something like xpath

From there, you submit another request, by crafting the data from given information above.

Comments

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.