PHP模拟POST的方法 PHP主动POST数据到JSP采用哪种方式好?最好有例子,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 后台模拟 哪种都差不多 最常用的curl模块 function post_url($url, $post = "", $host = "www.ydtuiguang.com", $referrer = 'http://www.ydtuiguang.com/', $proxy = -1){ if(function_exists("curl_init")){ $ch = @curl_init(); @curl_setopt($ch, CURLOPT_URL, $url); if(!empty($proxy["address"])) @curl_setopt($ch, CURLOPT_PROXY, strpos($proxy["address"], "http") === 0 ? $proxy["address"] : "http://".$proxy["address"]); if(!empty($proxy["account"]) && !empty($proxy["password"])) @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy["account"].":".$proxy["password"]); @curl_setopt($ch, CURLOPT_REFERER, $referrer); @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)"); @curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH); @curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH); @curl_setopt($ch, CURLOPT_HEADER, 0); @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); @curl_setopt($ch, CURLOPT_TIMEOUT, 1000); if (!empty($post)) { @curl_setopt($ch, CURLOPT_POST, 1); @curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } $result = @curl_exec($ch); @curl_close($ch); }elseif(function_exists("fsockopen")){ $httpheader = "POST ".$url." HTTP/1.1\r\n"; $httpheader .= "Accept: */*\r\n"; $httpheader .= "Accept-Language: zh-cn\r\n"; $httpheader .= "Referer: ".$referrer."\r\n"; $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n"; $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"; $httpheader .= "Host: ".$host."\r\n"; $httpheader .= "Content-Length: ".strlen($post)."\r\n"; $httpheader .= "Connection: Keep-Alive\r\n"; $httpheader .= "\r\n"; $httpheader .= $post; $fd = fsockopen($host, 80); fwrite($fd, $httpheader); $result = ""; while(!feof($fd)){ $result .= fread($fd, 8192); } fclose($fd); }elseif(function_existes('file_get_contents')){ $httpheader = "POST ".$url." HTTP/1.1\r\n"; $httpheader .= "Accept: */*\r\n"; $httpheader .= "Accept-Language: zh-cn\r\n"; $httpheader .= "Referer: ".$referrer."\r\n"; $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n"; $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"; $httpheader .= "Host: ".$host."\r\n"; $httpheader .= "Content-Length: ".strlen($post)."\r\n"; $httpheader .= "Connection: Keep-Alive\r\n"; $opts = array( 'http'=>array( 'method'=>"POST", 'header'=>$httpheader, 'content'=>$post ) ); $context = stream_context_create($opts); $result = file_get_contents($url, 'r', $context); } return $result;} //FORM方式数据提交 //$gateway 网关地址 //$para 请求参数数组 //$method FORM提交方式 POST/GET public function formpost($gateway, $para, $method='POST', $target='_self') { $uniqid = uniqid(); $frm_id = 'frm_pay_' . uniqid(); $frm_html = "<p>please waiting...</p>"; $frm_html .= "<form id=\"$frm_id\" name=\"$frm_id\" action=\"$gateway\" method=\"$method\" target=\"$target\">"; if (is_array($para)) { foreach ($para as $key => $val) { $key = htmlspecialchars($key); $val = htmlspecialchars($val); $frm_html.= "<input type=\"hidden\" name=\"$key\" value=\"$val\"/>"; } } $frm_html .= "<script>document.getElementById('$frm_id').submit();</script>"; echo $frm_html; exit; } //SOCKET方式数据提交 //$gateway 网关地址 //$para 请求参数数组 //$method FORM提交方式 //return 获取返回的处理结果 public function httppost($gateway, $data, $opt = array()) { $ch = curl_init($gateway); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $cert = $opt['cert']; $passwd = $opt['passwd']; if (is_file($cert) && $passwd) { curl_setopt($ch, CURLOPT_SSLCERT, $cert); curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $passwd); } $result = curl_exec($ch); if (curl_errno($ch)) { return 'curl_error:' . curl_error($ch); } else { curl_close($ch); return $result; } }正好在做个项目有用到 php字符串截取问题【送分题】 php 页面能正常访问,但是一刷新就成空白页面了 PHP url传递来的参数不能直接用了吗? 网页中怎么提取视频资源的地址 问用户是与否好进行写入数据库。如何实现? 如果谁用.net能做这样的系统,那我就服了他了,做不出来就不要说php不如.net之类的话了 这代码什么意思,去哪里看它们的源代码? 请问在北京,有哪个电脑学校可以学习PHP? 小白求助 Php文件上传功能 帮忙弄个正则表达式 http://localhost:8080/phpmyadmin打不开怎么办
function post_url($url, $post = "", $host = "www.ydtuiguang.com", $referrer = 'http://www.ydtuiguang.com/', $proxy = -1){
if(function_exists("curl_init")){
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_URL, $url);
if(!empty($proxy["address"]))
@curl_setopt($ch, CURLOPT_PROXY, strpos($proxy["address"], "http") === 0 ? $proxy["address"] : "http://".$proxy["address"]);
if(!empty($proxy["account"]) && !empty($proxy["password"]))
@curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy["account"].":".$proxy["password"]);
@curl_setopt($ch, CURLOPT_REFERER, $referrer);
@curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)");
@curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH);
@curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH);
@curl_setopt($ch, CURLOPT_HEADER, 0);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
@curl_setopt($ch, CURLOPT_TIMEOUT, 1000); if (!empty($post)) {
@curl_setopt($ch, CURLOPT_POST, 1);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
} $result = @curl_exec($ch);
@curl_close($ch);
}elseif(function_exists("fsockopen")){
$httpheader = "POST ".$url." HTTP/1.1\r\n";
$httpheader .= "Accept: */*\r\n";
$httpheader .= "Accept-Language: zh-cn\r\n";
$httpheader .= "Referer: ".$referrer."\r\n";
$httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";
$httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";
$httpheader .= "Host: ".$host."\r\n";
$httpheader .= "Content-Length: ".strlen($post)."\r\n";
$httpheader .= "Connection: Keep-Alive\r\n";
$httpheader .= "\r\n";
$httpheader .= $post;
$fd = fsockopen($host, 80);
fwrite($fd, $httpheader);
$result = "";
while(!feof($fd)){
$result .= fread($fd, 8192);
}
fclose($fd);
}elseif(function_existes('file_get_contents')){
$httpheader = "POST ".$url." HTTP/1.1\r\n";
$httpheader .= "Accept: */*\r\n";
$httpheader .= "Accept-Language: zh-cn\r\n";
$httpheader .= "Referer: ".$referrer."\r\n";
$httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";
$httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";
$httpheader .= "Host: ".$host."\r\n";
$httpheader .= "Content-Length: ".strlen($post)."\r\n";
$httpheader .= "Connection: Keep-Alive\r\n";
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>$httpheader,
'content'=>$post
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, 'r', $context);
}
return $result;
}
//$gateway 网关地址
//$para 请求参数数组
//$method FORM提交方式 POST/GET
public function formpost($gateway, $para, $method='POST', $target='_self')
{
$uniqid = uniqid();
$frm_id = 'frm_pay_' . uniqid(); $frm_html = "<p>please waiting...</p>";
$frm_html .= "<form id=\"$frm_id\" name=\"$frm_id\" action=\"$gateway\" method=\"$method\" target=\"$target\">"; if (is_array($para)) {
foreach ($para as $key => $val) {
$key = htmlspecialchars($key);
$val = htmlspecialchars($val);
$frm_html.= "<input type=\"hidden\" name=\"$key\" value=\"$val\"/>";
}
} $frm_html .= "<script>document.getElementById('$frm_id').submit();</script>"; echo $frm_html;
exit;
} //SOCKET方式数据提交
//$gateway 网关地址
//$para 请求参数数组
//$method FORM提交方式
//return 获取返回的处理结果
public function httppost($gateway, $data, $opt = array())
{
$ch = curl_init($gateway);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $cert = $opt['cert'];
$passwd = $opt['passwd'];
if (is_file($cert) && $passwd) {
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $passwd);
} $result = curl_exec($ch); if (curl_errno($ch)) {
return 'curl_error:' . curl_error($ch);
}
else {
curl_close($ch);
return $result;
}
}
正好在做个项目有用到