Trouble with sockets
Adam Fields
fields at surgam.net
Mon Jan 6 10:12:55 EST 2003
I'm working on a php class to post to livejournal programmatically
from my other sites, and running into some problems with the socket.
class source is below, but the basic problem is that I don't seem to
be connecting a valid socket to www.livejournal.com. I thought maybe
the post was the problem (the first symptom was I just didn't get
anything back), so I changed the request to a simple get of the
root. Still nothing. I changed the host to www.yahoo.com, and it
worked. I'm able to telnet to port 80 on www.livejournal.com (from the
server), and issue the exact same set of commands, and I get a
response, but no dice going through the socket from the script. I
tried increasing the timeout, but that's clearly not the problem. It
looks like it's establishing the socket okay, but then I get no
response back.
Any idea why this would behave this way?
----------------------------------------
class lj {
var $user;
var $pass;
function lj ($user, $pass) {
$this->user = $user;
$this->pass = $pass;
}
function postevent ($msgtext, $subject) {
$options = array('mode' => 'postevent',
'user' => $this->user,
'hpassword' => md5($this->pass),
'event' => $msgtext,
'year' => date('Y'),
'mon' => date('n'),
'day' => date('d'),
'hour' => date('H'),
'min' => date('i'),
'subject' => $subject);
foreach (array_keys($options) as $key) {
$poststring .= $key . '=' . urlencode($options[$key]) . '&';
}
$poststring = substr($poststring, 0, strlen($poststring) - 1);
$method = 'POST';
$path = '/interface/flat';
$host = 'www.livejournal.com';
$fp = fsockopen($host, 80, $errno, $errstr, 600);
if (!$fp) {
die ('Unable to connect');
}
socket_set_timeout($fp, 600);
# fputs($fp, "$method $path HTTP/1.0\
");
# fputs($fp, "Host: $host\
");
# fputs($fp, "Content-type: application/x-www-form-urlencoded\
");
# fputs($fp, "Content-length: " . strlen($poststring) . '\
\
');
# fputs($fp, $poststring);
# instead of the above post, try a simple get instead
fputs($fp, "GET / HTTP/1.0\
\
");
while (!feof ($fp)) {
$response = fgets($fp, 128);
print $response;
}
fclose ($fp);
return $response;
}
}
--
- Adam
-----
Adam Fields, Managing Partner, fields at surgam.net
Surgam, Inc. is a technology consulting firm with strong background in
delivering scalable and robust enterprise web and IT applications.
http://www.adamfields.com
More information about the talk
mailing list