tempnam — 建立一个具有唯一文件名的文件,结合CURL的CURLOPT_COOKIEJAR使用很实用

jackxiang 2011-2-14 11:01 | |
tempnam — 建立一个具有唯一文件名的文件
说明
string tempnam ( string $dir , string $prefix )
在指定目录中建立一个具有唯一文件名的文件。如果该目录不存在,tempnam() 会在系统临时目录中生成一个文件,并返回其文件名。
简单Demo:
<?php
$tmpfname = tempnam("/tmp", "FOO");
$handle = fopen($tmpfname, "w");
fwrite($handle, "writing to tempfile");
fclose($handle);
// do here something
unlink($tmpfname);
?>





<?php
/*******************************************************************************
Version: 1.11 ($Rev: 175 $)
Website: http://www.115.co
Author: S.C. Chen <admin@185.cm>
Acknowledge: Jose Solorzano (http://www.115.co/)
Contributions by:QQ交流群:89097023
    Yousuke Kumakura (Attribute filters)
    Vadim Voituk (Negative indexes supports of "find" method)
    Antcs (Constructor with automatically load contents either text or file/url)
Licensed under The MIT License
Redistributions of files must retain the above copyright notice.
*******************************************************************************/
//缓存时间,单位:秒
$t = 360;
if(!is_file('index.html')||(time()-filemtime('index.html'))>$t){
  //微博帐号
  $qq = 'kuaisubeian';
  //经过腾讯那个md5_3()加密后的密码
  $pwd = '624D3274815F2237817A7C62F42DD26A';
  $verifyURL = 'http://ptlogin2.qq.com/check?uin=@'.$qq.'&appid=46000101';
  $loginURL = 'http://ptlogin2.qq.com/login?';

  //获取验证码及第一次cookie
  $curl = curl_init($verifyURL);
  $cookie_jar = tempnam('.', 'cookie');
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  $verifyCode = curl_exec($curl);
  curl_close($curl);
  $verifyCode = strtoupper(substr($verifyCode, 18, 4));
  //echo '验证码:'.$verifyCode;
  //echo '<hr />';
  // echo 'Cookies:'.$cookie_jar;
  // echo '<hr />';

  //发送登录请求并获取第二次cookie
  $loginURL .= 'u=@'.$qq.'&p='.md5($pwd.$verifyCode).'&verifycode='.$verifyCode.'&aid=46000101&u1=http%3A%2F%2Ft.qq.com&h=1&from_ui=1&fp=loginerroralert';
  //echo '登录地址:'.$loginURL;
  //echo '<hr />';
  $curl = curl_init($loginURL);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  $loginResult = curl_exec($curl);
  curl_close($curl);
  //echo '登录验证结果:'.$loginResult;
  //echo '<hr />';

  //获取第三次cookie
  $curl = curl_init('http://t.qq.com');
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  $loginResult = curl_exec($curl);
  curl_close($curl);

  //第四次
  $curl = curl_init('http://t.qq.com/'.$qq.'/mine');
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  $loginResult = curl_exec($curl);
  curl_close($curl);

  unlink($cookie_jar);
  file_put_contents('index.html',$loginResult);
}
include('cnz.php');
$html = file_get_html('index.html');
$talkList = $html->find('#talkList');
$lastTalk = $talkList[0];

$userName = $lastTalk->children(0)->children(1)->find('.userName');
$msgCnt = $lastTalk->children(0)->children(1)->find('.msgCnt');
$pubInfo =  $lastTalk->children(0)->children(1)->find('.pubInfo');

$userName = $userName[0]->plaintext;

$result = '';


//大于二则是转播

if(count($msgCnt) < 10){
  $pi = $pubInfo[0]->find('.left');
  $result = $userName.$msgCnt[0]->plaintext.'<p style="text-align:center;"> <span style="display:none"></span></p>
<title>QQweiboQQ|www.beiantuan.com</title><meta http-equiv="content-type" content="text/html; charset=UTF-8" /><span style="font-style: italic; color: rgb(238, 29, 36);">'.$pi[0]->children(0)->plaintext.' '.$pi[0]->children(1)->plaintext.'</span>';
}else{
  $pi = $pubInfo[1]->find('.left');
  $result = $userName.$msgCnt[0]->plaintext.'['.$msgCnt[1]->plaintext.'] <span style="font-style: italic; color: rgb(149, 158, 135);">'.$pi[0]->plaintext.'</span>';
}
echo $result;


?>

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/4020/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
2019-4-2 19:18 | 热
流域
分页: 1/1 第一页 1 最后页
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]