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;
}
?>
日志分类
最近日志
随机日志
最新评论
- 美图看看
哎 找些药酒擦擦 - 土
我今年都25了······ - 南宁搬家公司
越听越有感觉 - 南宁搬家公司
说实在的我学计算机的excel还没学会 - 草民
我也是学php的不过现在什么也不会了 - 南宁搬家公司
最是无情了 - 南宁搬家公司
我们都已不再年轻了 - 阿芙精油
我正遇到这个问题,以前的都是从数据库读取 - 宜春人才网
情人节过去好久了。。 - 杏林男科
一不小心三八节都过了哈哈
日志档案
- 2012年4月(1)
- 2012年2月(2)
- 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)


