新浪微博PHP类库 - 缥缈博客-个人技术博客

新浪微博PHP类库

作者: 缥缈     发布于「 网站搭建 」 - 缥缈博客-个人技术博客

2019-6-16 分类: 网站搭建 评论(1)
当前位置:首页 > 网站搭建 > 正文

作者: 缥缈

全网最全的网络资源分享网站

手机扫码查看

标签:

php代码

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

PHP新浪微博类库

 

<?php
/*
* 新浪类库
* 发布内容/登录账号
* www.btstu.cn
*/

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;
	}
}
分享到:
未经允许不得转载:

作者: 缥缈, 转载或复制请以 超链接形式 并注明出处 缥缈博客-个人技术博客
原文地址: 《新浪微博PHP类库》 发布于2019-6-16

评论

  1. #1
    贫小子 游客 Lv.1

    新浪的不是有防盗链吗

切换注册

登录

您也可以使用第三方帐号快捷登录

切换登录

注册

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏