N
namliw_reloaded
Guest
Hi, i need to post some values to a php page, i've tried with pear and fsockopen but with pear the values are not send, and with fsockopen i can't connect to my test page in my local apache, does anyone knows how to do this without curl? thanks,
// code with fsockopen
function post_it($datastream, $url) {
$url = preg_replace("@^http://@i", "", $url);
$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");
echo "$url<br>$host<br>$uri";
$reqbody = "";
foreach($datastream as $key=>$val) {
if ($reqbody!="") $reqbody.= "&";
$reqbody.= $key."=".urlencode($val);
}
$contentlength = strlen($reqbody);
$reqheader = "POST $uri HTTP/1.1\r\n".
"Host: $host\n". "User-Agent: PostIt\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $contentlength\r\n\r\n".
"$reqbody\r\n";
$socket = fsockopen($host, 80, $errno, $errstr,30);
if (!$socket) {
$result["errno"] = $errno;
$result["errstr"] = $errstr;
return $result;
}
fputs($socket, $reqheader,strlen($reqheader));
while (!feof($socket)) {
$result[] = fgets($socket, 4096);
}
fclose($socket);
return $result;
}
$data["foo"] = "some";
$data["bar"] = "data";
$result = post_it($data, "http://localhost/pruebasWillman/submitAjaxXml/proxi.php");
if (isset($result["errno"])) {
$errno = $result["errno"];
$errstr = $result["errstr"];
echo "<B>Error $errno</B> $errstr";
exit;
} else {
for($i=0;$i< count($result); $i++) echo $result[$i];
}
//code with pear
require_once 'HTTP/Request.php';
$url = 'index.php';
$data = "HOLA";
$req = new HTTP_Request($url,array("method" => "POST"));
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addHeader("Content-Type", "text/plain");
$req->addPostData("xmldata",$data);
$req->sendRequest();
echo "---> <br>".$req->getResponseBody();
// code with fsockopen
function post_it($datastream, $url) {
$url = preg_replace("@^http://@i", "", $url);
$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");
echo "$url<br>$host<br>$uri";
$reqbody = "";
foreach($datastream as $key=>$val) {
if ($reqbody!="") $reqbody.= "&";
$reqbody.= $key."=".urlencode($val);
}
$contentlength = strlen($reqbody);
$reqheader = "POST $uri HTTP/1.1\r\n".
"Host: $host\n". "User-Agent: PostIt\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $contentlength\r\n\r\n".
"$reqbody\r\n";
$socket = fsockopen($host, 80, $errno, $errstr,30);
if (!$socket) {
$result["errno"] = $errno;
$result["errstr"] = $errstr;
return $result;
}
fputs($socket, $reqheader,strlen($reqheader));
while (!feof($socket)) {
$result[] = fgets($socket, 4096);
}
fclose($socket);
return $result;
}
$data["foo"] = "some";
$data["bar"] = "data";
$result = post_it($data, "http://localhost/pruebasWillman/submitAjaxXml/proxi.php");
if (isset($result["errno"])) {
$errno = $result["errno"];
$errstr = $result["errstr"];
echo "<B>Error $errno</B> $errstr";
exit;
} else {
for($i=0;$i< count($result); $i++) echo $result[$i];
}
//code with pear
require_once 'HTTP/Request.php';
$url = 'index.php';
$data = "HOLA";
$req = new HTTP_Request($url,array("method" => "POST"));
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addHeader("Content-Type", "text/plain");
$req->addPostData("xmldata",$data);
$req->sendRequest();
echo "---> <br>".$req->getResponseBody();