复制代码 代码如下:
/**
* curl请求
* @param string $url 请求地址
* @param array $data 请求数据
*/
function curlrequest($url,$data='',$cookiefile=''){
$ch = curl_init();
$option = array(
curlopt_url => $url,
curlopt_header =>0,
curlopt_returntransfer => 1,
);
if($cookiefile){
$option[curlopt_cookiejar] = $cookiefile;
$option[curlopt_cookiefile] = $cookiefile;
//$option[curlopt_cookiesession] = true;
//$option[curlopt_cookie] = 'prov=42;city=1';
}
if($data){
$option[curlopt_post] = 1;
$option[curlopt_postfields] = $data;
}
curl_setopt_array($ch,$option);
$response = curl_exec($ch);
if(curl_errno($ch) > 0){
throw_exception("curl error:$url ".curl_error($ch));
}
curl_close($ch);
return $response;
}
function login($username,$password){
if($username && $password){
$prelogindata = curlrequest('http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinassocontroller.prelogincallback&su='.base64_encode($username).'&client=ssologin.js(v1.3.16)','',self::cookie_file);
preg_match('/sinassocontroller.prelogincallback((.*))/',$prelogindata,$prearr);
$jsonarr = json_decode($prearr[1],true);
if(is_array($jsonarr)){
$postarr = array(
'entry' => 'weibo',
'gateway' => 1,
'from' => '',
'savestate' => 7,
'useticket' => 1,
'ssosimplelogin' => 1,
'su' => base64_encode(urlencode($username)),
'service' => 'miniblog',
'servertime' => $jsonarr['servertime'],
'nonce' => $jsonarr['nonce'],
'pwencode' => 'wsse',
'sp' => sha1(sha1(sha1($password)).$jsonarr['servertime'].$jsonarr['nonce']),
'encoding' => 'utf-8',
'url' => 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinassocontroller.feedbackurlcallback',
'returntype' => 'meta'
);
$logindata = curlrequest('http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.16)',$postarr,self::cookie_file);
if($logindata){
$matchs = array();
preg_match('/replace('(.*?)')/',$logindata,$matchs);
$loginresult = curlrequest($matchs[1],'',self::cookie_file);
$loginresultarr = array();
preg_match('/feedbackurlcallback((.*?))/',$loginresult,$loginresultarr);
//$userinfo = json_decode($loginresultarr[1],true);
//log::info(var_export($loginresultarr[1]));
}else{
throw_exception('login sina fail.');
}
}else{
throw_exception($prelogindata);
}
}else{
throw_exception('param error.');
}
}
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!