本文实例讲述了php实现发送微博消息功能。分享给大家供大家参考,具体如下:

<?php
$cookie_file = dirname(__file__)."/weibo.cookie";
$username = '用户名';
$password = '密码';
  $usercenter = loginweibo($username,$password);
  echo sendmsg("不知道能不能成功呢!");
  function sendmsg($msg){
    global $cookie_file,$usercenter;
    $posturl = "http://weibo.com/aj/mblog/add?_wv=5&__rnd=".time()."707";
    $postdata['text'] = $msg;
    $postdata['pic_id'] = "";
    $postdata['rank'] = 0;
    $postdata['rankid'] = "";
    $postdata['_surl'] = "";
    $postdata['hottopicid'] = "";
    $postdata['location'] = "home";
    $postdata['module'] = "stissue";
    $postdata['_t'] = 0;
    foreach($postdata as $key =>$value){
        $tmp .= $key."=".$value."&";
    }
    $post = trim($tmp,"&");
    $header = array(
      'host:weibo.com',
      'accept: */*',
      'accept-language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
      'content-type:application/x-www-form-urlencoded',
      'x-requested-with:xmlhttprequest',
      'referer: '.$usercenter,
    );
    //var_dump($header);exit;
    $ch = curl_init();
    curl_setopt($ch,curlopt_url,$posturl);
    curl_setopt($ch,curlopt_returntransfer,1);
    curl_setopt($ch,curlopt_cookiefile, $cookie_file);
    curl_setopt($ch,curlopt_useragent,'mozilla/5.0 (windows nt 6.1; wow64; rv:30.0) gecko/20100101 firefox/30.0');
    curl_setopt($ch,curlopt_httpheader,$header);
    curl_setopt($ch,curlopt_post,1);
    curl_setopt($ch,curlopt_postfields,$post);
    $str = curl_exec($ch);
    curl_close($ch);
    return $str;
  }
  function loginweibo($u,$p){
    global $cookie_file ;
    //$password = sha1(sha1(sha1($p)).$encryption['servertime'].$encryption['nonce']);
    $password = $p;
    $username = base64_encode($u);
    $loginurl = 'https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1403138799543';
    $logindata['entry'] = 'sso';
    $logindata['gateway'] = '1';
    $logindata['from'] = 'null';
    $logindata['savestate'] = '30';
    $logindata['useticket'] = '0';
    $logindata['pagerefer'] = '';
    $logindata['vsnf'] = '1';
    $logindata['su'] = base64_encode($u);
    $logindata['service'] = 'sso';
    $logindata['sp'] = $password;
    $logindata['sr'] = '1920*1080';
    $logindata['encoding'] = 'utf-8';
    $logindata['cdult'] = '3';
    $logindata['domain'] = 'sina.com.cn';
    $logindata['prelt'] = '0';
    $logindata['returntype'] = 'text';
    //var_dump($logindata);exit;
    $login = json_decode(loginpost($loginurl,$logindata),true);
    //获取微博
    get($login['crossdomainurllist'][0]);
    $ch = curl_init();
    curl_setopt($ch,curlopt_url,"http://weibo.com");  
    curl_setopt($ch,curlopt_returntransfer,1);  
    curl_setopt($ch,curlopt_cookiefile, $cookie_file);  
    curl_setopt($ch,curlopt_cookiejar,$cookie_file);  
    $return = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    $ch = curl_init();
    curl_setopt($ch,curlopt_url,$info['redirect_url']);  
    curl_setopt($ch,curlopt_returntransfer,1);  
    curl_setopt($ch,curlopt_cookiefile, $cookie_file);  
    curl_setopt($ch,curlopt_cookiejar,$cookie_file);  
    curl_exec($ch);
    curl_close($ch);
    return $info['redirect_url'];
  }
  function loginpost($url,$data){
    global $cookie_file ;
    //echo $cookie_file ;exit;
    $tmp = '';
    if(is_array($data)){
        foreach($data as $key =>$value){
            $tmp .= $key."=".$value."&";
        }
        $post = trim($tmp,"&");
    }else{
        $post = $data;
    }
    $ch = curl_init();
    curl_setopt($ch,curlopt_url,$url);  
    curl_setopt($ch,curlopt_returntransfer,1);  
    curl_setopt($ch, curlopt_ssl_verifypeer, false);
    curl_setopt($ch, curlopt_ssl_verifyhost, false);    
    curl_setopt($ch,curlopt_post,1);
    curl_setopt($ch,curlopt_postfields,$post);
    curl_setopt($ch,curlopt_cookiejar,$cookie_file);
    curl_setopt($ch,curlopt_cookiejar,$cookie_file);
    $return = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    return $return;
  }
  function post($url,$data){
    global $cookie_file;
    $tmp = '';
    if(is_array($data)){
        foreach($data as $key =>$value){
            $tmp .= $key."=".$value."&";
        }
        $post = trim($tmp,"&");
    }else{
        $post = $data;
    }
    $ch = curl_init();
    curl_setopt($ch,curlopt_url,$url);  
    curl_setopt($ch,curlopt_returntransfer,1);      
    curl_setopt($ch,curlopt_post,1);
    curl_setopt($ch,curlopt_cookiefile, $cookie_file);
    curl_setopt($ch,curlopt_postfields,$post);
    $return = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    return $return;
  }
  function get($url,$getinfo=false){
      global $cookie_file;  
      $ch = curl_init();
      curl_setopt($ch,curlopt_url,$url);  
      curl_setopt($ch,curlopt_returntransfer,1);  
      curl_setopt($ch,curlopt_cookiefile, $cookie_file);  
      curl_setopt($ch,curlopt_cookiejar,$cookie_file);  
      curl_setopt($ch, curlopt_ssl_verifypeer, false);
      curl_setopt($ch, curlopt_ssl_verifyhost, false);      
      $return = curl_exec($ch);
      $info = curl_getinfo($ch);
      if($getinfo)return $info;
      curl_close($ch);
      return $return;
  }
  function mp(){
    $getrndcode = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinassocontroller.prelogincallback&su=&rsakt=mod&client=ssologin.js(v1.4.15)&_=1403081692608";
    @preg_match_all('/\((.*?)\)/is',get($getrndcode,$data),$data);
    $encryption = json_decode($data[1][0],true);
    return $encryption;
  }