新浪微博PHP类库

极速赛车手机版下载 PHP新浪微博类库

<?php
/*
* 新浪类库
* 发布内容/登录账号
* www.god-gaming.com
*/

class Sina {
	/**
	 * 新浪微博发布
	 */
	public static function update($text,$pic_id='005BYqpggy1fvftfrsxb8j31hc0xckft',$cookie){
	$post=[
		'title' =>'今日要说什么?',
		'location' => 'v6_content_home',
		'text' => $text,
		'pic_id' => $pic_id,
		'isReEdit' => false,
		'pub_source' => 'page_2',
		'topic_id' => '1022%3A',
		'pub_type' => 'dialog',
		'_t' => 0,
		'style_type' => 1,
	];
	$url='https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=2918942797035';
	$referer='https://weibo.com/u/2218641227/home?topnav=1&wvr=6';
	$ret = self::_curl($url,$post,false,$cookie,$referer);
	return json_decode($ret,true);
	}

	/**
	 * 上传图片
	 */
	public static function upload($file, $multipart = true,$cookie) {
		$url = 'http://picupload.service.weibo.com/interface/pic_upload.php'.'?mime=image%2Fjpeg&data=base64&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog';
		if($multipart) {
		$url .= '&cb=http://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_'.time();
		if (class_exists('CURLFile')) { // php 5.5
			$post['pic1'] = new \CURLFile(realpath($file));
		} else {
			$post['pic1'] = '@'.realpath($file);
		}
		}else {
			$post['b64_data'] = base64_encode(self::_curl($file));
		}
		$ch = curl_init($url);
		curl_setopt_array($ch, array(
			CURLOPT_POST => true,
			CURLOPT_VERBOSE => true,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HTTPHEADER => array("Cookie: $cookie"),
			CURLOPT_POSTFIELDS => $post,
		));
		$output = curl_exec($ch);
		curl_close($ch);
		// 正则表达式提取返回结果中的json数据
		preg_match('/({.*)/i', $output, $match);
		if(!isset($match[1])) return array('code'=>'201','msg'=>'上传失败');
		$a=json_decode($match[1],true);
		$width = $a['data']['pics']['pic_1']['width'];
		$size = $a['data']['pics']['pic_1']['size'];
		$height = $a['data']['pics']['pic_1']['height'];
		$pid = $a['data']['pics']['pic_1']['pid'];
		if(!$pid) return array('code'=>'201','msg'=>'上传失败');
		$arr = array('code'=>'200','width'=>$width,"height"=>$height,"size"=>$size,"pid"=>$pid,"imgurl"=>"https://ws2.sinaimg.cn/large/".$pid.".jpg");
		return $arr;
	}

	/**
	 * 登录新浪微博
	 */
	public static function Login($u,$p){
		$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'] = $p;
		$loginData['sr'] = '1920*1080';
		$loginData['encoding'] = 'UTF-8';
		$loginData['cdult'] = '3';
		$loginData['domain'] = 'sina.com.cn';
		$loginData['prelt'] = '0';
		$loginData['returntype'] = 'TEXT';

		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$loginUrl); 
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
		curl_setopt($ch,CURLOPT_HEADER,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,http_build_query($loginData));
		$return = curl_exec($ch);
		list($header, $body) = explode("\r\n\r\n", $return); 
		preg_match("/Set\-Cookie: SUB([^\r\n]*);/i", $header, $matches); 
		curl_close($ch);
		$arr = json_decode($body, true);
		if ($arr['retcode'] == 0) $arr['cookie'] = 'SUB'.$matches[1];
		return $arr;
	}

	private static function _curl($url,$post=0,$header=0,$cookie=0,$referer=0,$ua=0,$nobaody=0){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$httpheader[] = "Accept:*/*";
		$httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
		$httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
		$httpheader[] = "Connection:close";
		curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
		if($post){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		if($header){
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
		}
		if($cookie){
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		if($referer){
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}
		if($ua){
			curl_setopt($ch, CURLOPT_USERAGENT,$ua);
		}else{
			curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36');
		}
		if($nobaody){
			curl_setopt($ch, CURLOPT_NOBODY,1);
		}
		curl_setopt($ch, CURLOPT_ENCODING, "gzip");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$ret = curl_exec($ch);
		curl_close($ch);
		return $ret;
	}
}

转载请注明出处 AE博客|墨渊 » 新浪微博PHP类库

相关推荐

发表评论

路人甲

网友评论(0)