一直没得到新浪微博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;
}
?>