<?php /*
#################################################################
Copyright 2007, Michael Schrenk This software is designed for use with the book, "Webbots, Spiders, and Screen Scarpers", Michael Schrenk, 2007 No Starch Press, San Francisco CA W3C� SOFTWARE NOTICE AND LICENSE This work (and included software, documentation such as READMEs, or other related items) is being provided by the copyright holders under the following license. By obtaining, using and/or copying this work, you (the licensee) agree that you have read, understood, and will comply with the following terms and conditions. Permission to copy, modify, and distribute this software and its documentation, with or without modification, for any purpose and without fee or royalty is hereby granted, provided that you include the following on ALL copies of the software and documentation or portions thereof, including modifications: 1. The full text of this NOTICE in a location viewable to users of the redistributed or derivative work. 2. Any pre-existing intellectual property disclaimers, notices, or terms and conditions. If none exist, the W3C Software Short Notice should be included (hypertext is preferred, text is permitted) within the body of any redistributed or derivative code. 3. Notice of any changes or modifications to the files, including the date changes were made. (We recommend you provide URIs to the location from which the code is derived.) THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION. The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the software without specific, written prior permission. Title to copyright in this software and any associated documentation will at all times remain with copyright holders.
#################################################################
*/ #----------------------------------------------------------------------- # LIB_http # # F U N C T I O N S # # http_get() # Fetches a file from the Internet with the HTTP protocol # http_header() # Same as http_get(), but only returns HTTP header instead of # the normal file contents # http_get_form() # Submits a form with the GET method # http_get_form_withheader() # Same as http_get_form(), but only returns HTTP header instead of # the normal file contents # http_post_form() # Submits a form with the POST method # http_post_withheader() # Same as http_post_form(), but only returns HTTP header instead of # the normal file contents # http_header() # # http() # A common routine called by all of the previously described # functions. You should always use one of the other wrappers for this # routine and not call it directly. # #----------------------------------------------------------------------- # R E T U R N E D D A T A # All of these routines return a three dimensional array defined as # follows: # return_array['STATUS'] = CURL generated status of transfer #
#################################################################
********************************************************************* Webbot defaults (scope = global) ----------------------------------------------------------------------
# Define how your webbot will appear in server logs define("WEBBOT_NAME", "Test Webbot"); # Length of time cURL will wait for a response (seconds) define("CURL_TIMEOUT", 25); # Location of your cookie file. (Must be fully resolved local address) define("COOKIE_FILE", "c:\cookie.txt"); # DEFINE METHOD CONSTANTS define("HEAD", "HEAD"); define("GET", "GET"); define("POST", "POST"); # DEFINE HEADER INCLUSION define("EXCL_HEAD", FALSE); define("INCL_HEAD", TRUE);
********************************************************************* User interfaces -------------------------------------------------------------
********************************************************************* function http_get(target, target The target file (to download) return_array['FILE'] = Contents of fetched file, will also include the HTTP header if requested return_array['ERROR'] = CURL generated error status *********************************************************************
function http_get(ref) { return http(ref, data_array="", EXCL_HEAD); }
********************************************************************* http_get_withheader(ref) ------------------------------------------------------------- DESCRIPTION: Downloads an ASCII file with the http header INPUT: ref The server referer variable OUTPUT: return_array['STATUS'] = CURL generated status of transfer
function http_get_withheader(target, target, method="GET",
********************************************************************* http_get_form(target, data_array) ------------------------------------------------------------- DESCRIPTION: Submits a form with the GET method and downloads the page (without a http header) referenced by the form's ACTION variable INPUT: ref The server referer variable data_array) OUTPUT: return_array['STATUS'] = CURL generated status of transfer
function http_get_form(target, data_array) { return http(ref, data_array, EXCL_HEAD); }
********************************************************************* http_get_form_withheader(ref, target The target file (to download) data_array An array that defines the form variables (See "Webbots, Spiders, and Screen Scrapers" for more information about return_array['FILE'] = Contents of fetched file, will also include the HTTP header if requested return_array['ERROR'] = CURL generated error status *********************************************************************
function http_get_form_withheader(ref, target, method="GET",
********************************************************************* http_post_form(target, data_array) ------------------------------------------------------------- DESCRIPTION: Submits a form with the POST method and downloads the page (without http header) referenced by the form's ACTION variable INPUT: ref The server referer variable data_array) OUTPUT: return_array['STATUS'] = CURL generated status of transfer
function http_post_form(target, data_array) { return http(ref, data_array, EXCL_HEAD); } function http_post_withheader(ref, target, method="POST", target, target, method="HEAD",
********************************************************************* http(url, method, incl_head) ------------------------------------------------------------- DESCRIPTION: This function returns a web page (HTML only) for a web page through the execution of a simple HTTP GET request. All HTTP redirects are automatically followed. IT IS BEST TO USE ONE THE EARLIER DEFINED USER INTERFACES FOR THIS FUNCTION INPUTS: ref Address of the target web site's referrer data_array A keyed array, containing query string return_array['FILE'] = Contents of fetched file, will also include the HTTP header if requested return_array['ERROR'] = CURL generated error status *********************************************************************
function http(ref, data_array, ch = curl_init(); # Prcess data, if presented if(is_array(data_array as value) { if(strlen(trim(temp_string[] = value); else key; } temp_string); } # HEAD method configuration if(ch, CURLOPT_HEADER, TRUE); // No http head curl_setopt(method == GET) { if(isset(target = query_string; curl_setopt (ch, CURLOPT_POST, FALSE); } # POST method configuration if(query_string)) curl_setopt (query_string); curl_setopt (ch, CURLOPT_HTTPGET, FALSE); } curl_setopt(incl_head); // Include head as needed curl_setopt(ch, CURLOPT_COOKIEJAR, COOKIE_FILE); // Cookie management. curl_setopt(ch, CURLOPT_TIMEOUT, CURL_TIMEOUT); // Timeout curl_setopt(ch, CURLOPT_URL, ch, CURLOPT_REFERER, ch, CURLOPT_VERBOSE, FALSE); // Minimize logs curl_setopt(ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects curl_setopt(ch, CURLOPT_RETURNTRANSFER, TRUE); // Return in string # Create return array ch); ch); ch); # Close PHP/CURL handle curl_close(return_array; } ?>
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.