如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量的

yipeiwu_com6年前PHP代码库

可以使用以下函数来实现: 

<?php
function posttohost($url, $data) {
$url = parse_url($url);
if (!$url) return "couldn't parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url['query'])) { $url['query'] = ""; } 

$encoded = ""; 

while (list($k,$v) = each($data)) {
$encoded .= ($encoded ? "&" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($v);


$fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
if (!$fp) return "Failed to open socket to $url[host]"; 

fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($encoded) . "\n");
fputs($fp, "Connection: close\n\n"); 

fputs($fp, "$encoded\n"); 

$line = fgets($fp,1024);
if (!eregi("^HTTP/1\.. 200", $line)) return; 

$results = ""; $inheader = 1;
while(!feof($fp)) {
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
elseif (!$inheader) {
$results .= $line;
}
}
fclose($fp); 

return $results;
}
?>
--------------------------------------------------------------------------------------------------
也可以这样 

<?php
$URL="www.mysite.com/test.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://$URL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Data1=blah&Data2=blah");
curl_exec ($ch);
curl_close ($ch);
?>


相关文章

PHP文件下载类

复制代码 代码如下:<?    //==================================================== ...

php检索或者复制远程文件的方法

本文实例讲述了php检索或者复制远程文件的方法。分享给大家供大家参考。具体实现方法如下: <?php if(!@copy('http://someserver.com/...

thinkphp5 migrate数据库迁移工具

thinkphp5 migrate数据库迁移工具

tp5相对与tp3.2有很大的不同 migrate是其中一点,通过migrate程序员可以在php代码中创建数据库修改回滚等操作 首先下载migrate扩展,命令行到当前项目目录下执行...

PHP常用数组函数介绍

在编程中查手册是少不了的,所以要会学着使用已有的东西,就如PHP中的数组处理函数已经有排序函数了,为什么还要在写东西是费着劲去写冒泡或者堆排或者快排呢。   编程是间接的过程,也是重用的...

php下网站防IP攻击代码,超级实用

今天我开发了下面的代码,算是大功初成,一天拦截了15个IP,服务器负载正常。 复制代码 代码如下: <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADD...