/* $Id$ */
-#define _WINSOCKAPI_
-#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
-
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
getObject(httpi_response_object, obj);
NO_ARGS;
-
+
do_cache = GET_PROP(obj, cache);
do_gzip = GET_PROP(obj, gzip);
}
/* }}} */
+/* {{{ proto void HTTPi_Request::unsetOptions()
+ *
+ * Unset all options/headers/cookies.
+ */
+PHP_METHOD(HTTPi_Request, unsetOptions)
+{
+ getObject(httpi_request_object, obj);
+
+ NO_ARGS;
+
+ FREE_PARR(obj, options);
+ INIT_PARR(obj, options);
+}
+/* }}} */
+
+/* {{{ proto bool HTTPi_Request::addHeader(array header)
+ *
+ * Add (a) request header name/value pair(s).
+ */
+PHP_METHOD(HTTPi_Request, addHeader)
+{
+ zval *opts, **headers, *new_headers;
+ getObject(httpi_request_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
+ RETURN_FALSE;
+ }
+
+ opts = GET_PROP(obj, options);
+
+ if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
+ array_merge(new_headers, *headers);
+ } else {
+ zval_add_ref(&new_headers);
+ add_assoc_zval(opts, "headers", new_headers);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool HTTPi_Request::addCookie(array cookie)
+ *
+ * Add (a) cookie(s).
+ */
+PHP_METHOD(HTTPi_Request, addCookie)
+{
+ zval *opts, **cookies, *new_cookies;
+ getObject(httpi_request_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
+ RETURN_FALSE;
+ }
+
+ opts = GET_PROP(obj, options);
+
+ if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
+ array_merge(new_cookies, *cookies);
+ } else {
+ zval_add_ref(&new_cookies);
+ add_assoc_zval(opts, "cookies", new_cookies);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
/* {{{ proto bool HTTPi_Request::setURL(string url)
*
* Set the request URL.
/* {{{ proto bool HTTPi_Request::setMethod(long request_method)
*
- * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
+ * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
* <tt>HTTP_POST</tt> constants.
*/
PHP_METHOD(HTTPi_Request, setMethod)
/* {{{ proto array HTTPi_Request::getResponseData()
*
- * Get all resposonse data after sending the request.
+ * Get all response data after the request has been sent.
*/
PHP_METHOD(HTTPi_Request, getResponseData)
{
}
/* }}} */
-/* {{{ proto array HTTPi_Request::getResponseHeaders()
+/* {{{ proto string HTTPi_Request::getResponseHeader([string name])
*
- * Get the response headers after sending the request.
+ * Get response header(s) after the request has been sent.
*/
-PHP_METHOD(HTTPi_Request, getResponseHeaders)
+PHP_METHOD(HTTPi_Request, getResponseHeader)
{
- zval *data, **headers;
- getObject(httpi_request_object, obj);
+ zval *data, **headers, **header;
+ char *header_name = NULL;
+ int header_len = 0;
+ getObject(httpi_response_object, obj);
- NO_ARGS;
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
+ RETURN_FALSE;
+ }
- array_init(return_value);
data = GET_PROP(obj, responseData);
- if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
+ if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
+ RETURN_FALSE;
+ }
+
+ if (!header_len || !header_name) {
+ array_init(return_value);
array_copy(*headers, return_value);
+ } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
+ RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
+ } else {
+ RETURN_FALSE;
}
}
-/* }}} */
/* {{{ proto string HTTPi_Request::getResponseBody()
*
- * Get the response body after sending the request.
+ * Get the response body after the request has been sent.
*/
PHP_METHOD(HTTPi_Request, getResponseBody)
{
if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
} else {
- Z_TYPE_P(return_value) = IS_NULL;
+ RETURN_FALSE;
}
}
/* }}} */
-/* {{{ proto array HTTPi_Request::getResponseInfo()
+/* {{{ proto int HTTPi_Request::getResponseCode()
*
- * Get response info after sending the request.
+ * Get the response code after the request has been sent.
+ */
+PHP_METHOD(HTTPi_Request, getResponseCode)
+{
+ zval **code, **hdrs, *data;
+ getObject(httpi_request_object, obj);
+
+ NO_ARGS;
+
+ data = GET_PROP(obj, responseData);
+ if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &hdrs)) &&
+ (SUCCESS == zend_hash_find(Z_ARRVAL_PP(hdrs), "Status", sizeof("Status"), (void **) &code))) {
+ RETVAL_STRINGL(Z_STRVAL_PP(code), Z_STRLEN_PP(code), 1);
+ convert_to_long(return_value);
+ } else {
+ RETURN_FALSE;
+ }
+}
+/* }}} */
+
+/* {{{ proto array HTTPi_Request::getResponseInfo([string name])
+ *
+ * Get response info after the request has been sent.
* See http_get() for a full list of returned info.
*/
PHP_METHOD(HTTPi_Request, getResponseInfo)
{
- zval *info;
+ zval *info, **infop;
+ char *info_name = NULL;
+ int info_len = 0;
getObject(httpi_request_object, obj);
- NO_ARGS;
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
+ RETURN_FALSE;
+ }
info = GET_PROP(obj, responseInfo);
- array_init(return_value);
- array_copy(info, return_value);
+
+ if (info_len && info_name) {
+ if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
+ RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not find response info named %s", info_name);
+ RETURN_FALSE;
+ }
+ } else {
+ array_init(return_value);
+ array_copy(info, return_value);
+ }
}
/* }}}*/
/* {{{ proto bool HTTPi_Request::send()
*
* Send the HTTP request.
+ *
+ * GET example:
+ * <pre>
+ * <?php
+ * $r = new HTTPi_Request('http://example.com/feed.rss', HTTP_GET);
+ * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
+ * $r->addQueryData(array('category' => 3));
+ * if ($r->send() && $r->getResponseCode() == 200) {
+ * file_put_contents('local.rss', $r->getResponseBody());
+ * }
+ * ?>
+ * </pre>
+ *
+ * POST example:
+ * <pre>
+ * <?php
+ * $r = new HTTPi_Request('http://example.com/form.php', HTTP_POST);
+ * $r->setOptions(array('cookies' => array('lang' => 'de')));
+ * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
+ * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
+ * if ($r->send()) {
+ * echo $r->getResponseBody();
+ * }
+ * ?>
+ * </pre>
*/
PHP_METHOD(HTTPi_Request, send)
{
info = GET_PROP(obj, responseInfo);
resp = GET_PROP(obj, responseData);
- // HTTP_URI_MAXLEN+1 big char *
- request_uri = http_absolute_uri(Z_STRVAL_P(URL), NULL);
+ // HTTP_URI_MAXLEN+1 long char *
+ request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
if (!strchr(request_uri, '?')) {