Juggling Data

HTTP_Request makes data management pretty easy and straightforward, so it’s all covered in this section.

<?php
$request =& new HTTP_Request('http://localhost.example');

// GET
$request->addQueryString('variable', 'value');
$request->addRawQuerySt ring('foo=bar');

// POST
$request->addPostData('variable', 'value');
$request->clearPostData();

// COOKIE
$request->addCookie('variable', 'value');
$request->clearCookies();

// FILES
$request->addFile('fieldname', '/path/to/file', 'text/plain');
?>
  • addRawQueryString isn’t named very intuitively, as it actually overwrites any query string that you’ve previously set.
  • By default, addQueryString and addPostData will URL-encode the variable value. To prevent this if your data is already pre-encoded, pass the value t rue as the third parameter to either method.
  • clearPostData and clearCookies reset internal variables used to store data passed in by addQueryString and addPostData respectively.
  • setURL and addQueryString or addRawQueryString can be used together to control the content of the internal variable for the URL of the request. getURL can be used to see the effects of these during development.
  • addFile is intended to simulate uploading a file via a file input field in an HTML form. For forms with multiple file upload files with the same name, pass an array of file paths as the second parameter and (optionally) an array of corresponding file MIME types as the third parameter.
  • To send the contents of a file as the entire body of a request, use file_get_contents to retrieve the file contents into a string and pass that to the setBody method of the request instance.

© PEAR::HTTP_Client — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (01.09.2014)
Views: 335 | Rating: 0.0/0
Total comments: 0
avatar