返回首页
当前位置: 主页 > 网络编程 > Php实例教程 >

利用php curl写个新浪发sina微博的API接口

时间:2011-09-29 13:38来源:知行网www.zhixing123.cn 编辑:麦田守望者

<?php

/*

*How to use?

*publish("username","password","have a test.");

*/

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.'&amp;' ; }
rtrim($fields_string ,'&amp;') ;
$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;
}

?>
 

顶一下
(0)
0%
踩一下
(0)
0%
标签(Tag):php php实例教程 php源代码 php基础教程 php技巧
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片