php獲取新浪微博數(shù)據(jù)API的實(shí)例代碼
分享下php取得新浪微博數(shù)據(jù)API的一個(gè)例子,學(xué)習(xí)下在php編程中,使用新浪微博數(shù)據(jù)API進(jìn)行開(kāi)發(fā)的方法,感興趣的朋友可以參考下。
php獲取新浪微博數(shù)據(jù)API
要取得新浪微博的數(shù)據(jù),可以通過(guò)其提供的API,地址:/pic/wiki/API文檔_V2。
獲取數(shù)據(jù)的方法:
復(fù)制代碼 代碼示例:
<?php
/**
* 通過(guò)新浪微博數(shù)據(jù)API取得微博數(shù)據(jù)
* edit: www.jbxue.com
*/
function getWeiboData()
{
$count = 15;
/pic/strong>
$url = "/pic/2/statuses/home_timeline.json?source=123456789&count=".$count."&page=1";
echo $url.'<br />';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
/pic/p>
/pic/p>
/pic/p>
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
/pic/strong>
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>';
}
?>
說(shuō)明:
json_decode($data) 會(huì)輸出一個(gè)對(duì)象,而json_decode($data, true) 則強(qiáng)制輸出為數(shù)組。
獲取數(shù)組則使用了 CURL 庫(kù)。
【php獲取新浪微博數(shù)據(jù)API的實(shí)例代碼】相關(guān)文章:
PHP實(shí)用的代碼實(shí)例08-17
PHP開(kāi)發(fā)微信支付實(shí)例代碼12-19
學(xué)習(xí)php分頁(yè)代碼實(shí)例12-06
PHP獲取MySQL數(shù)據(jù)庫(kù)里所有表的實(shí)現(xiàn)代碼11-04
實(shí)用的PHP實(shí)例代碼20個(gè)02-03