randomsymbols/fsubmit

Submit HTML forms from PHP

2.9.1 2022-03-28 14:43 UTC

This package is auto-updated.

Last update: 2024-03-29 04:18:55 UTC


README

Have you ever tried to submit an HTML form with cUrl? You have to clearly state all the fields you submit and what values they have.

In real life, we most often only want to fill in one or two fields without even thinking about what other fields are.

If you hardcode the other fields' values into your cUrl request, what if the form changes over time? You code will be broken.

To keep you code adoptable to the changes of the form, you will have to download the form as is first, parse its fields and values, change/add values to the right fields and submit it with cUrl.

It causes a lot of questions if you do not know how HTML forms work. For example, if there is a select tag with several options, which one will be submitted as the value for the field if none is selected? What if the option tag has no value attribute?

An Internet browser does the job for us when we submit a form. We do not have to bother about hidden fields or any other fields at all. The library provides the same functionality for PHP.

Requirements

PHP 7.4 and later.

Composer

You can install the library via Composer. Run the following command:

composer require randomsymbols/fsubmit

To use the library, use composer's autoload:

require_once('vendor/autoload.php');

Dependencies

The library requires the following extensions in order to work properly:

If you use Composer, these dependencies should be handled automatically. If you install manually, make sure these extensions are available.

Getting Started

use Fsubmit\Form;

$form = Form::fromUrl('https://www.google.com');
$form->setParams(['q' => 'John 3:16']);
$answer = $form->submit();
echo $answer['content'];