# Using Network functions.

- Thread: https://externals.io/message/38618
- Original archive: https://news-web.php.net/php.internals/38618
- Started by Mangol Smith on 2008-06-25T20:11:16+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #38618 — Mangol Smith — 2008-06-25T20:11:16+00:00

URL: https://externals.io/message/38618#38618

Hey guys,

I just started hacking PHP. I want to use Netwok Functions.
So far from the source code browsing I learnt that In PHP *"obvious"
solution  is not the right answer.*

The obvious solution for implementing HTTP methods or any Network functions
for that matter, is using socket(), connect(), send(), recv() with
appropriate HTTP request which only works for *NIX systems. So, there must
be some kind of wrappers over it make these Platform Independent.

Though, I looked into the implementations of
PHP_FUNCTION('php_get_contents'). I didn't get much of it. I din't see any
socket functions in its implementation. I saw some kind of stream_*
functions. (which was complete abstract to me).

1. Can any one help me in using Network functions?
2. What does streams has to do with this networking? Can it be done using
streams?
3. In List of Supported
Protocols/Wrappers<http://in2.php.net/manual/en/wrappers.php>its
mentioned that
Allows read-only access to files/resources via HTTP 1.0, using the HTTP GET
method. This means isn't there any wrapper to use all HTTP methods. I'm
concerned about HTTP PUT/POST (esp. PUT)  method. How can I use that method?
4. what are the other alternatives (if any)?

---

## #38620 — Mangol Smith — 2008-06-25T23:22:22+00:00

URL: https://externals.io/message/38618#38620 · In reply to: #38618

Hey guys,

someone suggested me to have a look at libcURL. I did, but its very complex.

All I needed is lowlevel/network wrappers.

like emalloc() & pemalloc() as wrapper on malloc(). There might be some
wrappers (platform independent & used in php source code) which I can use to
make a connection and then send HTTP requests.

I hope this will be the simplest method.

---

## #38623 — Larry Garfield — 2008-06-26T02:57:16+00:00

URL: https://externals.io/message/38618#38623 · In reply to: #38620

On Wednesday 25 June 2008 6:22:22 pm Mangol Smith wrote:
> Hey guys,
>
> someone suggested me to have a look at libcURL. I did, but its very
> complex.
>
> All I needed is lowlevel/network wrappers.
>
> like emalloc() & pemalloc() as wrapper on malloc(). There might be some
> wrappers (platform independent & used in php source code) which I can use
> to make a connection and then send HTTP requests.
>
> I hope this will be the simplest method.

Why do you need raw string level control over the HTTP request?  Wouldn't it 
be easier to use something that will handle all of the thousand edge cases 
and locking for you?  (That is, I think, part of the meaning behind the 
obvious solution, write to a socket yourself, not being the right solution, 
letting someone else do the hard stuff for you.)

-- 
Larry Garfield
larry@garfieldtech.com

---

## #38625 — Mangol Smith — 2008-06-26T05:30:21+00:00

URL: https://externals.io/message/38618#38625 · In reply to: #38623

Hi,




> If you want to POST or do weird HTTP stuff, try the Curl extension.
> (Multiple platforms)

Also look into PEAR, they have some socket stuff as well I think, as
> well as lots of other extensions that implement other protocols.


But, doesn't this make my extension depend on Curl or any PEAR extension you
are talking about? That is what I wan't to avoid.

On Thu, Jun 26, 2008 at 8:27 AM, Larry Garfield <larry@garfieldtech.com>
wrote:

>
> Why do you need raw string level control over the HTTP request?  Wouldn't
> it
> be easier to use something that will handle all of the thousand edge cases
> and locking for you?

Yeah, might be the only thing I want to avoid is my extension depending on
other non standard (which are not shipped with PHP  by default) extensions.
Thats why I'm looking for lower level wrappers. Thats why I feel using CORE
API.

---

## #38630 — pstradomski@gmail.com — 2008-06-26T08:40:28+00:00

URL: https://externals.io/message/38618#38630 · In reply to: #38625

W liście Mangol Smith z dnia czwartek 26 czerwca 2008:

> But, doesn't this make my extension depend on Curl or any PEAR extension
> you are talking about? That is what I wan't to avoid.
>

Are you sure you even need to write your own extension? If you just want to do 
HTTP requests, then curl or HTTP extension should be sufficient.


-- 
Paweł Stradomski

---

## #38631 — Mangol Smith — 2008-06-26T08:45:40+00:00

URL: https://externals.io/message/38618#38631 · In reply to: #38630

> Are you sure you even need to write your own extension? If you just want to
> do
> HTTP requests, then curl or HTTP extension should be sufficient.


Nope, I'm developing an extension. In which I require this functionality.
Its not the entire purpose of my extension. Just a part of the
functionality.

---

## #38633 — crrodriguez@suse.de — 2008-06-26T09:08:50+00:00

URL: https://externals.io/message/38618#38633 · In reply to: #38620

Mangol Smith escribió:
> Hey guys,
> 
> someone suggested me to have a look at libcURL. I did, but its very complex.
> 
> All I needed is lowlevel/network wrappers.
> 
> like emalloc() & pemalloc() as wrapper on malloc(). There might be some
> wrappers (platform independent & used in php source code) which I can use to
> make a connection and then send HTTP requests.

use the api provided by either pecl_http or curl extension, you dont 
reaaly want to do it in a different way.

also. evaluate if you realy want to write a C extension for that, proper 
userspace code should do the trick just fine.


-- 
"Perhaps, my biggest pet peeve pertaining to safe_mode is that it is not 
really safe,
  merely offering an illusion of safety that does not stand to closer 
examination" - Ilia on PHP safe_mode

Cristian Rodríguez R.
Platform/OpenSUSE - Core Services
SUSE LINUX Products GmbH
Research & Development
http://www.opensuse.org/

---

## #38643 — Christian Seiler — 2008-06-26T18:37:39+00:00

URL: https://externals.io/message/38618#38643 · In reply to: #38618

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
