note 33888 added to function.fsockopen
| From: | brage at rack1 dot php dot net | Date: | Thu, 10 Jul 2003 07:16:02 +0000 |
| Subject: | note 33888 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51826@lists.php.net to get a copy of this message | ||
thought you guys may appreciate this function, allows you to pass an array of urls to download and
does so simultaneously using non-blocking sockets, then returns the data in an array.
// function connects to an array of URLS at the same time
// and returns an array of results.
function multiHTTP ($urlArr) {
$sockets = Array(); // socket array!
$urlInfo = Array(); // info arr
$retDone = Array();
$retData = Array();
$errno = Array();
$errstr = Array();
for ($x=0;$x<count($urlArr);$x++) {
$urlInfo[$x] = parse_url($urlArr[$x]);
$urlInfo[$x][port] = ($urlInfo[$x][port]) ? $urlInfo[$x][port] : 80;
$urlInfo[$x][path] = ($urlInfo[$x][path]) ? $urlInfo[$x][path] : "/";
$sockets[$x] = fsockopen($urlInfo[$x][host], $urlInfo[$x][port],
$errno[$x], $errstr[$x], 30);
socket_set_blocking($sockets[$x],FALSE);
$query = ($urlInfo[$x][query]) ? "?" . $urlInfo[$x][query] : "";
fputs($sockets[$x],"GET " . $urlInfo[$x][path] . "$query HTTP/1.0\r\nHost: " .
$urlInfo[$x][host] . "\r\n\r\n");
}
// ok read the data from each one
$done = false;
while (!$done) {
for ($x=0; $x < count($urlArr);$x++) {
if (!feof($sockets[$x])) {
if ($retData[$x]) {
$retData[$x] .= fgets($sockets[$x],128);
} else {
$retData[$x] = fgets($sockets[$x],128);
}
} else {
$retDone[$x] = 1;
}
}
$done = (array_sum($retDone) == count($urlArr));
}
return $retData;
}
# also if anyone has idea to improve this that would be wonderful
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33888
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33888&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33888&report=yes