11.3. Fetching a URL with the POST Method11.3.1. ProblemYou want to retrieve a URL with the POST method, not the default GET method. For example, you want to submit an HTML form. 11.3.2. SolutionUse the cURL extension with the CURLOPT_POST option set:
If the cURL extension isn't available, use the PEAR HTTP_Request class:
11.3.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. Because of the argument handling and additional headers, you can't use fopen( ) to make a POST request. If neither cURL nor HTTP_Request are available, use the pc_post_request( ) function, shown in Example 11-1, which makes the connection to the remote web server with fsockopen( ). Example 11-1. pc_post_request( )
Call pc_post_request( ) like this:
Retrieving a URL with POST instead of GET is especially useful if the URL 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. 11.3.4. See AlsoRecipe 11.2 for fetching a URL with the GET method; documentation on curl_setopt( ) at http://www.php.net/curl-setopt and fsockopen( ) at http://www.php.net/fsockopen; the PEAR HTTP_Request class at http://pear.php.net/package-info.php?package=HTTP_Request; RFC 2616 is available at http://www.faqs.org/rfcs/rfc2616.html.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|