• 还有半个月就走了…挺好的.
  • 无聊啊,最近也没啥更新的.
  • 妈的,写之前不说,写完了了你说不行.中途我还问你你说对.你真是中国第一Monkey
  • 沃妮马,过的真快!转眼又上班了.等待十月一.
  • 还有1天就是端午节了.过的真快, 在这个公司工作半年啦..
  • 咦~哪个弟弟CC我呢?
  • 简单,简单.我提个思路.
  • 不算今天还有2天就放假了.
  • 一切随缘,随波逐流.
  • 我的天啊,手欠把写了两天的项目删除了,回收站都没有!真是一干二净!心态爆炸!

新浪微博PHP类库

PHP KIENG 10个月前 (12-27) 63833次浏览 已收录 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 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA 4.0协议进行授权
转载请注明原文链接:新浪微博 PHP 类库
本文章链接:https://blog.kieng.cn/418.html
喜欢 (0)
KIENG
关于作者:
一个热衷网络的Man
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

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

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