Page 1 of 1

Accessing box.net from php code

Posted: Mon Mar 27, 2006 5:40 am
by lars
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I am trying to implement this code in PHP to connect to http://www.box.net:

Code: Select all

API Documentation
Authorization
XML should be posted to http://www.box.net/ping 
<xml>
    <action>
       authorization
    </action>
    <login>
       login_here
    </login>
    <password>
       password_here
    </password>
</xml>
I have tried the following without success. I appreciate your help:

Code: Select all

<?php
error_reporting(E_ALL);

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

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

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

/* 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 /ping HTTP/1.1\r\n";
$in .= "Host: http://www.box.net\r\n";
$in .= "Content-Type: text/xml\r\n";
$in .= "<xml>\r\n";
$in .= "<action>authorization</action>\r\n";
$in .= "<login>login</login>\r\n";
$in .= "<password>password</password>\r\n";
$in .= "</xml>\r\n\r\n";

$out = '';

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

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

echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Mar 27, 2006 5:47 am
by lars
These are the errors that I get:

TCP/IP Connection
OK. Attempting to connect to '67.15.218.200' on port '80'...
Warning: socket_connect() unable to connect [110]: Connection timed out in test.php on line 21
OK. Sending HTTP HEAD request...
Warning: socket_write() unable to write to socket 6 [32]: Broken pipe in test.php on line 40
OK. Reading response:
Warning: socket_read() unable to read from socket [107]: Transport endpoint is not connected in test.php on line 44
Closing socket...OK.

Posted: Mon Mar 27, 2006 11:20 am
by feyd
Have you tried setting the timeout length a bit longer?

Alternately, you could possibly use a timeout implementation example a user posted on the socket_connect() page.

Even more alternate, you could possibly use fsockopen() or cURL to perform your query.

Posted: Mon Mar 27, 2006 12:15 pm
by lars
Feyd,

thanks for the edited msg :)

I have tried fsockopen() but I get a timeout, no matter if I increase the timeout interval.

Curiously, if I do a telnet http://www.box.net 80, it instantly connects.

What may be failing ? Thanks,

Posted: Mon Mar 27, 2006 12:25 pm
by lars
I wonder if it may be that my website provider is blocking the sockets use from php ?

Posted: Mon Mar 27, 2006 12:59 pm
by feyd
talk to them and find out. :) It is quite possible due to security concerns.