php curl写个新浪发sina微博的API接口
作者:寒川 发布于:2010-7-31 22:24 Saturday 分类:网页编程
一直没得到新浪微博API Key,自己用php curl写个新浪发sina微博的API接口,发出来分享,和《封装一个php发QQ微博的类
》是一个原理,几乎一模一样了。废话不多说,直接上菜:
<?php
/*
*How to use?
*publish("username","password","have a test.");
*Copy Right 寒川
*URL:http://huikon.cn
*/
function publish($username, $password, $content=''){//发布
$referURL = 'http://t.sina.com.cn';
$url = 'http://t.sina.com.cn/mblog/publish.php';
$fields = array(
'content'=>urlencode($content) ,
);
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
rtrim($fields_string ,'&') ;
$fields_string = substr($fields_string, 0,-5);
$cookie_jar = login($username, $password);
$curl = curl_init($url) ;
curl_setopt($curl, CURLOPT_POST,count($fields)) ;
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields_string) ;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($curl, CURLOPT_REFERER, $referURL);
curl_exec($curl);
curl_close($curl);
unlink($cookie_jar);
}
function login($username, $password){//登录
$loginURL = 'https://login.sina.com.cn/sso/login.php?username='.$username.'&password='.$password.'&returntype=TEXT';
$curl = curl_init($loginURL);
$cookie_jar = tempnam('.', 'cookie');
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_exec($curl);
curl_close($curl);
return $cookie_jar;
}
?>
评论:
日志分类
最近日志
随机日志
最新评论
- CCC certification
对这块还真是不... - 那风筝
happy new year ! - Roy
你好, 交换个链... - 太阳城娱乐网
说实话,对钛白... - 卡特&&艾弗森
不错啊!!,过段... - 奇遇
沙发 - 寒川
@Mr.邱℡:现在可... - Mr.邱℡
曾经加过,失效... - 寒川
@欧盛网络:链接... - 欧盛网络
换个链接好不?...
日志档案
- 2011年11月(2)
- 2011年9月(1)
- 2011年8月(2)
- 2011年7月(2)
- 2011年5月(1)
- 2011年3月(3)
- 2011年2月(2)
- 2010年12月(1)
- 2010年11月(2)
- 2010年10月(3)
- 2010年9月(9)
- 2010年8月(8)
- 2010年7月(13)
- 2010年6月(18)
- 2010年5月(24)
- 2010年4月(10)
- 2010年3月(14)
- 2010年2月(6)
- 2010年1月(7)
- 2009年11月(2)
- 2009年10月(3)
- 2009年9月(3)
- 2009年8月(8)
- 2009年7月(15)
- 2009年6月(14)
- 2009年4月(2)
- 2008年12月(1)
- 2008年6月(1)
- 2008年5月(10)
- 2008年4月(9)
- 2008年3月(5)
- 2008年2月(2)
- 2008年1月(5)
- 2007年12月(4)
- 2006年9月(6)
- 2006年7月(1)



2010-11-27 21:18