InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Fetching a URL with the POST Method and Form Data in PHP
PHP

Fetching a URL with the POST Method and Form Data in PHP

InfinityCoder December 20, 2016

You want to submit a document using the POST method, passing data formatted as an HTML form.

Set the method and content stream context options when using the http stream:

1
2
3
4
5
6
7
8
9
10
11
$url = 'http://www.example.com/submit.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$options = array('method' => 'POST',
                 'content' => $body,
                 'header' => 'Content-type: application/x-www-form-urlencoded');
// Create the stream context
$context = stream_context_create(array('http' => $options));
// Pass the context to file_get_contents()
print file_get_contents($url, false, $context);

Using HTTP_Request2, pass HTTP_Request2::METHOD_POST to setMethod() and chain calls to addPostParameter() for each name/value pair in the data to submit:

1
2
3
4
5
6
7
8
9
10
require 'HTTP/Request2.php';
 
$url = 'http://www.example.com/submit.php';
$r = new HTTP_Request2($url);
 
$r->setMethod(HTTP_Request2::METHOD_POST)
    ->addPostParameter('monkey', 'uncle')
    ->addPostParameter('rhino','aunt');
 
$page = $r->send()->getBody();

Sending a POST method request requires different handling of any form arguments. In a GET request, these arguments are in the query string, but in a POST request, they go in the request body.

Additionally, the request needs a Content-Length header that tells the server the size of the content to expect in the request body.
Although they each have different mechanisms by which you specify the request method and the body content, each of the examples in the Solution automatically add the proper Content-Length header for you.
If you use a stream context to send a POST request, make sure to set the method option to post. Case matters.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Removing a Directory and Its Contents in PHP
You want to remove a directory and all of its …

Removing a Directory and Its Contents in PHP

Exposing Clean Resource Paths in PHP
You want your URLs to look clean and not include …

Exposing Clean Resource Paths in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap