Netwrkengeer
IS-IT--Management
I just wrote some code, and I'm getting an error
Warning: Failed opening '/usr/home/jbh/htdocs/codetest.phtml' for inclusion (include_path='.:/usr/local/lib/php') in /usr/home/jbh/htdocs/pagetest.phtml on line 9
Fatal error: Call to undefined function: codetest() in /usr/home/jbh/htdocs/pagepost.phtml on line 14
let me give you a description for the code,
This function (allegedly) takes an associative array and a URL. The array is URL-encoded and then POSTed to the URL. If the request succeeds, the response, if any, is returned in a scalar array. Outputting this is the caller's responsibility; bear in mind that it will include the HTTP headers. If the request fails, an associative array is returned with the elements 'errno' and 'errstr' corresponding to the error number and error message.
/*
this is the codetest.phtml code
*/
<?php
function codetest($DataStream, $URL) {
// Strip http:// from the URL if present
$URL = ereg_replace("^ "", $URL);
// Separate into Host and URI
$Host = substr($URL, 0, strpos($URL, "/");
$URI = strstr($URL, "/"
// Form up the request body
$ReqBody = "";
while (list($key, $val) = each($DataStream)) {
if ($ReqBody) $ReqBody.= "&";
$ReqBody.= $key."=".urlencode($val);
}
$ContentLength = strlen($ReqBody);
// Generate the request header
$ReqHeader =
"POST $URI HTTP/1.1\n".
"Host: $Host\n".
"User-Agent: codetest\n".
"Content-Type: application/x- "Content-Length: $ContentLength\n\n".
"$ReqBody\n";
// Open the connection to the host
$socket = fsockopen($Host, 80, &$errno, &$errstr);
if (!$socket) {
$Result["errno"] = $errno;
$Result["errstr"] = $errstr;
return $Result;
}
$idx = 0;
fputs($socket, $ReqHeader);
while (!feof($socket)) {
$Result[$idx++] = fgets($socket, 128);
}
return $Result;
}
?>
/*
this is the pagetest.phtml code
*/
/*
this is Sample code calling codetest(). Note that it is better to test for an error result with isset() rather than simply if ($Result["errno"]) as the error number can be zero.
*/
<?php
include("codetest.phtml"
$d["foo"] = "some";
$d["bar"] = "data";
$Result = codetest($d, "
if (isset($Result["errno"])) {
$errno = $Result["errno"]; $errstr = $Result["errstr"];
echo "<B>Error $errno</B> $errstr";
exit;
}
else {
while (list($key, $val) = each($Result)) echo $val;
}
?>
/*
thanks Rob
*/
Warning: Failed opening '/usr/home/jbh/htdocs/codetest.phtml' for inclusion (include_path='.:/usr/local/lib/php') in /usr/home/jbh/htdocs/pagetest.phtml on line 9
Fatal error: Call to undefined function: codetest() in /usr/home/jbh/htdocs/pagepost.phtml on line 14
let me give you a description for the code,
This function (allegedly) takes an associative array and a URL. The array is URL-encoded and then POSTed to the URL. If the request succeeds, the response, if any, is returned in a scalar array. Outputting this is the caller's responsibility; bear in mind that it will include the HTTP headers. If the request fails, an associative array is returned with the elements 'errno' and 'errstr' corresponding to the error number and error message.
/*
this is the codetest.phtml code
*/
<?php
function codetest($DataStream, $URL) {
// Strip http:// from the URL if present
$URL = ereg_replace("^ "", $URL);
// Separate into Host and URI
$Host = substr($URL, 0, strpos($URL, "/");
$URI = strstr($URL, "/"
// Form up the request body
$ReqBody = "";
while (list($key, $val) = each($DataStream)) {
if ($ReqBody) $ReqBody.= "&";
$ReqBody.= $key."=".urlencode($val);
}
$ContentLength = strlen($ReqBody);
// Generate the request header
$ReqHeader =
"POST $URI HTTP/1.1\n".
"Host: $Host\n".
"User-Agent: codetest\n".
"Content-Type: application/x- "Content-Length: $ContentLength\n\n".
"$ReqBody\n";
// Open the connection to the host
$socket = fsockopen($Host, 80, &$errno, &$errstr);
if (!$socket) {
$Result["errno"] = $errno;
$Result["errstr"] = $errstr;
return $Result;
}
$idx = 0;
fputs($socket, $ReqHeader);
while (!feof($socket)) {
$Result[$idx++] = fgets($socket, 128);
}
return $Result;
}
?>
/*
this is the pagetest.phtml code
*/
/*
this is Sample code calling codetest(). Note that it is better to test for an error result with isset() rather than simply if ($Result["errno"]) as the error number can be zero.
*/
<?php
include("codetest.phtml"
$d["foo"] = "some";
$d["bar"] = "data";
$Result = codetest($d, "
if (isset($Result["errno"])) {
$errno = $Result["errno"]; $errstr = $Result["errstr"];
echo "<B>Error $errno</B> $errstr";
exit;
}
else {
while (list($key, $val) = each($Result)) echo $val;
}
?>
/*
thanks Rob
*/