Recipe 13.2. Fetching a URL with the Post Method


13.2.1. Problem

You want to retrieve a URL with the post method, not the default get method. For example, you want to submit a form.

13.2.2. Solution

Set the method and content stream context options when using the http stream, as in Example 13-14.

Using POST with the http stream

<?php $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); // 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); ?>

With cURL, set the CURLOPT_POST and CURLOPT_POSTFIELDS options, as in Example 13-15.

Using POST with cURL

<?php $url = 'http://www.example.com/submit.php'; // The submitted form data, encoded as query-string-style // name-value pairs $body = 'monkey=uncle&rhino=aunt'; $c = curl_init($url); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_POSTFIELDS, $body); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $page = curl_exec($c); curl_close($c); ?>

Example 13-16 shows how to post with HTTP_Request: pass HTTP_REQUEST_METHOD_POST to the constructor and call addPostData( ) once for each name/value pair in the data to submit.

Using POST with HTTP_Request

<?php require 'HTTP/Request.php'; $url = 'http://www.example.com/submit.php'; $r = new HTTP_Request($url); $r->setMethod(HTTP_REQUEST_METHOD_POST); $r->addPostData('monkey','uncle'); $r->addPostData('rhino','aunt'); $r->sendRequest(); $page = $r->getResponseBody(); ?>

13.2.3. Discussion

Sending a post method request requires special handling of any 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.

Retrieving a URL with post instead of get is especially useful if the get query string is very long, more than 200 characters or so. The HTTP 1.1 specification in RFC 2616 doesn't place a maximum length on URLs, so behavior varies among different web and proxy servers. If you retrieve URLs with get and receive unexpected results or results with status code 414 ("Request-URI Too Long"), convert the request to a post request.

13.2.4. See Also

13.1 for fetching a URL with the get method; documentation on curl_setopt( ) at http://www.php.net/curl-setopt and on stream options at http://www.php.net/wrappers.http; the PEAR HTTP_Request class at http://pear.php.net/package/HTTP_Request; RFC 2616 is available at http://www.w3.org/Protocols/rfc2616/rfc2616.html.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net