Webmaster (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Using sockets in PHP : Get articles from Usenet
PHP can open sockets on remote or local hosts. Here is a hands-on example of using such a socket: getting connected to a Usenet News Server, talking to this server, and downloading some articles for a precise newsgroup.
Opening a socket in PHP
Sockets are opened using fsockopen(). This function is both available in PHP3 and PHP4. It uses the following prototype :

<?php

int fsockopen
    
(string hostname,
        
int port [,
        
int errno [,
        
string errstr [,
        
double timeout]]])
?>
For the Internet domain, it will open a TCP socket connection to hostname on port port. hostname may in this case be either a fully qualified domain name or an IP address. For UDP connections, you need to explicitly specify the protocol: udp://hostname. For the Unix domain, hostname will be used as the path to the socket, port must be set to 0 in this case. The optional timeout can be used to set a timeout in seconds for the connect system call.
More information about fsockopen() : http://www.php.net/manual/function.fsockopen.php
Network News Transfer Protocol
Accessing a Usenet News Server requires using a specific protocol, called NNTP and standing for Network News Transfer Protocol.
This protocol is higly detailed in RFC977 (Request For Comment number 977), which is available at : http://www.w3.org/Protocols/rfc977/rfc977.html
This document described precisely how to connect to and then dialog with the NNTP server thanks to the various commands available for the task.
Connecting
Connecting to the NNTP server requires knowing its hostname (or IP address) and the port it is listening on. You should include a timeout so that an unsuccessful attempt at connecting does not "freeze" the application.

<?php

$cfgServer    
= "your.news.host";
$cfgPort    = 119;
$cfgTimeOut    = 10;

// open a socket
if(!$cfgTimeOut)
    
// without timeout
    
$usenet_handle = fsockopen($cfgServer, $cfgPort);
else
    
// with timeout
    
$usenet_handle = fsockopen($cfgServer, $cfgPort, &$errno, &$errstr, $cfgTimeOut);

if(!
$usenet_handle) {
    echo
"Connexion failed\n";
    exit();
}    
else {
    echo
"Connected\n";
    
$tmp = fgets($usenet_handle, 1024);
}

?>
[ Next Page ]


Comments:
RE: Sockets and Half-LifeJustin Frydman02/14/06 01:56
RE: use php exec() to Control Parallel Port coolvista10/31/05 13:24
RE: Retrieving article from usenetsangeeta chakraborty10/15/05 00:24
saving scriptsRogy04/05/05 08:15
how to access external email behind my proxyrelly02/15/05 20:53
RE: Sockets and Half-LifeTurk01/25/05 13:38
And how about those fairies? Sex12/05/04 04:48
For some reason, Sex12/05/04 04:47
PLease, how Can i download. Sex Beplaced Ru 12/05/04 04:43
ummmmmmmmm whats this mummy??Sex Beplaced Ru 12/05/04 04:34
This was closely associated with the rebirth of Freeflashonlinegames Com 12/05/04 04:33
Thanks. I hope other people test it too. Freeflashonlinegames Com 12/05/04 04:33
This was closely associated with the rebirth of Sex Beplaced Ru 12/05/04 04:32
die Reet is anders een gemene man...Freeflashonlinegames Com 12/05/04 04:31
You are right ofcourse... But...Sex Beplaced Ru 12/05/04 04:31
For some reason, Sex Beplaced Ru 12/05/04 04:29
Were there ever real witches? Sex12/05/04 04:28
This was closely associated with the rebirth of Sex12/05/04 04:26
What?, don't they know who we are? Freeflashonlinegames Com 12/05/04 04:25
Finally figured this out - my Sex12/05/04 04:25
If not, what were all the witch trials about? Sex12/05/04 04:23
PLease, how Can i download. Freeflashonlinegames Com 12/05/04 04:08
RE: HTTP Proxy Servers and PHP SocketsRev Aaron04/23/04 10:47
RE: Quake III ProtocolKyle04/02/04 13:42
RE: Sockets not working...Please helpusman shahid10/19/02 04:58
Retrieving ip from urlSmoske09/20/02 15:19
Sockets not working...Please helpRenny09/19/02 14:03
Sned HTML message to all connected clientJim09/13/02 08:17
How to use PHP Control Paralle PortPyrawate09/09/02 00:32
How to use PHP Control Paralle PortPyrawate09/08/02 23:59
RE: Retrive client's ip addressQuan Mac09/04/02 00:54
RE: How to open sockets from behind proxy serverIonut Daia08/22/02 20:39
ADVANCED PHP SOCKETSGustavo Almeida08/14/02 14:34
ADVANCED PHP (SOCKET)Gustavo Almeida08/14/02 14:27
timeout does not workOsman Darcan08/06/02 19:32
RE: How to open sockets from behind proxy serverzingit07/18/02 04:53
StuckBamma06/29/02 17:57
RE: Sockets and Half-Life full script hereNvE0s06/26/02 19:41
passing parameterVit06/25/02 23:24
Quake III ProtocolJohan06/24/02 15:24
GET??Giuseppe06/20/02 05:22
Retrive client's ip addressJohn06/16/02 03:17
Script for negotiating proxy with user auth.Xixulon06/14/02 10:52
RE: How to open sockets from behind proxy serveXixulon06/14/02 10:49
Wap -> Website -> Computer shutdown.Marek Miettinen06/14/02 06:56
RE: IRC Socket?Bryan06/12/02 01:36
RE: Browser Chat clientMax Mo06/11/02 08:10
RE: PHP sockets/networksKamran05/07/02 03:49
RE: IRC Socket?rubbi05/03/02 13:05
RE: PHP sockets/networksMorrissey04/27/02 03:25
html codesmarie shelton04/22/02 07:23
ProxyTom04/15/02 08:59
RE: Re Sockets and Half-Lifebstanescu04/08/02 05:52
PERL Sockets AND PHP Socketspopnikos04/03/02 09:24
RE: Browser Chat clientbluBattery03/13/02 14:54
cookiesmohd03/09/02 16:37
Accessing POP3 mailbox using PHP SocketsArun G P Dutt03/07/02 00:41
RE: socket codeMichelle02/26/02 17:58
RE: Sockets and Half-LifeDave02/18/02 18:46
RE: Browser Chat clientRob_Earl02/05/02 22:14
RE: How to redirect in LANRob02/05/02 21:54
RE: importing a html page in a php scriptRob02/05/02 21:32
RE: How to redirect in LANRob02/05/02 21:30
RE: Sockets and Half-LifeRob02/05/02 15:46
RE: PHP telnet client?Per-Ingar Auberg02/02/02 06:03
Telnet scripting from PHPPer-Ingar Auberg02/02/02 06:00
RE: How to open sockets from behind proxy serverValberg Larusson01/19/02 01:02
socket codeColleen01/16/02 16:47
RE: How to post a form to another...Palan01/12/02 17:51
socket response-urg!!!sands01/09/02 10:50
How can I change xml file to php arrayS.Murali Krishna01/08/02 05:21
How to redirect in LANmk401/05/02 05:28
RE: Sockets and Half-LifeRuud Kamphuis01/01/02 09:05
RE: Sockets and Half-LifeDinesh12/30/01 14:46
RE: PHP telnet sockets(was client)Gulopine12/20/01 11:14
importing a html page in a php scriptjoe12/14/01 20:11
Retrieving article from usenetBill12/12/01 18:05
RE: How to open sockets from behind proxy serverSteven Chalker12/11/01 17:04
How to open sockets from behind proxy serverSatish Krishna12/04/01 06:43
Display pictures which are attached?valerie11/29/01 14:16
Can pictures be displaid which are attached? valerie11/28/01 18:41
opening sockets in phpYogesh Randhawa11/28/01 07:33
PHP sockets/networksArmando P.11/21/01 05:21
How can I catch a socket in action?Albert MAYE11/05/01 09:58
How to post a form to another...Marcel11/05/01 08:25
And now, what's is the final script?mc-no-solard11/04/01 12:41
Can I display pictures which are attached?Jens11/03/01 10:41
RE: saving php output in html fileTomas10/29/01 12:51
RE: how to enable socket() in PHParchi6910/21/01 23:59
RE: Sockets and Half-Lifehuib bakker10/07/01 07:46
saving php output in html fileandrea09/28/01 05:49
all about socketslavanya09/28/01 05:28
Browser Chat clientLeeWynn09/15/01 19:42
QuestionSteven C09/07/01 10:31
FTP connectionJ.Bijleveld09/04/01 08:00
RE: I would like to known more about php Ger van Vugt08/18/01 08:58
how to enable socket() in PHPDiou07/30/01 03:02
RE: Sockets and Half-Lifeady07/28/01 09:33
How to make only on queryClaude07/27/01 02:56
RE: HTTPS SocketsdRago07/25/01 04:39
RE: I would like to known more about php Phillip Oertel07/24/01 04:52
SOCKET PHOJose Luis Castro07/18/01 13:09
help me!!!ice_berg1607/18/01 04:58
RE: Sockets and Half-Lifepaul07/09/01 22:55
RE: PHP telnet sockets(was client)OmagaSohe07/09/01 05:46
RE: IRC Socket?Nilo07/02/01 11:41
RE: IRC Socket?zeke07/02/01 02:48
Page LinksRavinder singh06/29/01 02:44
RE: IRC Socket?Bertrand DUNOGIER06/26/01 04:12
Sounds with PHPCHATAlan06/25/01 12:53
I would like to known more about php Hugo Ivan06/18/01 10:31
is the socket() function available in windowsPaul Villavicencio06/13/01 07:57
RE: HTTPS SocketsUwi06/13/01 07:34
Which nntp server to use?Seb Delcampe06/07/01 10:59
PHP Sockets...Kyle Olsen06/06/01 14:41
RE: PHP telnet client?Kyle Olsen06/04/01 11:30
RE: HTTPS SocketsJordan05/25/01 09:58
replacing charactersMike05/22/01 01:57
Sockets and Half-LifeRafael Bastidas05/21/01 11:28
Example at phpindex.com/ngAlex05/21/01 08:17
Getting Headers OnlySean05/20/01 01:11
RE: HTTPS SocketsErwin05/16/01 10:00
HTTPS SocketsJon Thomas05/13/01 18:32
PHP telnet client?jchc05/12/01 23:50
RE: IRC Socket?steve05/11/01 16:03
RE: decoding attachmentsOnno Benschop05/08/01 19:23
Achive this from behind a proxy !Steve haemelinck05/06/01 06:19
RE: PHP Sockets on Windowsbmpc05/06/01 04:49
RE: Oy, why did the RFC specify numeric codes?Armel FAUVEAU05/03/01 09:45
RE: IRC Socket?Armel FAUVEAU05/03/01 09:42
RE: IRC Socket?Mathieu 'CaPS' Kooiman05/02/01 23:20
RE: Oy, why did the RFC specify numeric codes?Mathieu 'CaPS' Kooiman05/02/01 23:18
IRC Socket?Ninconvert05/02/01 17:33
RE: Oy, why did the RFC specify numeric codes?Armel FAUVEAU05/02/01 17:20
Radius check using PHP+socketsO. van Zandwijk05/02/01 15:07
Oy, why did the RFC specify numeric codes?Mathieu 'CaPS' Kooiman05/01/01 05:59
RE: disconneting ....Dave05/01/01 05:00
RE: IMAP functions?Dave05/01/01 04:59
decoding attachmentsChristian Lund04/30/01 14:02
RE: IMAP functions?Justin Plock04/30/01 11:08
PHP Sockets on WindowsC Clarke04/30/01 06:39
RE: IMAP functions?Armel FAUVEAU04/29/01 17:47
RE: IMAP functions?Xan Nick04/29/01 10:33
IMAP functions?Justin Plock04/28/01 01:24
disconneting ....E.Daspet04/28/01 00:49
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.