Re: Using Network functions.
| From: | Christian Seiler | Date: | Thu, 26 Jun 2008 18:37:39 +0000 |
| Subject: | Re: Using Network functions. | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-38643@lists.php.net to get a copy of this message | ||
Hi!
2. What does streams has to do with this networking? Can it be done using streams?Yes, it can. Take the following simple, incomplete example without much error handling as a starting point: // in the header #include "php_streams.h" // PHP function: PHP_FUNCTION(simplehttp_fetch) { php_stream *stream; struct timeval tv; int err; char *errstr; char buf[8192]; tv.tv_sec = 30; tv.tv_usec = 0; stream = php_stream_xport_create("tcp://www.google.com:80",
sizeof("tcp://www.google.com:80")-1,
ENFORCE_SAFE_MODE | REPORT_ERRORS,
TREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
NULL, &tv, NULL, &errstr, &err);
if (stream == NULL) {
php_error(E_WARNING TSRMLS_CC,
"unable to connect to tcp://www.google.com:80: %s",
errstr == NULL ? "unknown error" : errstr);
RETURN_NULL();
}
php_stream_write(stream,
"GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n",
sizeof("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n")-1);
memset(buf, 0, 8192);
php_stream_read(stream, buf, 8191);
php_stream_close(stream);
RETURN_STRING(buf, 1);
}
Basically, you want to look at main/php_streams.h and
main/streams/php_stream_transport.h in the PHP source code. Userland
fsockopen() essentially maps to php_stream_xport_create() and userland
fread, fwrite, close, etc. essentially map to php_stream_read(),
php_stream_write, php_stream_close, etc.
Regards,
Christian