Home >php教程 >php手册 >php获取新浪微博数据API实例

php获取新浪微博数据API实例

WBOY
WBOYOriginal
2016-06-13 09:30:55731browse

function getWeiboData()
{
 $count = 15;
 // 参数source后面输入你的授权号
 $url = "https://api.weibo.com/2/statuses/home_timeline.json?source=123456789&count=".$count."&page=1";
 echo $url.'
';

 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, $url);
 // 设置是否显示header信息 0是不显示,1是显示  默认为0
 //curl_setopt($curl, CURLOPT_HEADER, 0);
 // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。0显示在屏幕上,1不显示在屏幕上,默认为0
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 // 要验证的用户名密码
 curl_setopt($curl, CURLOPT_USERPWD, "username:password");
 $data = curl_exec($curl);
 curl_close($curl);

 $result = json_decode($data, true);

 echo '

';<br> print_r($result);<br> echo '
';
}
?>
补充一下,json_decode($data) 会输出一个对象,而json_decode($data, true) 则强制输出为数组。获取数组则使用了 CURL 库。
PHP CodeBase 代码库计划,一般一次收集一个函数,解决一个问题。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn