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
developerWorks - FREE Tools! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David 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!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
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!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |