Hi
I am not sure if this should go in the Newbies section or here shrug
I am fairly new to PHP, and have a problem with getting data from another website.
I wrote a script to send some POST data and get the return page for several sites, however when I tested it, I realized that it was slow as fsockopen() doesn't return until it has resolved the hostname and made a successful connection.
I then looked into it, and managed to find this page on how to make asynchronous connections. Great I though, get this to work, write in the changes on how to make it work with POST, and the sites I want etc.
I copied the script, and made the changes specified to convert it to PHP 4, unfortunately it doesn't work, and I cannot find out why. I also cannot find much alternative information on how to do this.
The script is here and the script output is here
My server is running PHP4.4.2
EDIT:
For a bit more obvious script:
<?php
$hosts = array("yacoby.trap17.com", "yacoby.silgrad.com", "silgrad.com");
$timeout = 15;
$status = array();
$sockets = array();
/* Initiate connections to all the hosts simultaneously */
foreach ($hosts as $id => $host) {
$s = non_blocking_connect("$host","80", $errno, $errstr, $timeout,
STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
if ($s) {
$sockets[$id] = $s;
$status[$id] = "in progress";
} else {
$status[$id] = "failed, $errno $errstr";
}
}
/* Now, wait for the results to come back in */
while (count($sockets)) {
$read = $write = $sockets;
/* This is the magic function - explained below */
$n = socket_select($read, $write, $e = null, $timeout);
if ($n > 0) {
/* readable sockets either have data for us, or are failed
* connection attempts */
foreach ($read as $r) {
$id = array_search($r, $sockets);
$data = socket_read($r, 8192);
if (strlen($data) == 0) {
if ($status[$id] == "in progress") {
$status[$id] = "failed to connect";
}
socket_close($r);
unset($sockets[$id]);
} else {
$status[$id] .= $data;
}
}
/* writeable sockets can accept an HTTP request */
foreach ($write as $w) {
$id = array_search($w, $sockets);
socket_write($w, "HEAD / HTTP/1.0\r\nHost: "
. $hosts[$id] . "\r\n\r\n");
$status[$id] = "waiting for response";
}
} else {
/* timed out waiting; assume that all hosts associated
* with $sockets are faulty */
foreach ($sockets as $id => $s) {
$status[$id] = "timed out " . $status[$id];
}
break;
}
}
foreach ($hosts as $id => $host) {
echo "Host: $host\n";
echo "Status: " . $status[$id] . "\n\n";
}
// This value is correct for Linux, other systems have other values
define('EINPROGRESS', 115);
function non_blocking_connect($host, $port, &$errno, &$errstr, $timeout) {
$ip = gethostbyname($host);
$s = socket_create(AF_INET, SOCK_STREAM, 0);
if (socket_set_nonblock($s)) {
$r = @socket_connect($s, $ip, $port);
if ($r || socket_last_error() == EINPROGRESS) {
$errno = EINPROGRESS;
return $s;
}
}
$errno = socket_last_error($s);
$errstr = socket_strerror($errno);
socket_close($s);
return false;
}
?>
Does anyone know what is wrong with the script?
Many thanks,
~Yacoby