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!
|
|
|
|
The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times. FREE! Go There Now!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |