Miscellaneous Code
  Home arrow Miscellaneous Code arrow getpostlib
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS CODE

getpostlib
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2005-05-25

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    php unit for performing HTTP GET and POST requests.

    By : igosha

    <?php


    /*! This function performs a http POST query (i.e. form submission) and
    * returns the received response.
    \return FALSE Something went wrong, possibly the server refused your query or
    the connection to the server failed.
    \return The response string.
    */
    function http_post($server /**< Server's or proxie's host name or IP.*/,
    $port, /**< Server's or proxie's port */
    $url, /**< Should point to the URL on the server or full address if a proxy is used. */
    $cookie, /**< The optional cookie string. */
    $vars /**< Associative array of POST variables. Each key is the name of the variable and the value
    is the variable's content */
    )
    {
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";


    $urlencoded = "";
    while (list($key,$value) = each($vars))
    $urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
    $urlencoded = substr($urlencoded,0,-1);

    $content_length = strlen($urlencoded);

    $headers = "POST $url HTTP/1.1\r\nHost: $server\r\nAccept: */*\r\nAcceptLanguage: en-au\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: $user_agent\r\nCache-Control: no-cache\r\nCookie: {$cookie}\r\nContent-Length: $content_length\r\n\r\n";

    $fp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

    if (!socket_connect($fp, $server, $port))
    {
    socket_close($fp);
    return false;
    }

    socket_set_option($fp, SOL_SOCKET, SO_KEEPALIVE, TRUE);
    socket_send($fp, $headers, strlen($headers), 0);
    socket_send($fp, $urlencoded, strlen($urlencoded), 0);

    $ret = "";
    while ($rd = socket_read($fp, 1024))
    {
    $ret .= $rd;
    }

    socket_close($fp);

    $pos = strpos($ret, "\r\n\r\n");

    if (!is_bool($pos))
    {
    $ret = substr($ret, $pos);
    }

    return $ret;

    }

    /** This function performs a http GET query (i.e. download a web page or file) and
    * returns the received response.
    \return FALSE Something went wrong, possibly the server refused your query or
    the connection to the server failed.
    \return The response string.
    */
    function http_get($server, /**< Server's or proxie's host name or IP.*/
    $port, /**< Server's or proxie's port */
    $url, /**< Should point to the URL on the server or full address if a proxy is used. */
    $cookie, /**< The optional cookie string. */
    $additional_fields = "" /**< Optional string containing additional HTTP request fields,
    each terminated by "\r\n". */)
    {
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";

    $headers = "GET $url HTTP/1.1\r\nAccept: */*\r\nAccept-Language: en-au\r\nUser-Agent: $user_agent\r\nHost: $server\r\nCookie: {$cookie}\r\nCache-Control: no-cache\r\nConnection: close\r\n{$additional_fields}\r\n";

    $fp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if (!socket_connect($fp, $server, $port))
    {
    socket_close($fp);
    return FALSE;
    }

    socket_set_option($fp, SOL_SOCKET, SO_KEEPALIVE, TRUE);
    socket_send($fp, $headers, strlen($headers), 0);

    $ret = "";
    while ($rd = socket_read($fp, 1024))
    {
    $ret .= $rd;
    }

    socket_close($fp);

    $pos = strpos($ret, "\r\n\r\n");

    if (!is_bool($pos))
    {
    $ret = substr($ret, $pos);
    }

    return $ret;
    }

    ?>

    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! IBM – Taking Web 2.0 to Work

    David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
    FREE! Go There Now!


    NEW! Best practices for software analysis: An introduction to the IBM Rational Software Analyzer application

    This whitepaper presents the benefits of successfully introducing static analysis into your organization using IBM Rational Software Analyzer. Additionally, it identifies some common pitfalls that can hinder the effective use of static analysis tooling as well as presents 10 simple strategies designed to help you quickly realize the value of static analysis using Rational Software Analyzer.
    FREE! Go There Now!


    NEW! Build Web services with transport-level security using Rational Application Developer V7, Part 1: Build Web services and Web services clients

    Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services.
    FREE! Go There Now!


    NEW! Hello World: Monitor a simple business process using WebSphere Business Monitor V6.0.2

    This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product.
    FREE! Go There Now!


    NEW! IBM Enterprise Modernization Sandbox for System z

    IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for People

    Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek