13.2.1. ProblemYou 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. SolutionSet the method and content stream context options when using the http stream, as in Example 13-14. Using POST with the http stream
With cURL, set the CURLOPT_POST and CURLOPT_POSTFIELDS options, as in Example 13-15. Using POST with cURL
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
13.2.3. DiscussionSending 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 Also13.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. |