Log in

No account? Create an account
LiveJournal Client Discussions [entries|archive|friends|userinfo]
LiveJournal Client Discussions

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

[Aug. 2nd, 2006|12:09 am]
LiveJournal Client Discussions
I'm just starting at all this socket stuff in my programming.
But anyway; i have a LJ module on my website, and i thought it would be really nifty to throw a little application so my users could update their LJs straight from my site. So i'm working on an LJ-Client in PHP.

I was trying the Get Challange method of authentication, but i can't seem to get a response back from the server when i send the request. the script sits there for 10 or 20 seconds and then claims that the server didn't send a response:

this is what my test script outputs:

TCP/IP Connection

Attempting to connect to '' on port '80'...OK.
Request to be sent:
POST /interface/xmlrpc HTTP/1.0
User-Agent: XMLRPC Client 1.0
Host: www.livejournal.com
Content-Type: text/xml
Content-Length: 193

<?xml version="1.0"?>

Connection: Close

Sending HTTP POST request...OK.
Reading response:

Closing socket...OK.

And this is the script itself:


echo "<h2>TCP/IP Connection</h2><pre>\n";

/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');

/* Get the IP address for the target host. */
$address = gethostbyname('www.livejournal.com');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
} else {
echo "OK.\n";

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
} else {
echo "OK.\n";

$in = "POST /interface/xmlrpc HTTP/1.0\r\n";
$in .= "User-Agent: XMLRPC Client 1.0\r\n";
$in .= "Host: www.livejournal.com\r\n";
$in .= "Content-Type: text/xml\r\n";
$in .= "Content-Length: 193 \r\n";
$in .= "\r\n";
$in .= "<?xml version=\"1.0\"?>\r\n";
$in .= "<methodCall>\r\n";
$in .= "<methodName>LJ.XMLRPC.getchallenge</methodName>\r\n";
$in .= "<params>\r\n";
$in .= "<param>\r\n";
$in .= "\r\n";
$in .= "</param>\r\n";
$in .= "</params>\r\n";
$in .= "</methodCall>\r\n";
$in .= "Connection: Close\r\n\r\n";

echo "Request to be sent:\r\n";
echo htmlspecialchars($in);
echo "\r\n";

$out = '';

echo "Sending HTTP POST request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";

echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo htmlspecialchars($out);

echo "\r\nClosing socket...";
echo "OK.\n\n</pre>";

forgive me if this is a stupid question, this is my first dabble into sockets.

Oh, i'm aware there is an XML-RPC extension for PHP, however this is not compiled into the PHP on my server. (i'm noticing more and more how my server lacks extensions that would prove invaluable to extending my site how i wish. Oh well, i suppose that's what i get for a free high-bandwidth server)

[User Picture]From: hythloday
2006-08-02 09:31 am (UTC)
$in .= "Connection: Close\r\n\r\n";

Is this a typo? 'cause if not, this should be at the top with the other headers.

Also, your content-length is 193, and even with the erroneously placed header I only make your entity 165 bytes. I can only make it 193 bytes by: counting "\r" and "\n" as two bytes each (they're not - "\r" is ascii 13, and "\n" is ascii 10, in exactly the same way that "a" is ascii 97); counting the \" as two bytes (it's one), and by counting the double-newline that seperates the headers and the entity (it's not a part of the entity for the purposes of content-length).

This is consistent with your bug - the LJ server is basically waiting on your socket saying, "come on then, where's the rest?". I suspect if you feed it 200 bytes of spaces, you'll get the answer you're expecing (as long as you move the Connection header back to the top.

Good luck!
(Reply) (Thread)
From: lilmuckers
2006-08-02 10:01 am (UTC)

i knew it was something simple

the request is just copy/pasted from the deeloper help section, so i'd assumed it was right
(Reply) (Parent) (Thread)
[User Picture]From: hythloday
2006-08-02 10:14 am (UTC)
So then you probably want to file a bug with a patch for the documentation once you've figured out what the right request is. :)
(Reply) (Parent) (Thread)
From: lilmuckers
2006-08-02 10:03 am (UTC)
oh, thanks by the way
^^ *offers hugs*
(Reply) (Parent) (Thread)
(Deleted comment)
From: lileja
2006-08-05 03:51 am (UTC)
<struct> i mean
(Reply) (Parent) (Thread)
From: lileja
2006-08-05 03:53 am (UTC)
missing tag

should be:
$in .= "<param>\r\n";
$in .= "<struct>\r\n";
$in .= "</struct>\r\n";
$in .= "</param>\r\n";
(Reply) (Parent) (Thread)