Home >Backend Development >PHP Tutorial >php获取qq用户昵称和在线状态(实例分析)_php实例

php获取qq用户昵称和在线状态(实例分析)_php实例

WBOY
WBOYOriginal
2016-06-07 17:23:28985browse

QQ通过返回不同的图片,来表示在线或离线,图标也随之变换,既然图片不同,那么,返回的HTTP头信息中的Content-Length 也一定不同,而且,彩色图片一定会比同样子的暗色图片要大,于是,找出某个样式的彩色与暗色图片的中间值,就能达到通过判断头部返回长度的方法来获取QQ在线状态
以下是代码

复制代码 代码如下:

{
 error_reporting(0);
 $f=file_get_contents('http://wpa.qq.com/pa?p=1:'.$uin.':4');
 if(!$f) return(true);
 foreach($http_response_header as $val)
 {
  if(strpos($val,'Content-Length')!==false)
  {
   return(intval(substr($val,16,50))>1000);
  }
 }
}

?>


上面比较简单,下面来个更好的
复制代码 代码如下:


function tphp_qq_online( $uin )
{
    $reques  = "GET /pa?p=1:".$uin.":1 HTTP/1.1rn";
    $reques .= "Host: wpa.qq.comrn";
    $reques .= "User-Agent: PHP_QQ_SPYrnrn";

    if ( !( $socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP ) ) ) return(-1);
    if ( !( socket_connect( $socket, "wpa.qq.com", 80 ) ) ) return(-1);
    if ( !( socket_write( $socket, $reques ) ) ) return(-1);
    if ( !( $respon = socket_read( $socket, 1024, PHP_BINARY_READ ) ) ) return(-1);;
    socket_close( $socket );

    $field = explode( "rn", $respon ); 
    for ( $i=0; $i        if ( strncasecmp($field[$i], "Location:", 9) == 0 ) {
            if ( strpos( $field[$i], "online") ) {
                $ret = 1;
            } else if ( strpos( $field[$i], "offline") ) {
                $ret = 0;
            } else { 
                $ret = -1;
            } // if
            break;
        } // if
    } // for

    return( $ret );
}
/* }}} */

 

echo tphp_qq_online( 561272831 );

?>


例,qq用户昵称和在线状态
复制代码 代码如下:

//获取QQ状态
function getQQState($qq){
    $url ='http://wpa.qq.com/pa?p=2:'.$qq.':41&r=' . time ();
 $headInfo = get_headers($url,1);
 $length = $headInfo['Content-Length'];
 if ($length==1243) {
  return true;
 }else {
  return false;
 }
}
//获取QQ昵称
function getQQNick($qq){
 $str = file_get_contents('http://r.qzone.qq.com/cgi-bin/user/cgi_personal_card?uin='.$qq);
 $pattern = '/'.preg_quote('"nickname":"','/').'(.*?)'.preg_quote('",','/').'/i';
 preg_match ( $pattern,$str, $result );
 return $result[1];
}
//获取QQ姓名
function getQQName($qq){
 //$qqArr = include 'friendArr.php';//预先设置的
 //$username = $qqArr[$qq];
 if (!$username) {
  $username = getQQNick($qq);
 }
 return $username;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn