- 相關(guān)推薦
php獲取新浪微博數(shù)據(jù)API的實(shí)例代碼
分享下php取得新浪微博數(shù)據(jù)API的一個(gè)例子,學(xué)習(xí)下在php編程中,使用新浪微博數(shù)據(jù)API進(jìn)行開發(fā)的方法,感興趣的朋友可以參考下。
php獲取新浪微博數(shù)據(jù)API
要取得新浪微博的數(shù)據(jù),可以通過其提供的API,地址:http://open.weibo.com/wiki/API文檔_V2。
獲取數(shù)據(jù)的方法:
復(fù)制代碼 代碼示例:
<?php
/**
* 通過新浪微博數(shù)據(jù)API取得微博數(shù)據(jù)
* edit: www.jbxue.com
*/
function getWeiboData()
{
$count = 15;
// 參數(shù)source后面輸入你的授權(quán)號(hào)
$url = "https://api.weibo.com/2/statuses/home_timeline.json?source=123456789&count=".$count."&page=1";
echo $url.'<br />';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
// 設(shè)置是否顯示header信息 0是不顯示,1是顯示 默認(rèn)為0
//curl_setopt($curl, CURLOPT_HEADER, 0);
// 設(shè)置cURL 參數(shù),要求結(jié)果保存到字符串中還是輸出到屏幕上。0顯示在屏幕上,1不顯示在屏幕上,默認(rèn)為0
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 要驗(yàn)證的用戶名密碼
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
$data = curl_exec($curl);
curl_close($curl);
$result = json_decode($data, true);
echo '<pre>';
print_r($result);
echo '</pre>';
}
?>
說明:
json_decode($data) 會(huì)輸出一個(gè)對(duì)象,而json_decode($data, true) 則強(qiáng)制輸出為數(shù)組。
獲取數(shù)組則使用了 CURL 庫。
【php獲取新浪微博數(shù)據(jù)API的實(shí)例代碼】相關(guān)文章:
PHP實(shí)用的代碼實(shí)例08-12
學(xué)習(xí)php分頁代碼實(shí)例05-20
PHP獲取MySQL數(shù)據(jù)庫里所有表的實(shí)現(xiàn)代碼05-28
php網(wǎng)站來路獲取代碼04-25