• 2022-09-04被罚款200元记6分.
  • 特么的.电脑风扇坏了.快递还全部停发.太难了...求求了.疫情赶紧走吧.
  • 难啊难!要钱难!
  • 更新到WordPress5.6啦
  • 有点伤心了,今年净遇到王某海这种人.
  • 难啊难...
  • 七牛的JS SDK 的文档真坑啊.
  • 蓝奏云分享部分地区无法访问需手动修改www.lanzous.com变为:www.lanzoux.com
  • 好气啊~原来使用的CDN服务商莫名其妙的给我服务取消了~
  • 遇见一个沙雕汽车人.

新浪微博PHP类库

PHP KIENG 6年前 (2018-12-27) 148346次浏览 已收录 0个吐槽 扫描二维码

新浪微博 PHP 类库
新浪PHP类.蛮不错的!

<?php
/*
* 新浪类库
* 发布内容/登录账号
* www.aeink.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;
	}

	/**
	 * 登录<a href="https://blog.kieng.cn/tag/%e6%96%b0%e6%b5%aa" title="查看更多关于新浪的文章" target="_blank">新浪</a>微博
	 */
	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;
	}
}

KIENG.CN , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA 4.0协议进行授权
转载请注明出处:新浪微博 PHP 类库
本文章链接:https://blog.kieng.cn/418.html
喜欢 (2)
KIENG
关于作者:
一个热衷网络的Man
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 快速获取昵称
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址