php curl_setopt与模拟用户登录
bool curl_setopt (int ch, string option, mixed value)
curl_setopt()函数将为一个curl会话设置选项,option参数是你想要的设置.
value是这个选项给定的值,该例子为模拟登录,代码如下:
$cookie_file = tempnam( './temp' , 'cookie' ); //保存临时cookie $login_url = 'http://xxx/login.php' ;//登录页提交地址 $post_fields = 'username=&password=' ; //传值参数 $ch = curl_init( $login_url ); curl_setopt( $ch , curlopt_header, 0); curl_setopt( $ch , curlopt_returntransfer, 1); curl_setopt( $ch , curlopt_post, 1); curl_setopt( $ch , curlopt_postfields, $post_fields ); curl_setopt( $ch , curlopt_cookiejar, $cookie_file ); curl_exec( $ch ); curl_close( $ch );下列选项的值将被作为长整形使用(在option参数中指定):curlopt_infilesize: 当你上传一个文件到远程站点,这个选项告诉php你上传文件的大小。
curlopt_verbose:如果你想curl报告每一件意外的事情,设置这个选项为一个非零值.
curlopt_header:如果你想把一个头包含在输出中,设置这个选项为一个非零值.
curlopt_noprogress:如果你不会php为curl传输显示一个进程条,设置这个选项为一个非零值.
注意: php自动设置这个选项为非零值,你应该仅仅为了调试的目的来改变这个选项.
curlopt_nobody:如果你不想在输出中包含body部分,设置这个选项为一个非零值.
curlopt_failonerror:如果你想让php在发生错误(http代码返回大于等于300)时,不显示,设置这个选项为一人非零值,默认行为是返回一个正常页,忽略代码.
curlopt_upload:如果你想让php为上传做准备,设置这个选项为一个非零值.
curlopt_post:如果你想php去做一个正规的http post,设置这个选项为一个非零值,这个post是普通的 application/x-www-from-urlencoded 类型,多数被html表单使用.
curlopt_ftplistonly:设置这个选项为非零值,php将列出ftp的目录名列表.
curlopt_ftpappend:设置这个选项为一个非零值,php将应用远程文件代替覆盖它.
curlopt_netrc:设置这个选项为一个非零值,php将在你的 ~./netrc 文件中查找你要建立连接的远程站点的用户名及密码.
curlopt_followlocation:设置这个选项为一个非零值(象 'location: ')的头,服务器会把它当做http头的一部分发送(注意这是递归的,php将发送形如'location: '的头).
curlopt_put:设置这个选项为一个非零值去用http上传一个文件,要上传这个文件必须设置curlopt_infile和curlopt_infilesize选项.
curlopt_mute:设置这个选项为一个非零值,php对于curl函数将完全沉默.
curlopt_timeout:设置一个长整形数,作为最大延续多少秒.
curlopt_low_speed_limit:设置一个长整形数,控制传送多少字节.
curlopt_low_speed_time:设置一个长整形数,控制多少秒传送.
curlopt_low_speed_limit规定的字节数.
curlopt_resume_from: 传递一个包含字节偏移地址的长整形参数(你想转移到的开始表单).
curlopt_sslversion:传递一个包含ssl版本的长参数,默认php将被它自己努力的确定,在更多的安全中你必须手工设置.
curlopt_timecondition:传递一个长参数,指定怎么处理curlopt_timevalue参数,你可以设置这个参数为timecond_ifmodsince 或 timecond_isunmodsince,这仅用于http.
curlopt_timevalue:传递一个从1970-1-1开始到现在的秒数,这个时间将被curlopt_timevalue选项作为指定值使用,或被默认timecond_ifmodsince使用.
下列选项的值将被作为字符串:
curlopt_url:这是你想用php取回的url地址,你也可以在用curl_init()函数初始化时设置这个选项.
curlopt_userpwd:传递一个形如[username]:[password]风格的字符串,作用php去连接.
curlopt_proxyuserpwd:传递一个形如[username]:[password] 格式的字符串去连接http代理.
curlopt_range:传递一个你想指定的范围,它应该是'x-y'格式,x或y是被除外的,http传送同样支持几个间隔,用逗句来分隔(x-y,n-m).
curlopt_postfields:传递一个作为http [post]操作的所有数据的字符串.
curlopt_referer:在http请求中包含一个'referer'头的字符串.
curlopt_useragent:在http请求中包含一个'user-agent'头的字符串.
curlopt_ftpport:传递一个包含被ftp 'post'指令使用的ip地址,这个post指令.
告诉远程服务器去连接我们指定的ip地址,这个字符串可以是一个ip地址,一个主机名,一个网络界面名(在unix下),或是‘-’(使用系统默认ip地址).
curlopt_cookie: 传递一个包含http cookie的头连接。
curlopt_sslcert: 传递一个包含pem格式证书的字符串。
curlopt_sslcertpasswd: 传递一个包含使用curlopt_sslcert证书必需的密码。
curlopt_cookiefile: 传递一个包含cookie数据的文件的名字的字符串。这个
cookie文件可以是netscape格式,或是堆存在文件中的http风格的头。
curlopt_customrequest: 当进行http请求时,传递一个字符被get或head使用,为进行delete或其它操作是有益的.
注意:在确认你的服务器支持命令先不要去这样做,下列的选项要求一个文件描述(通过使用fopen()函数获得):
curlopt_file: 这个文件将是你放置传送的输出文件,默认是stdout.
curlopt_infile: 这个文件是你传送过来的输入文件。
curlopt_writeheader: 这个文件写有你输出的头部分。
curlopt_stderr: 这个文件写有错误而不是stderr。
<?php function curl_redir_exec( $ch , $debug = "" ) { static $curl_loops = 0; static $curl_max_loops = 20; if ( $curl_loops ++ >= $curl_max_loops ) { $curl_loops = 0; return false; } curl_setopt( $ch , curlopt_header, true); curl_setopt( $ch , curlopt_returntransfer, true); $data = curl_exec( $ch ); $debbbb = $data ; list( $header , $data ) = explode ( "nn" , $data , 2); $http_code = curl_getinfo( $ch , curlinfo_http_code); if ( $http_code == 301 || $http_code == 302) { $matches = array (); preg_match( '/location:(.*?)n/' , $header , $matches ); $url = @ parse_url (trim( array_pop ( $matches ))); //print_r($url); if (! $url ) { //couldn't process the url to redirect to $curl_loops = 0; return $data ; } $last_url = parse_url (curl_getinfo( $ch , curlinfo_effective_url)); /* if (!$url['scheme']) $url['scheme'] = $last_url['scheme']; if (!$url['host']) $url['host'] = $last_url['host']; if (!$url['path']) $url['path'] = $last_url['path'];*/ $new_url = $url [ 'scheme' ] . '://' . $url [ 'host' ] . $url [ 'path' ] . ( $url [ 'query' ]? '?' . $url [ 'query' ]: '' ); curl_setopt( $ch , curlopt_url, $new_url ); // debug('redirecting to', $new_url); return curl_redir_exec( $ch ); } else { $curl_loops =0; return $debbbb ; } } ?>查看更多关于php curl_setopt与模拟用户登录 - php高级应用的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did30096