A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::open($save_path, $name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 132

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 290

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::read($session_id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 164

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::write($session_id, $session_data) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 233

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::destroy($session_id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 313

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 354

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 296

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Session cannot be started after headers have already been sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

Logo
Code Ranks ×

PHP cURL Part 4(Form Submission)

08/04/2019  .   1 minute, 51 seconds to read  .   Posted by Admin
#php #curl #phpcurl

So far, we've learned the following features of PHP cURL in this series of articles.

  1. PHP cURL Basics (1st part)
  2. Getting Contents of any URL (2nd part)
  3. Downloading File from URL (3rd part) 
  4. Checking URL existence

In this article, we'll learn how to submit any form using PHP cURL. There are many cases where we need to submit any form automatically without user interaction. For example, login pages are forms and we can submit those forms and login into most of the sites automatically using PHP cURL.

Following code snippet is a very basic example of form submission using PHP cURL. 

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://httpbin.org/post");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$data = array(
    'first_name' => 'John',
    'last_name' => 'Smith',
    'user_name' => 'john',
    'email' => 'john.smith@gmail.com',
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
echo '<pre>',$result;

Explanation:

Most of the cURL options have been discussed in our previous parts of the article. Here is a short description

  1. CURLOPT_URL: used set target URL.
  2. CURLOPT_RETURNTRANSFER: used to direct cURL to not display response(default) but return after execution.
  3. CURLOPT_SSL_VERIFYPEER: used to enable or disable SSL.

We've used two new options in this article.

CURLOPT_POST

To submit the form we normally use post method. CURLOPT_POST is used to tell PHP cURL that it's a post request or not.

CURLOPT_POSTFIELDS

This option is used to set the form data in the request.

Response:

HTTP Bin is used to test HTTP requests. It returns the request data as a response, you can see we got all of our data in the response which means PHP cURL has submitted form successfully.

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "email": "john.smith@gmail.com", 
    "first_name": "John", 
    "last_name": "Smith", 
    "user_name": "john"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "468", 
    "Content-Type": "multipart/form-data; boundary=------------------------cd2b860537d5efbd", 
    "Host": "httpbin.org"
  }, 
  "json": null, 
  "origin": "103.255.6.78, 103.255.6.78", 
  "url": "https://httpbin.org/post"
}

That's all for this part of the article. In the next part, we'll learn Authentication using PHP cURL.

Thanks for reading.