php中模拟POST提交数据两种方法

通过curl函数
PHP中的CURL函数库(Client URL Library Function)

curl_close — 关闭一个curl会话
curl_copy_handle — 拷贝一个curl连接资源的所有内容和参数
curl_errno — 返回一个包含当前会话错误信息的数字编号
curl_error — 返回一个包含当前会话错误信息的字符串
curl_exec — 执行一个curl会话
curl_getinfo — 获取一个curl连接资源句柄的信息
curl_init — 初始化一个curl会话
curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄资源
curl_multi_close — 关闭一个批处理句柄资源
curl_multi_exec — 解析一个curl批处理句柄
curl_multi_getcontent — 返回获取的输出的文本流
curl_multi_info_read — 获取当前解析的curl的相关传输信息
curl_multi_init — 初始化一个curl批处理句柄资源
curl_multi_remove_handle — 移除curl批处理句柄资源中的某个句柄资源
curl_multi_select — Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt_array — 以数组的形式为一个curl设置会话参数
curl_setopt — 为一个curl设置会话参数
curl_version — 获取curl相关的版本信息

curl_init()函数的作用初始化一个curl会话,curl_init()函数唯一的一个参数是可选的,表示一个url地址。
curl_exec()函数的作用是执行一个curl会话,唯一的参数是curl_init()函数返回的句柄。
curl_close()函数的作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。

 代码如下 复制代码

$post_data = array();
$post_data['clientname'] = "test08";
$post_data['clientpasswd'] = "test08";
$post_data['submit'] = "submit";
$url='http://xxx.xxx.xxx.xx/xx/xxx/top.php';
$o="";
foreach ($post_data as $k=>$v)
{
    $o.= "$k=".urlencode($v)."&";
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
//为了支持cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);

模仿用户登录
模拟登录到sina

我们要抓取数据,可能是登录以后的内容,这个时候我们就要用到curl的模拟登录功能了。

 

 代码如下 复制代码

<?php

function checklogin( $user, $password )
 {
 if ( empty( $user ) || empty( $password ) )
 {
 return 0;
 }
 $ch = curl_init( );
 curl_setopt( $ch, CURLOPT_REFERER, "http://mail.sina.com.cn/index.html" );
 curl_setopt( $ch, CURLOPT_HEADER, true );
 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
 curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT );
 curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
 curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT );
 curl_setopt( $ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi" );
 curl_setopt( $ch, CURLOPT_POST, true );
 curl_setopt( $ch, CURLOPT_POSTFIELDS, "&logintype=uid&u=".urlencode( $user )."&psw=".$password );
 $contents = curl_exec( $ch );
 curl_close( $ch );
 if ( !preg_match( "/Location: (.*)\/cgi\/index\.php\?check_time=(.*)n/", $contents, $matches ) )
 {
 return 0;
 }else{
 return 1;
 }
 }

 define( "USERAGENT", $_SERVER['HTTP_USER_AGENT'] );
 define( "COOKIEJAR", tempnam( "/tmp", "cookie" ) );
 define( "TIMEOUT", 500 );

 echo checklogin("zhangying215","xtaj227");
 ?>

2.通过fsockopen

.PHP fsockopen函数说明:

Open Internet or Unix domain socket connection(打开套接字链接)

Initiates a socket connection to the resource specified by target .

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ).就是返回一个文件句柄

开启PHP fsockopen这个函数

PHP fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启。

 代码如下 复制代码

$URL=‘http://xxx.xxx.xxx.xx/xx/xxx/top.php';
$post_data['clientname'] = "test08";
$post_data['clientpasswd'] = "test08";
$post_data['submit'] = "ログイン";
$referrer="";
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer=$_SERVER["SCRIPT_URI"];
 
// making string from $data
foreach($post_data as $key=>$value)
$values[]="$key=".urlencode($value);
 
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1n";
$request.="Host: ".$URL_Info["host"]."n";
$request.="Referer: $referrern";
$request.="Content-type: application/x-www-form-urlencodedn";
$request.="Content-length: ".strlen($data_string)."n";
$request.="Connection: closen";
$request.="n";
$request.=$data_string."n";
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
    $result .= fgets($fp, 128);
}
fclose($fp);

如果出现

Warning: fsockopen() has been disabled for security reasons in D:…cos-html-cachecos-html-cache.php on line 35
换了其他版本的cos-html-cache,还是不行。后来找到下面的方法。 (结果不行,因为函数都被禁用了。)
大家试下,很少有我这样的情况的,用其他替代函数。
一、如何禁用fsockopen()
下面是两种常用的禁用fsockopen的方法。
1、修改php.ini,将 disable_functions = 后加入 fsockopen
2、修改php.ini,将 allow_url_fopen = On 改为 allow_url_fopen = Off
二、如何解决fsockopen函数被禁用
1、如果服务器没有同时禁用pfsockopen,那么直接将fsockopen函数替换为pfsockopen。
具体操作:搜索程序中的字符串 fsockopen( 替换为 pfsockopen( 。示例如下
修改前:
$fp = fsockopen($host, 80, $errno, $errstr, 30);
修改后:
$fp = pfsockopen($host, 80, $errno, $errstr, 30);

时间: 2024-09-21 01:40:55

php中模拟POST提交数据两种方法的相关文章

jQuery的 $.ajax防止重复提交的两种方法(推荐)_jquery

下面给大家带来两种关于jquery 的ajax防止重复提交的解决方法,具体介绍如下所示: 1.第一种,对于onclick事件触发的的ajax 可以采用如下方法: 即在beforeSend中使点击按钮不可用,ajax结果返回后置为可用 $.ajax( { type: 'POST', url: APP+'?m=Shopping&a=ajaxSubmitorder&sid='+sid+'&src='+src, cache:false, dataType: 'json', data: {'

JavaScript禁止用户多次提交的两种方法_javascript技巧

[当服务器超载时,会出现提交卡顿的现象,但是用户在操作时,会不停重复点击提交,会造成服务器压力更大.所以我们需要进行限制] [1]将提交按钮禁止 <html> <head> <script> //禁止默认行为 因为这里要模拟服务器超载的时候,所以需要先禁止掉submit按钮自动提交的功能 function preventDef(event){ event=event||window.event; if(event.preventDefault){ return even

利用JavaScript阻止表单提交的两种方法_javascript技巧

在JavaScript中,阻止表单默认提交行为的方法有两种,分别是: (1) return false 示例代码 <form name="loginForm" action="login.aspx" method="post"> <button type="submit" value="Submit" id="submit">Submit</button&g

实用技巧:PHP中调用Java类的两种方法

Java语言功能强大,因此在许多情况下在php中来调用Java的功能将十分有用.在php中调用Java语言有两种方法,一种是使用php中的Java扩展模块,另一种是使用minij2ee应用服务器提供的SJOP协议实现.下面我们来比较一下这两种方法各自的特点. 1.php的Java模块 php发布版中包含一个Java扩展模块,可以用来调用Java对象,例如: <?php$system=new Java("java.lang.System");print "Java ver

PowerShell中字符串分行显示的两种方法技巧

  这篇文章主要介绍了PowerShell中字符串分行显示的两种方法技巧,本文直接给出代码实例,需要的朋友可以参考下 复制代码 代码如下: $a="1111111111111111111111101111111111111111111111111111111111111110111111111111111111011111 11111111111111111111111111111111111111111111111111111111111111111111111101111" 字符串

java-请问:用Java代码中实现在一个类编写两种方法,下面的代码接下来怎么办呢?谢谢!

问题描述 请问:用Java代码中实现在一个类编写两种方法,下面的代码接下来怎么办呢?谢谢! 解决方案 啥东西,写两个方法,那你就写两个函数就行了,什么怎么办 解决方案二: 你的类里不就2个方法了吗

在PHP中实现用户身份认证两种方法

用户在设计和维护站点的时候,经常需要限制对某些重要文件或信息的访问.通常,我们可以采用内置于Web于HTTP协议的用户身份验证机制.当访问者浏览受保护页面时,客户端浏览器会弹出对话窗口要求用户输入用户名和密码,对用户的身份进行验证,以决定用户是否有权访问页面.下面用两种方法来说明其实现原理. 一.用HTTP标头来实现 标头是服务器以HTTP协议传送HTML信息到浏览器前所送出的字串.HTTP采用一种挑战/响应模式对试图进入受密码保护区域的用户进行身份验证.具体来说,当用户首次向Web器发出访问受

Android中Intent传递对象的两种方法(Serializable,Parcelable)

Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,为了让大家更容易理解我还是照常写了一个简单的Demo,大家就一步一步跟我来吧! 第一步:新建一个Android工程命名为ObjectTranDemo(类比较

DotNet开发中关于SQLServer连接的两种方法之比较

      在.net中关于SQL Server有两种打开连接的方式,一种是在System.Data.SqlClient命名空间下,相信很多人都是用这里面的方法进行编程的,还有一种是在Microsoft.ApplicationBlocks.Data.SqlHelper命名空间下,方法大致相同,为什么会有两个不同的空间提供相似的功能呢?     微软在推出.net体系时默认附带了System和Microsoft两个命名空间,Microsoft命名空间正是System命名空间的有力补充,即以当前讨论