> Socket
fr en

It's usually useful to be allowed to open a connection to a host with on given port. Moreover, you'll have sometimes to get the content of a web page but your installation of PHP won't allow to use "fopen" on a url...

You will then want to use the Socket class.

How to use it

The Socket class is part of "generictools". It is straightforward to instanciate:


<?php
//...
 $sock = _ioClass('generictools|socket');
//...
?>
 

It counts 5 methods:

  1. open
  2. close
  3. write
  4. read
  5. getHttpContent

Open, Read, Write...

Below is a tiny sample that will allow you to open and use a Socket:


<?php
//...
    $s = _ioClass('generictools|socket')->open("www.google.com",80);
    $s->write("GET HTTP/1.0\n\n");
    $response = $s->read();
    $s->close();
//...
?>
 

Getting the HTTP content

Here is how to get rid of fopen and to read HTTP content. The method is getHttpContent and takes the url to read as parameter:


<?php
//...
    $url = "http://www.copix/org/wiki/Socket";
    $content = _ioClass('generictools|socket')->getHttpContent($url);
//...
?>
 

$content will contain the HTML content of the page "http://www.copix/org/wiki/Socket"